Skip to content

Commit

Permalink
Remove building included object to BaseHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
nikajukic committed May 25, 2021
1 parent d088a2e commit 48b45f7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/jsonapi_parameters/default_handlers/base_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def find_included_object(related_id:, related_type:)
end
end

def build_included_object(included_object, related_id)
{ **(included_object[:attributes] || {}) }.tap do |body|
body[:id] = related_id unless client_generated_id?(related_id)
body[:relationships] = included_object[:relationships] if included_object.key?(:relationships) # Pass nested relationships
end
end

def client_generated_id?(related_id)
related_id.to_s.starts_with?(JsonApi::Parameters.client_id_prefix)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def prepare_relationship_vals
@with_inclusion &= !included_object.empty?

if with_inclusion
{ **(included_object[:attributes] || {}) }.tap do |body|
body[:id] = related_id unless client_generated_id?(related_id)
body[:relationships] = included_object[:relationships] if included_object.key?(:relationships) # Pass nested relationships
end
build_included_object(included_object, related_id)
else
relationship.dig(:id)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ def handle

return ["#{singularize(relationship_key)}_id".to_sym, related_id] if included_object.empty?

included_object = { **(included_object[:attributes] || {}) }.tap do |body|
body[:id] = related_id unless client_generated_id?(related_id)
body[:relationships] = included_object[:relationships] if included_object.key?(:relationships) # Pass nested relationships
end
included_object = build_included_object(included_object, related_id)

["#{singularize(relationship_key)}_attributes".to_sym, included_object]
end
Expand Down

0 comments on commit 48b45f7

Please sign in to comment.