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

  # 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

#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



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

def run
  parents = process_belongs_to(@relationships)
  update_foreign_key_for_parents(parents)

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

  associate_parents(persisted, parents)

  children = process_has_many(@relationships, persisted) { |x|
    update_foreign_key(persisted, x[:attributes], x)
  }

  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