Class: JsonapiCompliable::Util::Persistence
- Inherits:
-
Object
- Object
- JsonapiCompliable::Util::Persistence
- Defined in:
- lib/jsonapi_compliable/util/persistence.rb
Instance Method Summary collapse
- #assign_temp_id(object, temp_id) ⇒ Object
- #associate_children(object, children) ⇒ Object
- #associate_parents(object, parents) ⇒ Object
-
#initialize(resource, meta, attributes, relationships) ⇒ Persistence
constructor
A new instance of Persistence.
- #persist_object(method, attributes) ⇒ Object
- #process_belongs_to(relationships) ⇒ Object
- #process_has_many(relationships) ⇒ Object
-
#run ⇒ Object
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.
-
#update_foreign_key(parent_object, attrs, x) ⇒ Object
The child's attributes should be modified to nil-out the foreign_key when the parent is being destroyed or disassociated.
- #update_foreign_key_for_parents(parents) ⇒ Object
Constructor Details
#initialize(resource, meta, attributes, relationships) ⇒ Persistence
Returns a new instance of Persistence.
2 3 4 5 6 7 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 2 def initialize(resource, , attributes, relationships) @resource = resource @meta = @attributes = attributes @relationships = relationships end |
Instance Method Details
#assign_temp_id(object, temp_id) ⇒ Object
98 99 100 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 98 def assign_temp_id(object, temp_id) object.instance_variable_set(:@_jsonapi_temp_id, temp_id) end |
#associate_children(object, children) ⇒ Object
60 61 62 63 64 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 60 def associate_children(object, children) children.each do |x| x[:sideload].associate(object, x[:object]) if x[:object] && object end end |
#associate_parents(object, parents) ⇒ Object
54 55 56 57 58 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 54 def associate_parents(object, parents) parents.each do |x| x[:sideload].associate(x[:object], object) if x[:object] && object end end |
#persist_object(method, attributes) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 66 def persist_object(method, attributes) case method when :destroy @resource.destroy(attributes[:id]) when :disassociate, nil @resource.update(attributes) else @resource.send(method, attributes) end end |
#process_belongs_to(relationships) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 88 def process_belongs_to(relationships) [].tap do |processed| iterate(only: [:belongs_to]) do |x| x[:object] = x[:sideload].resource .persist_with_relationships(x[:meta], x[:attributes], x[:relationships]) processed << x end end end |
#process_has_many(relationships) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 77 def process_has_many(relationships) [].tap do |processed| iterate(except: [:belongs_to]) do |x| yield x x[:object] = x[:sideload].resource .persist_with_relationships(x[:meta], x[:attributes], x[:relationships]) processed << x end end end |
#run ⇒ Object
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
- return current object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 22 def run parents = process_belongs_to(@relationships) update_foreign_key_for_parents(parents) persisted = persist_object(@meta[:method], @attributes) assign_temp_id(persisted, @meta[:temp_id]) associate_parents(persisted, parents) children = process_has_many(@relationships) do |x| update_foreign_key(persisted, x[:attributes], x) end associate_children(persisted, children) persisted unless @meta[:method] == :destroy end |
#update_foreign_key(parent_object, attrs, x) ⇒ Object
The child's attributes should be modified to nil-out the foreign_key when the parent is being destroyed or disassociated
40 41 42 43 44 45 46 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 40 def update_foreign_key(parent_object, attrs, x) if [:destroy, :disassociate].include?(x[:meta][:method]) attrs[x[:foreign_key]] = nil else attrs[x[:foreign_key]] = parent_object.send(x[:primary_key]) end end |
#update_foreign_key_for_parents(parents) ⇒ Object
48 49 50 51 52 |
# File 'lib/jsonapi_compliable/util/persistence.rb', line 48 def update_foreign_key_for_parents(parents) parents.each do |x| update_foreign_key(x[:object], @attributes, x) end end |