-
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
Fix root/meta for ArraySerializer #915
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 |
---|---|---|
|
@@ -4,9 +4,10 @@ class ArraySerializer | |
include Enumerable | ||
delegate :each, to: :@objects | ||
|
||
attr_reader :meta, :meta_key | ||
attr_accessor :meta, :meta_key, :root | ||
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's the read to change the reader to an accessor? |
||
|
||
def initialize(objects, options = {}) | ||
@root = options[:root] | ||
options.merge!(root: nil) | ||
|
||
@objects = objects.map do |object| | ||
|
@@ -21,11 +22,11 @@ def initialize(objects, options = {}) | |
end | ||
|
||
def json_key | ||
@objects.first.json_key if @objects.first | ||
end | ||
|
||
def root=(root) | ||
@objects.first.root = root if @objects.first | ||
if root == true && @objects.first | ||
@objects.first.class.root_name | ||
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'm not sure about using the @objects = objects.map do |object|
serializer_class = options.fetch(
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
serializer_class.new(object, options.except(:serializer))
end By checking this code we can presume that the objects might not be instances from the same class. Wrap them by the class name from its first object doesn't would be 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. The problem here is the jsonapi adapter. It sets When all of the elements are the same, it makes sense to autodetect the 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. We could define a default |
||
else | ||
root | ||
end | ||
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.
this logic doesn't make sense to me. why are we only checking if the value of options[:root] is truthy?