rails-api / active_model_serializers

ActiveModel::Serializer implementation and Rails hooks
MIT License
5.33k stars 1.39k forks source link

Conditional for attributes in plural not working #1885

Open ehannes opened 8 years ago

ehannes commented 8 years ago

Expected behavior vs actual behavior

I expect conditionals for attribute and attributes to behave the same, but they don't. An example:

class UserSerializer < ActiveModel::Serializer
  # Just some attributes...
  attributes :id

  # NOTE: attributes in plural!
  attributes :secrets, if: :admin_signed_in?

  def admin_signed_in?
    # Some authentication logic...
  end
end

This gives me TypeError ({:if=>"admin_signed_in?"} is not a symbol nor a string):

If I change this it to attribute in singular, it works!

  attribute :secrets, if: :admin_signed_in?

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.0

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-linux]

OS Type & Version: Ubuntu 16.04

Integrated application and version (e.g., Rails, Grape, etc): Rails 4.2.4

bf4 commented 8 years ago

PR's welcome :) Let me know if you'd like any guidance. There are some other similar feature requests that have been posted.

By the way, you should try 0.10.2, esp if you're using caching.

nicklandgrebe commented 8 years ago

I started down this road here https://github.com/rails-api/active_model_serializers/pull/1714

But there was an issue with applying conditionals in multiple attributes definitions. Check out the thread and see if you'd like to pick it up. I was planning on doing so soon, but am short for time at the moment.