module ZendeskAPI::Save

Public Instance Methods

clear_associations() click to toggle source

Removes all cached associations

# File lib/zendesk_api/actions.rb, line 56
def clear_associations
  self.class.associations.each do |association_data|
    name = association_data[:name]
    instance_variable_set("@#{name}", nil) if instance_variable_defined?("@#{name}")
  end
end
save(options = {}, &block) click to toggle source

Saves, returning false if it fails and attaching the errors

# File lib/zendesk_api/actions.rb, line 46
def save(options = {}, &block)
  save!(options, &block)
rescue ZendeskAPI::Error::RecordInvalid => e
  @errors = e.errors
  false
rescue ZendeskAPI::Error::ClientError
  false
end
save!(options = {}) { |req| ... } click to toggle source

If this resource hasn’t been deleted, then create or save it. Executes a POST if it is a {Data#new_record?}, otherwise a PUT. Merges returned attributes on success. @return [Boolean] Success?

# File lib/zendesk_api/actions.rb, line 17
def save!(options = {})
  return false if respond_to?(:destroyed?) && destroyed?

  if new_record? && !options[:force_update]
    method = :post
    req_path = path
  else
    method = :put
    req_path = url || path
  end

  req_path = options[:path] if options[:path]

  save_associations

  @response = @client.connection.send(method, req_path) do |req|
    req.body = attributes_for_save.merge(@global_params)

    yield req if block_given?
  end

  handle_response(@response)

  @attributes.clear_changes
  clear_associations
  true
end
save_associations() click to toggle source

Saves associations Takes into account inlining, collections, and id setting on the parent resource.

# File lib/zendesk_api/actions.rb, line 65
def save_associations
  self.class.associations.each do |association_data|
    association_name = association_data[:name]

    next unless send("#{association_name}_used?") && association = send(association_name)

    inline_creation = association_data[:inline] == :create && new_record?
    changed = association.is_a?(Collection) || association.changed?

    if association.respond_to?(:save) && changed && !inline_creation && association.save
      send("#{association_name}=", association) # set id/ids columns
    end

    if (association_data[:inline] == true || inline_creation) && changed
      attributes[association_name] = association.to_param
    end
  end
end