-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nested relationships for the json adapter
initial refactor to make way for easy feature adding refactored a bit more, and matched some method names to that of json_api rename add_relationship methods to be less awkward tests added all tests tpass remove binding.pry require fixed app warnings removed unused method changed restrict_to to nested_associations and changed nested_relationships to nested_associations_to_include removed unneeded parameter from add_resource_relationships simplify key check added another test, showing multiple relationship serializers refactored include_array_to_hash to a utils module - this also simplified the test for it. yay
- Loading branch information
1 parent
5d10741
commit 0589aa2
Showing
5 changed files
with
418 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module ActiveModel::Serializer::Utils | ||
module_function | ||
|
||
# converts the include hash to a standard format | ||
# for constrained serialization recursion | ||
# | ||
# converts | ||
# [:author, :comments => [:author]] to | ||
# {:author => [], :comments => [:author]} | ||
# | ||
# and | ||
# [:author, :comments => {:author => :bio}, :posts => [:comments]] to | ||
# {:author => [], :comments => {:author => :bio}, :posts => [:comments]} | ||
# | ||
# The data passed in to this method should be an array where the last | ||
# parameter is a hash | ||
# | ||
# the point of this method is to normalize the include | ||
# options for the child relationships. | ||
# if a sub inclusion is still an array after this method, | ||
# it will get converted during the next iteration | ||
def include_array_to_hash(include_array) | ||
# still don't trust input | ||
# but this also allows | ||
# include: :author syntax | ||
include_array = Array[*include_array].compact | ||
|
||
result = {} | ||
|
||
hashes = include_array.select{|a| a.is_a?(Hash)} | ||
non_hashes = include_array - hashes | ||
|
||
hashes += non_hashes.map{ |association_name| { association_name => [] } } | ||
|
||
# now merge all the hashes | ||
hashes.each{|hash| result.merge!(hash) } | ||
|
||
result | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.