-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Serializer attributes options #2148
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
Serializer attributes options #2148
Conversation
…ttributes_options Moved feature contained in deprecated lib/active_model/serializer/attributes.rb to lib/active_model/serializer.rb # Conflicts: # lib/active_model/serializer/attributes.rb
@nicklandgrebe, thanks for your PR! By analyzing the history of the files in this pull request, we identified @bf4, @groyoh and @beauby to be potential reviewers. |
Hi @nicklandgrebe I opened #2145 a couple days ago about the same issue. I think the extract options is good. If you could take the documentation update here : https://github.com/rails-api/active_model_serializers/pull/2145/files#diff-683b15b4d7c82f436aaeada1733d9eec and I'll close my PR so this can get merged upstream ASAP! :) |
@charlesvallieres Awesome, thanks so much for adding that useful doc |
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've never really followed the need for this feature, but the implementation is fine. Just missing a test for the prohibited keys scenario.
lib/active_model/serializer.rb
Outdated
@@ -199,10 +199,12 @@ def self._attributes | |||
# class AdminAuthorSerializer < ActiveModel::Serializer | |||
# attributes :id, :name, :recent_edits | |||
def self.attributes(*attrs) | |||
options = attrs.extract_options!.except(:key) |
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.
Nice catch with key. would you mind adding a test?
lib/active_model/serializer.rb
Outdated
@@ -199,10 +199,12 @@ def self._attributes | |||
# class AdminAuthorSerializer < ActiveModel::Serializer | |||
# attributes :id, :name, :recent_edits | |||
def self.attributes(*attrs) |
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.
Why not def self.attributes(*attrs, **options)
?
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.
That works! The things you learn. Thanks
@bf4 The original use case of this feature was to be able to use one serializer in two different contexts of our API, separated between being queries made in a private environment (our own backend) vs. a public environment (a third party user). For example, a
|
Please post an update on this PR, not being able to use conditions on attributes makes life hard right now. |
Hello, this would still be useful to us. Any reason why it has been closed? Thanks |
Bump 🙏 |
@bf4 Can this be followed up on? Looks like the changes were approved, but for some reason this was closed. |
@bf4 Hi, I saw that you just released a new version, can you add this also in the next version ? Thanks for your work ! |
@bf4 @nicklandgrebe @beauby was there ever any update on this? |
This was approved, but then closed? What happened? |
Purpose
Add options to
Serializer.attributes
so we can apply conditions to multiple attributes at once.Changes
options
hash from last arg ofSerializer.attributes
usingActiveSupport::Array#extract_options!
options[:key]
options
to the subsequenteach
call toattribute
=>attribute(attr, options)
Caveats
Related GitHub issues
Old PR: #1714
Additional helpful information
Resubmitted against 0-10-stable