Module: ZendeskAPI::Save
- Included in:
- Create, TicketComment, Update
- Defined in:
- lib/zendesk_api/actions.rb
Instance Method Summary collapse
- #clear_associations ⇒ Object
-
#save(options = {}) ⇒ Boolean
If this resource hasn’t been deleted, then create or save it.
- #save!(options = {}) ⇒ Object
- #save_associations ⇒ Object
Instance Method Details
#clear_associations ⇒ Object
40 41 42 43 44 45 |
# File 'lib/zendesk_api/actions.rb', line 40 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 = {}) ⇒ Boolean
If this resource hasn’t been deleted, then create or save it. Executes a POST if it is a #new_record?, otherwise a PUT. Merges returned attributes on 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(={}) 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 = [:path] if [: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 |
#save!(options = {}) ⇒ Object
36 37 38 |
# File 'lib/zendesk_api/actions.rb', line 36 def save!(={}) save() || raise("Save failed") end |
#save_associations ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/zendesk_api/actions.rb', line 47 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 |