-
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
Add support for nested serializers. #1157
Changes from 1 commit
69b26d2
8dfef17
6bcb84b
4b50f01
dd580cb
b3c12ec
61035bc
3f00e79
273f0a7
ef6577d
508ba34
597ae65
3ef1149
71816a5
db24432
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,6 @@ | ||
module ActiveModel::Serializer::Utils | ||
module_function | ||
|
||
# Translates a comma separated list of dot separated paths (JSONAPI format) into a Hash. | ||
# Example: `'posts.author, posts.comments.upvotes, posts.comments.author'` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`. | ||
# | ||
# @param [String] included | ||
# @return [Hash] a Hash representing the same tree structure | ||
def include_string_to_hash(included) | ||
included.delete(' ').split(',').inject({}) do |hash, path| | ||
hash.deep_merge!(path.split('.').reverse_each.inject({}) { |a, e| { e.to_sym => a } }) | ||
end | ||
end | ||
|
||
# Translates the arguments passed to the include option into a Hash. The format can be either | ||
# a String (see #include_string_to_hash), an Array of Symbols and Hashes, or a mix of both. | ||
# Example: `posts: [:author, comments: [:author, :upvotes]]` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`. | ||
# | ||
# @param [Symbol, Hash, Array, String] included | ||
# @return [Hash] a Hash representing the same tree structure | ||
def include_args_to_hash(included) | ||
case included | ||
when Symbol | ||
{ included => {} } | ||
when Hash | ||
included.each_with_object({}) { |(key, value), hash| hash[key] = include_args_to_hash(value) } | ||
when Array | ||
included.inject({}) { |a, e| a.merge!(include_args_to_hash(e)) } | ||
when String | ||
include_string_to_hash(included) | ||
else | ||
{} | ||
end | ||
end | ||
|
||
def nested_lookup_paths(klass) | ||
paths = klass.to_s.split('::').reverse.reduce([]) { |a, e| a + [[e] + Array(a.last)] }.reverse | ||
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. Would you mind helping me read this code?
Is that right? 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. It is indeed. I admit that part was slightly cryptic. 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. So that as the test says
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. Yessir. 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. And 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 believe so. 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. So that @share1 = Share.new
TweetSerializer.serializer_for(@share1) #=> TweetSerializer::ShareSerializer
ActiveModel::Serializer.serializer_for(@share1) #=> ShareSerializer 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. Exactly. 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. So that you can build associations on tweet.author using 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. Yes, exactly, if available, and safely fallback to any less specialized serializer that can handle an |
||
paths.map! { |path| "::#{path.join('::')}" } | ||
|
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.
can you add some yardoc?
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.
Done 👍