-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[FIX] Fetch json key from item serializer if empty collection is pass… #1537
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
Changes from all commits
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 |
---|---|---|
|
@@ -10,8 +10,14 @@ class CollectionSerializer | |
def initialize(resources, options = {}) | ||
@root = options[:root] | ||
@object = resources | ||
|
||
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer) | ||
|
||
if resources.blank? && options[:serializer] | ||
@each_serializer = options[:serializer] | ||
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.
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. Or explicit _collection_serializer On Fri, Feb 26, 2016 at 3:43 AM Yohan Robert [email protected]
|
||
end | ||
|
||
@serializers = resources.map do |resource| | ||
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer) | ||
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) } | ||
|
||
if serializer_class.nil? | ||
|
@@ -23,7 +29,7 @@ def initialize(resources, options = {}) | |
end | ||
|
||
def json_key | ||
root || derived_root | ||
root || derived_root || guess_root || default_root | ||
end | ||
|
||
def paginated? | ||
|
@@ -39,8 +45,15 @@ def paginated? | |
private | ||
|
||
def derived_root | ||
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore) | ||
key.try(:pluralize) | ||
serializers.first.try(:json_key).try(:pluralize) | ||
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. aside: we really need to replace 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. Here what about: def derived_root
return unless serializers.empty?
serializers.first.json_key.to_s.pluralize
end Makes it more readable IMO. |
||
end | ||
|
||
def default_root | ||
object.try(:name).try(:underscore).try(:pluralize) | ||
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. What about: def default_root
return unless object.respond_to?(:name)
object.name.to_s.underscore.pluralize
end |
||
end | ||
|
||
def guess_root | ||
@each_serializer.try(:allocate).try(:json_key).try(:pluralize) | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,12 @@ def json_key | |
end | ||
end | ||
|
||
MessagesSerializer = Class.new(ActiveModel::Serializer) do | ||
def json_key | ||
'messages' | ||
end | ||
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. would you mind moving this to the test file? poro's is a little cluttered 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. 👍 |
||
|
||
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do | ||
attribute :id | ||
attribute :name, key: :title | ||
|
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.
I'm thinking this should really be
@root = collection_root(options[:root], options[:serializer])
and something like (assuming root and json_key should behave the same... I don't remember)
aside: all these
try
make my head hurt. ( http://devblog.avdi.org/2015/10/30/activesupports-try-might-not-be-doing-what-you-think-its-doing/ )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.
👍 I also don't really enjoy all these
try
.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.
Right now, the only scenario tested is essentially:
(or a more thorough impl would be something like)
I'm also realizing we're inconsistent with object vs. resources. We should probably always refer to it as object (which is the serializer api for the thing it encapsulates).
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.
@bf4 @NullVoxPopuli @remear I really think that
allocate
is not the right solution here.json_key
by default useroot
or theobject
(https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb#L185). Neither of these would be available and might result in a bug if thejson_key
method is not overidden. That might also break for polymorphic collections.I stay on my position that the prefered solution here would be the ones described at #1536 (comment). Or even subclass the collection serializer and add its own root handling.