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

Support for Nested Relationships for the Json Adapter #1114

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactored a bit more, and matched some method names to that of json_api
  • Loading branch information
NullVoxPopuli committed Sep 1, 2015
commit 8df52d558bcf6d3144606bd6ec2c3858f0144501
64 changes: 43 additions & 21 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,73 @@ def serializable_hash(options = nil)
else
@hash = {}

@core = serialized_attributes_of(serializer, options)
@core = resource_object_for(serializer, options)

serializer.associations.each do |association|
serializer = association.serializer
opts = association.options
add_resource_relationships(serializer)

if serializer.respond_to?(:each)
@hash[association.key] = serialize_array(serializer, opts)
else
@hash[association.key] = serialized_or_virtual_of(serializer, opts)
end
end
@result = @core.merge @hash
end

{ root => @result }
end

def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
end

private
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# iterate through the associations on the serializer,
# adding them to @hash as needed (as singular or plural)
def add_resource_relationships(serializer)
serializer.associations.each do |association|
serializer = association.serializer
opts = association.options

if serializer.respond_to?(:each)
add_relationships(association.key, serializer, opts)
else
add_relationship(association.key, serializer, opts)
end
end

@hash
end

# add a singular relationship
def add_relationship(key, serializer, options)
@hash[key] = serialized_or_virtual_of(serializer, options)
end

# add a many relationship
def add_relationships(key, serializer, options)
@hash[key] = serialize_array(serializer, options)
end

def serialize_array_without_root(serializer, options)
serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the serializer should be calling the adapter... but I guess you didn't add that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the json_api adapter

serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
does

          if serializer.respond_to?(:each)
            serializer.each do |s|
              result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
              @hash[:data] << result[:data]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that doesn't look very nice to look at. :-\

aren't there json_api refactors in the prs?

I'm curious how that cleans things up.


# TODO: what is a virtual value?
# a virtual value is something that doesn't need a serializer,
# such as a ruby array, or any other raw value
def serialized_or_virtual_of(serializer, options)
if serializer && serializer.object
serialized_attributes_of(serializer, options)
resource_object_for(serializer, options)
elsif options[:virtual_value]
options[:virtual_value]
end
end

def serialized_attributes_of(item, options)
cache_check(item) do
item.attributes(options)
end
end

def serialize_array(serializer, options)
serializer.map do |item|
serialized_attributes_of(item, options)
resource_object_for(item, options)
end
end

def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
def resource_object_for(item, options)
cache_check(item) do
item.attributes(options)
end
end

end
Expand Down