lib/graphiti/util/persistence.rb in graphiti-1.0.rc.21 vs lib/graphiti/util/persistence.rb in graphiti-1.0.rc.22

- old
+ new

@@ -14,11 +14,11 @@ @relationships = relationships @caller_model = caller_model @foreign_key = foreign_key # Find the correct child resource for a given jsonapi type - if meta_type = @meta[:type].try(:to_sym) + if (meta_type = @meta[:type].try(:to_sym)) if @resource.type != meta_type && @resource.polymorphic? @resource = @resource.class.resource_for_type(meta_type).new end end @@ -52,13 +52,13 @@ @resource.decorate_record(persisted) assign_temp_id(persisted, @meta[:temp_id]) associate_parents(persisted, parents) - children = process_has_many(@relationships, persisted) do |x| + children = process_has_many(@relationships, persisted) { |x| update_foreign_key(persisted, x[:attributes], x) - end + } associate_children(persisted, children) unless @meta[:method] == :destroy post_process(persisted, parents) post_process(persisted, children) @@ -75,14 +75,14 @@ # in the graph, the foreign key gets merged into the child's attributes # This attribute should *not* need to be marked writable, as that # would allow writing as a straight attribute instead of just an association def typecast_attributes @attributes.each_pair do |key, value| - if @foreign_key == key - @attributes[key] = value + @attributes[key] = if @foreign_key == key + value else - @attributes[key] = @resource.typecast(key, value, :writable) + @resource.typecast(key, value, :writable) end end end def add_hook(prc, lifecycle_event) @@ -132,20 +132,16 @@ if x[:sideload].type == :belongs_to x[:sideload].disassociate(object, x[:object]) else x[:sideload].disassociate(x[:object], object) end + elsif x[:sideload].type == :belongs_to + x[:sideload].associate(object, x[:object]) + elsif [:has_many, :many_to_many].include?(x[:sideload].type) + x[:sideload].associate_all(object, Array(x[:object])) else - if x[:sideload].type == :belongs_to - x[:sideload].associate(object, x[:object]) - else - if [:has_many, :many_to_many].include?(x[:sideload].type) - x[:sideload].associate_all(object, Array(x[:object])) - else - x[:sideload].associate(x[:object], object) - end - end + x[:sideload].associate(x[:object], object) end end end end @@ -155,17 +151,16 @@ if x[:meta][:method] == :disassociate x[:sideload].disassociate(object, x[:object]) elsif x[:meta][:method] == :destroy if x[:sideload].type == :many_to_many x[:sideload].disassociate(object, x[:object]) - end # otherwise, no need to disassociate destroyed objects - else - if [:has_many, :many_to_many].include?(x[:sideload].type) - x[:sideload].associate_all(object, Array(x[:object])) - else - x[:sideload].associate(object, x[:object]) end + # otherwise, no need to disassociate destroyed objects + elsif [:has_many, :many_to_many].include?(x[:sideload].type) + x[:sideload].associate_all(object, Array(x[:object])) + else + x[:sideload].associate(object, x[:object]) end end end end @@ -241,10 +236,10 @@ { method: @meta[:method], temp_id: @meta[:temp_id], caller_model: @caller_model, attributes: @attributes, - relationships: @relationships + relationships: @relationships, } end def call_resource_method(method_name, attributes, caller_model) method = @resource.method(method_name)