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, assigned_model: 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.

TODO: make foreign_key a keyword once the satellite gems are rolled in

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

  • assigned_model (Model) (defaults to: nil)

    a model already built by #assign, to be assigned onto rather than rebuilt



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphiti/util/persistence.rb', line 13

def initialize(resource, meta, attributes, relationships, caller_model, foreign_key = nil, assigned_model: nil)
  @resource = resource
  @assigned_model = assigned_model
  @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

#assignObject

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.



31
32
33
34
35
36
37
# File 'lib/graphiti/util/persistence.rb', line 31

def assign
  attributes = @adapter.persistence_attributes(self, @attributes)
  assigned = @resource.assign(attributes, , @meta[:method], model_instance: @assigned_model)
  @resource.decorate_record(assigned)

  assigned
end

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



85
86
87
88
89
90
91
92
93
94
# File 'lib/graphiti/util/persistence.rb', line 85

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/graphiti/util/persistence.rb', line 58

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

  payload_attributes = attributes.dup
  parents = @adapter.process_belongs_to(self, attributes)
  apply_derived_attributes(attributes, payload_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