lib/jsonapi_compliable/util/persistence.rb in jsonapi_compliable-0.6.4 vs lib/jsonapi_compliable/util/persistence.rb in jsonapi_compliable-0.6.5

- old
+ new

@@ -1,26 +1,36 @@ +# Save the given Resource#model, and all of its nested relationships. +# @api private class JsonapiCompliable::Util::Persistence + # @param [Resource] resource the resource instance + # @param [Hash] meta see (Deserializer#meta) + # @param [Hash] attributes see (Deserializer#attributes) + # @param [Hash] relationships see (Deserializer#relationships) def initialize(resource, meta, attributes, relationships) @resource = resource @meta = meta @attributes = attributes @relationships = relationships end + # Perform the actual save logic. + # # belongs_to must be processed before/separately from has_many - # we need to know the primary key value of the parent before - # persisting the child + # persisting the child. # # Flow: # * process parents # * update attributes to reflect parent primary keys # * persist current object # * associate temp id with current object # * associate parent objects with current object # * process children # * associate children # * return current object + # + # @return the persisted model instance def run parents = process_belongs_to(@relationships) update_foreign_key_for_parents(parents) persisted = persist_object(@meta[:method], @attributes) @@ -33,10 +43,12 @@ associate_children(persisted, children) persisted unless @meta[:method] == :destroy end + private + # The child's attributes should be modified to nil-out the # foreign_key when the parent is being destroyed or disassociated def update_foreign_key(parent_object, attrs, x) if [:destroy, :disassociate].include?(x[:meta][:method]) attrs[x[:foreign_key]] = nil @@ -96,11 +108,9 @@ end def assign_temp_id(object, temp_id) object.instance_variable_set(:@_jsonapi_temp_id, temp_id) end - - private def iterate(only: [], except: []) opts = { resource: @resource, relationships: @relationships,