Module: ZendeskAPI::Save

Included in:
Create, Ticket::Comment, Update
Defined in:
lib/zendesk_api/actions.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) clear_associations

Removes all cached associations



43
44
45
46
47
48
# File 'lib/zendesk_api/actions.rb', line 43

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

- (Boolean) save(options = {})

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.

Returns:

  • (Boolean)

    Success?



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zendesk_api/actions.rb', line 7

def save(options={})
  return false if respond_to?(:destroyed?) && destroyed?

  if new_record?
    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 = if self.class.unnested_params
      attributes.changes
    else
      {self.class.singular_resource_name.to_sym => attributes.changes}
    end
  end

  @attributes.replace @attributes.deep_merge(@response.body[self.class.singular_resource_name] || {})
  @attributes.clear_changes
  clear_associations
  true
end

- (Object) save!(options = {})

Saves, raising an Argument error if it fails

Raises:

  • (ArgumentError)

    if saving failed



38
39
40
# File 'lib/zendesk_api/actions.rb', line 38

def save!(options={})
  save(options) || raise("Save failed")
end

- (Object) save_associations

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zendesk_api/actions.rb', line 52

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.changes.empty?

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

    if association_data[:inline] == true || inline_creation
      attributes[association_name] = (association.is_a?(Collection) ? association.map(&:to_param) : association.to_param)
    end
  end
end