lib/graphiti/util/persistence.rb in graphiti-1.2.16 vs lib/graphiti/util/persistence.rb in graphiti-1.2.17

- old
+ new

@@ -6,16 +6,16 @@ # @param [Hash] attributes see (Deserializer#attributes) # @param [Hash] relationships see (Deserializer#relationships) # @param [Model] caller_model The persisted parent object in the request graph # @param [Symbol] foreign_key Attribute assigned by parent object in graph def initialize(resource, meta, attributes, relationships, caller_model, foreign_key = nil) - @resource = resource - @meta = meta - @attributes = attributes + @resource = resource + @meta = meta + @attributes = attributes @relationships = relationships - @caller_model = caller_model - @foreign_key = foreign_key + @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 @resource.type != meta_type && @resource.polymorphic? @resource = @resource.class.resource_for_type(meta_type).new @@ -175,13 +175,19 @@ end def process_belongs_to(relationships) [].tap do |processed| iterate(only: [:polymorphic_belongs_to, :belongs_to]) do |x| - x[:object] = x[:resource] - .persist_with_relationships(x[:meta], x[:attributes], x[:relationships]) - processed << x + begin + id = x.dig(:attributes, :id) + x[:object] = x[:resource] + .persist_with_relationships(x[:meta], x[:attributes], x[:relationships]) + processed << x + rescue Graphiti::Errors::RecordNotFound + path = "relationships/#{x.dig(:meta, :jsonapi_type)}" + raise Graphiti::Errors::RecordNotFound.new(x[:sideload].name, id, path) + end end end end def post_process(caller_model, processed) @@ -200,11 +206,11 @@ end def iterate(only: [], except: []) opts = { resource: @resource, - relationships: @relationships, + relationships: @relationships }.merge(only: only, except: except) Graphiti::Util::RelationshipPayload.iterate(opts) do |x| yield x end @@ -214,10 +220,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)