-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Attribute filtering for 0.10.x #1141
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 |
---|---|---|
|
@@ -143,6 +143,10 @@ def json_key | |
root || object.class.model_name.to_s.underscore | ||
end | ||
|
||
def include_attributes(included) | ||
included | ||
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'd like to narrow the Serializer interface rather than broaden it. Every method we add has the potential to interfere with an attribute on the model. Also, with respect to this change, couldn't you just use the key option? class FooSerializer < AM::Serializer
attribute :name, key: title
end FooSerializer.new(foo).attributes
FooSerializer._attributes_keys
FooSerializer._attributes |
||
|
||
def attributes(options = {}) | ||
attributes = | ||
if options[:fields] | ||
|
@@ -151,7 +155,7 @@ def attributes(options = {}) | |
self.class._attributes.dup | ||
end | ||
|
||
attributes.each_with_object({}) do |name, hash| | ||
include_attributes(attributes).each_with_object({}) do |name, hash| | ||
unless self.class._fragmented | ||
hash[name] = send(name) | ||
else | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,31 @@ def test_attributes_definition | |
@profile_serializer.class._attributes) | ||
end | ||
|
||
def test_attributes | ||
assert_equal({ name: 'Name 1', description: 'Description 1' }, | ||
@profile_serializer.attributes) | ||
end | ||
|
||
def test_attributes_with_fields_option | ||
assert_equal({ name: 'Name 1' }, | ||
@profile_serializer.attributes(fields: [:name])) | ||
end | ||
|
||
def test_attributes_with_fields_method | ||
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. could you add a couple more tests with a couple more scenarios? 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. like, what happens when there are no fields on the serializer?, etc 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. Added test cases to cover no overridden fields and combination of fields method and option. |
||
@profile_serializer.define_singleton_method(:include_attributes) do |included| | ||
included - [:name] | ||
end | ||
assert_equal({ description: 'Description 1' }, | ||
@profile_serializer.attributes) | ||
end | ||
|
||
def test_attributes_with_fields_option_and_method | ||
@profile_serializer.define_singleton_method(:include_attributes) do |included| | ||
included - [:name] | ||
end | ||
assert_equal({}, @profile_serializer.attributes(fields: [: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. You should pretty much never be using AMS this way 👎 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. Also, as I wrote earlier, #1141 (comment) fields is already available to the adapter and so this PR will only work for people getting attributes off the serializer directly which is not something this library, IMHO, supports as a primary API (outside of testing the serializer gives you what you expect). |
||
end | ||
|
||
def test_attributes_inheritance_definition | ||
assert_equal([:id, :body], @serializer_klass._attributes) | ||
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.
Isn't this a lot of work just to be able to
I think the better approach would be just OO
I really just don't think this is a problem that needs to be solved in AMS
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 pretty 👎 on this till I'm convinced it's necessary, helpful, domain-appropriate, and doesn't introduce unnecessary complexity to AMS
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.
The PR is different as it allows serializer to skip the computation of the attributes. Overriding
attributes
does not work when you're concerned about performance.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.
With the OO approach, how could that be easily used with
has_many
? Is it possible to change the serializer for relationships dynamically?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.
Example plz