Class: Graphiti::Util::Persistence Private

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/util/persistence.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Save the given Resource#model, and all of its nested relationships.

Instance Method Summary collapse

Constructor Details

#initialize(resource, meta, attributes, relationships, caller_model, foreign_key = nil) ⇒ Persistence

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Persistence.

Parameters:

  • resource (Resource)

    the resource instance

  • meta (Hash)

    see (Deserializer#meta)

  • attributes (Hash)

    see (Deserializer#attributes)

  • relationships (Hash)

    see (Deserializer#relationships)

  • caller_model (Model)

    The persisted parent object in the request graph

  • foreign_key (Symbol) (defaults to: nil)

    Attribute assigned by parent object in graph



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphiti/util/persistence.rb', line 10

def initialize(resource, meta, attributes, relationships, caller_model, foreign_key = nil)
  @resource = resource
  @meta = meta
  @attributes = attributes
  @relationships = relationships
  @caller_model = caller_model
  @foreign_key = foreign_key
  @adapter = @resource.adapter

  # 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
    end
  end
end

Instance Method Details

#iterate(only: [], except: []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
74
75
76
77
78
79
80
# File 'lib/graphiti/util/persistence.rb', line 71

def iterate(only: [], except: [])
  opts = {
    resource: @resource,
    relationships: @relationships
  }.merge(only: only, except: except)

  Graphiti::Util::RelationshipPayload.iterate(**opts) do |x|
    yield x
  end
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

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

  • record hooks for later playback

  • run post-process sideload hooks

  • return current object

Returns:

  • a model instance



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/graphiti/util/persistence.rb', line 46

def run
  attributes = @adapter.persistence_attributes(self, @attributes)

  parents = @adapter.process_belongs_to(self, attributes)
  persisted = persist_object(@meta[:method], attributes)
  @resource.decorate_record(persisted)
  assign_temp_id(persisted, @meta[:temp_id])

  associate_parents(persisted, parents)

  children = @adapter.process_has_many(self, persisted)

  associate_children(persisted, children) unless @meta[:method] == :destroy

  post_process(persisted, parents)
  post_process(persisted, children)
  after_graph_persist = -> { @resource.after_graph_persist(persisted, ) }
  add_hook(after_graph_persist, :after_graph_persist)
  before_commit = -> { @resource.before_commit(persisted, ) }
  add_hook(before_commit, :before_commit)
  after_commit = -> { @resource.after_commit(persisted, ) }
  add_hook(after_commit, :after_commit)
  persisted
end