Module: ZendeskAPI::Save

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

Instance Method Summary collapse

Methods included from ResponseHandler

#handle_response

Instance Method Details

#clear_associationsObject

Removes all cached associations



56
57
58
59
60
61
# 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) ⇒ Object

Saves, returning false if it fails and attaching the errors



46
47
48
49
50
51
52
53
# 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 = {}) ⇒ Boolean

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?



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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_associationsObject

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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