-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 1 commit
edfec6c
8df52d5
4f2ad3e
a8e8515
05ec32a
8a6ee84
ce75e78
de2d9b4
2fd748e
85406f9
75d7a95
670c724
9f8f7fc
f11fa95
4339749
608741f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
||||||
# 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the json_api adapter
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] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍