Skip to content

Allow options on multiple attributes at once #1714

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Features:
- [#1687](https://github.com/rails-api/active_model_serializers/pull/1687) Only calculate `_cache_digest` (in `cache_key`) when `skip_digest` is false. (@bf4)
- [#1647](https://github.com/rails-api/active_model_serializers/pull/1647) Restrict usage of `serializable_hash` options
to the ActiveModel::Serialization and ActiveModel::Serializers::JSON interface. (@bf4)
- [#1714](https://github.com/rails-api/active_model_serializers/pull/1714) Add options to multiple attributes at once. (@nicklandgrebe)

Fixes:
- [#1700](https://github.com/rails-api/active_model_serializers/pull/1700) Support pagination link for Kaminari when no data is returned. (@iamnader)
Expand Down
5 changes: 4 additions & 1 deletion lib/active_model/serializer/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

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?


attrs = attrs.first if attrs.first.class == Array

attrs.each do |attr|
attribute(attr)
attribute(attr, options)
end
end

Expand Down
42 changes: 42 additions & 0 deletions test/serializers/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@ def test_multiple_calls_with_the_same_attribute

assert_equal([:id, :title, :body], serializer_class._attributes)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have a test with multiple attributes and an if/unless option since this is the intended use case.

# rubocop:disable Metrics/AbcSize
def test_multiple_conditional_attributes
model = ::Model.new(true: true, false: false)

scenarios = [
{ options: { if: :true }, included: true },
{ options: { if: :false }, included: false },
{ options: { unless: :false }, included: true },
{ options: { unless: :true }, included: false },
{ options: { if: 'object.true' }, included: true },
{ options: { if: 'object.false' }, included: false },
{ options: { unless: 'object.false' }, included: true },
{ options: { unless: 'object.true' }, included: false },
{ options: { if: -> { object.true } }, included: true },
{ options: { if: -> { object.false } }, included: false },
{ options: { unless: -> { object.false } }, included: true },
{ options: { unless: -> { object.true } }, included: false },
{ options: { if: -> (s) { s.object.true } }, included: true },
{ options: { if: -> (s) { s.object.false } }, included: false },
{ options: { unless: -> (s) { s.object.false } }, included: true },
{ options: { unless: -> (s) { s.object.true } }, included: false }
]

scenarios.each do |s|
serializer = Class.new(ActiveModel::Serializer) do
attributes :attribute1, :attribute2, s[:options]

def true
true
end

def false
false
end
end

hash = serializable(model, serializer: serializer).serializable_hash
assert_equal(s[:included], hash.key?(:attribute1), "Error with #{s[:options]}")
assert_equal(s[:included], hash.key?(:attribute2), "Error with #{s[:options]}")
end
end
end
end
end