Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow an adapter to return a new object to replace the existing one. #367

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/graphiti/adapters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ def associate_all(parent, children, association_name, association_type)
associate(parent, c, association_name, association_type)
end
end

parent
end

def associate(parent, child, association_name, association_type)
Expand All @@ -379,6 +381,8 @@ def associate(parent, child, association_name, association_type)
else
parent.send(:"#{association_name}=", child)
end

parent
end

def disassociate(parent, child, association_name, association_type)
Expand Down
5 changes: 5 additions & 0 deletions lib/graphiti/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ def associate_all(parent, children, association_name, association_type)
else
super
end

parent
end

def associate(parent, child, association_name, association_type)
Expand All @@ -265,6 +267,8 @@ def associate(parent, child, association_name, association_type)
else
super
end

parent
end

# When a has_and_belongs_to_many relationship, we don't have a foreign
Expand All @@ -275,6 +279,7 @@ def disassociate(parent, child, association_name, association_type)
parent.send(association_name).delete(child)
end
# Nothing to do in the else case, happened when we merged foreign key
parent
end

# (see Adapters::Abstract#create)
Expand Down
27 changes: 16 additions & 11 deletions lib/graphiti/util/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def run
@resource.decorate_record(persisted)
assign_temp_id(persisted, @meta[:temp_id])

associate_parents(persisted, parents)
persisted = associate_parents(persisted, parents)

children = @adapter.process_has_many(self, persisted)

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

post_process(persisted, parents)
post_process(persisted, children)
Expand Down Expand Up @@ -93,38 +93,42 @@ def associate_parents(object, parents)
if x[:object] && object
if x[:meta][:method] == :disassociate
if x[:sideload].type == :belongs_to
x[:sideload].disassociate(object, x[:object])
object = x[:sideload].disassociate(object, x[:object])
else
x[:sideload].disassociate(x[:object], object)
object = x[:sideload].disassociate(x[:object], object)
end
elsif x[:sideload].type == :belongs_to
x[:sideload].associate(object, x[:object])
object = x[:sideload].associate(object, x[:object])
elsif [:has_many, :many_to_many].include?(x[:sideload].type)
x[:sideload].associate_all(object, Array(x[:object]))
object = x[:sideload].associate_all(object, Array(x[:object]))
else
x[:sideload].associate(x[:object], object)
object = x[:sideload].associate(x[:object], object)
end
end
end

object
end

def associate_children(object, children)
children.each do |x|
if x[:object] && object
if x[:meta][:method] == :disassociate
x[:sideload].disassociate(object, x[:object])
object = x[:sideload].disassociate(object, x[:object])
elsif x[:meta][:method] == :destroy
if x[:sideload].type == :many_to_many
x[:sideload].disassociate(object, x[:object])
object = x[:sideload].disassociate(object, x[:object])
end
# otherwise, no need to disassociate destroyed objects
elsif [:has_many, :many_to_many].include?(x[:sideload].type)
x[:sideload].associate_all(object, Array(x[:object]))
object = x[:sideload].associate_all(object, Array(x[:object]))
else
x[:sideload].associate(object, x[:object])
object = x[:sideload].associate(object, x[:object])
end
end
end

object
end

def persist_object(method, attributes)
Expand All @@ -151,6 +155,7 @@ def post_process(caller_model, processed)

def assign_temp_id(object, temp_id)
object.instance_variable_set(:@_jsonapi_temp_id, temp_id)
object
end

def metadata
Expand Down