-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b674747
Allow attribute options on multiple attributes
nicklandgrebe 6c4b9cd
Pass in empty hash if options is nil
nicklandgrebe 0303bf0
Add test for options passed down from attributes to individual attribute
nicklandgrebe 1811389
Exclude option keys that don't work as intended
nicklandgrebe f2b52de
Switch test to multiple conditional attributes
nicklandgrebe f276b48
Disable rubocop Metrics/AbcSize check
nicklandgrebe 349d136
Update CHANGELOG
nicklandgrebe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,48 @@ def test_multiple_calls_with_the_same_attribute | |
|
||
assert_equal([:id, :title, :body], serializer_class._attributes) | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have a test with multiple attributes and an |
||
# 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?