Skip to content

Commit

Permalink
Make process_resource return an Array of hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
beauby committed Oct 26, 2015
1 parent f1a16b7 commit 206f890
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def initialize(serializer, options = {})
def serializable_hash(options = nil)
options ||= {}

resources = {}
process_resource(serializer, @include_tree, true, resources)
resources = process_resource(serializer)
primary_data, included = resources.each_with_object([[], []]) do |(_, resource), (prim, incl)|
is_primary = resource.delete(:is_primary)
if is_primary
Expand Down Expand Up @@ -94,25 +93,27 @@ def fragment_cache(cached_hash, non_cached_hash)
# on included associations.
#
# @api private
def process_resource(serializer, include_tree, is_primary, hashes)
def process_resource(serializer, include_tree = @include_tree, is_primary = true, hashes = {})
if serializer.respond_to?(:each)
serializer.each { |s| process_resource(s, include_tree, is_primary, hashes) }
return
return hashes
end

return unless serializer && serializer.object
return hashes unless serializer && serializer.object

key = [resource_identifier_type_for(serializer), resource_identifier_id_for(serializer)]
if hashes[key]
hashes[key][:is_primary] ||= is_primary
return
return hashes
end

hashes[key] = resource_object_for(serializer).merge(is_primary: is_primary)

serializer.associations(include_tree).each do |association|
process_resource(association.serializer, include_tree[association.key], false, hashes)
end

hashes
end

# Transform a serializer into a resource object.
Expand Down

0 comments on commit 206f890

Please sign in to comment.