Skip to content

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

Conversation

nicklandgrebe
Copy link

Purpose

Add options to Serializer.attributes so we can apply conditions to multiple attributes at once.

Changes

  • Extract options hash from last arg of Serializer.attributes using ActiveSupport::Array#extract_options!
    • Exclude options[:key]
  • Pass in options to the subsequent each call to attribute => attribute(attr, options)

Caveats

Related GitHub issues

Old PR: #1714

Additional helpful information

Resubmitted against 0-10-stable

@mention-bot
Copy link

@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.

@charlesvallieres
Copy link

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! :)

@nicklandgrebe
Copy link
Author

@charlesvallieres Awesome, thanks so much for adding that useful doc

Copy link
Member

@bf4 bf4 left a 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.

@@ -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)
Copy link
Member

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?

@@ -199,10 +199,12 @@ def self._attributes
# class AdminAuthorSerializer < ActiveModel::Serializer
# attributes :id, :name, :recent_edits
def self.attributes(*attrs)
Copy link
Contributor

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)?

Copy link
Author

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

@nicklandgrebe
Copy link
Author

@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 Product:

class ProductSerializer < ActiveModel::Serializer
  attributes :price, :number_available

  attributes :number_sold, :other_private_information, unless: :public?

  def public?
    # does the scope say we are in a public API environment?
  end
end

@maxKimoby
Copy link

Please post an update on this PR, not being able to use conditions on attributes makes life hard right now.

@bf4 bf4 closed this Nov 19, 2017
@nerfologist
Copy link

Hello, this would still be useful to us. Any reason why it has been closed?

Thanks

@tfwright
Copy link

Bump 🙏

@hashwin
Copy link

hashwin commented Oct 5, 2018

@bf4 Can this be followed up on? Looks like the changes were approved, but for some reason this was closed.

@btrd
Copy link

btrd commented May 14, 2019

@bf4 Hi, I saw that you just released a new version, can you add this also in the next version ?

Thanks for your work !

@nakwa
Copy link

nakwa commented May 19, 2020

@bf4 @nicklandgrebe @beauby was there ever any update on this?
I have this "temporarily" patched in my code for the last 4 years ... 😆

@sergioisidoro
Copy link

This was approved, but then closed? What happened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.