-
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
Allow options on multiple attributes at once #1714
Conversation
Heyo, I added a test for the lines changed. Apologies for not doing this in the first place! Didn't see a case till now. |
@@ -34,10 +34,11 @@ def inherited(base) | |||
# class AdminAuthorSerializer < ActiveModel::Serializer | |||
# attributes :id, :name, :recent_edits | |||
def attributes(*attrs) | |||
options = attrs.pop if attrs.last.class == Hash |
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 guess something like would be better:
+ options = attrs.last.class == Hash ? attrs.pop : {}
+ options = options.except(:key)
At least, we prevent ourselves from having unintended behavior.
Note that the CHANGELOG has changed in master recently with the release of 0.10.0. This PR needs to be updated to move the entry to the new master section. The commits also need to be squashed. |
Hey, @nicklandgrebe.
It returns
Attributes I think the problem is caused at
Only one attribute instance for one key so that the following duplicated attributes would replace the previous one. You did a greate job, and let's make it perfect. |
@zhen639 attributes are not really meant to be (re)defined multiple times (at least that meither tested or implemented AFAIK). I would suggest: class CandidateSerializer < ActiveModel::Serializer
attribute :paper_test_score, if: :in_paper_test?
attribute :homework, if: :in_homework?
attributes %I(university resume), if: :in_paper_test_and_homework?
def in_paper_test_and_homework?
in_paper_test? && in_homework?
end
def in_paper_test?
true
end
def in_home_work?
false
end
end |
@@ -34,10 +34,13 @@ def inherited(base) | |||
# class AdminAuthorSerializer < ActiveModel::Serializer | |||
# attributes :id, :name, :recent_edits | |||
def attributes(*attrs) | |||
options = attrs.last.class == Hash ? attrs.pop : {} | |||
options = 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.
good call. I wonder if this could be supported via activesupport with_options
and no code changes?
This is definitely a scenario ams does not protect against |
@bf4 I think it (AMS) should (protect against attributes redefinition). |
Anything happening with this? i have a workaround but it would be nice :) |
basic workaround is if you are attributes :university, :resume, :paper_test_score, if: :in_paper_test? then you can attribute :university, if: :in_paper_test?
attribute :resume, if: :in_paper_test?
attribute :paper_test_score, if: :in_paper_test? other work around is to use composition, inheritance, or options... |
PR needs to be resubmitted against 0-10-stable. |
Resubmitted as #2148 against 0-10-stable. This totally fell off my radar, thanks for the push! |
Purpose
Add options to
Serializer.attributes
so we can apply conditions to multiple attributes at once.Changes
Check if last item in
attrs
passed intoSerializer.attributes
has conditionclass == Hash
, and if so,pop
it offattrs
, then pass it intoattribute(attr, options)
every timeattrs
is iterated over.Before, only
attribute(attr)
would be called every timeattrs
is iterated over.Caveats
Related GitHub issues
#1403 (comment)
Additional helpful information