-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add optional fields support for associations #1284
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,72 @@ def test_include_option | |
|
||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_fields_option | ||
serializer = ArraySerializer.new([@first_post, @second_post]) | ||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) | ||
actual = adapter.serializable_hash(fields: [:id]) | ||
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. Looking good
Please don't test passing options into Which is to say, this isn't testing how the library is used 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. (that would be a better test usage in the integration test, where the options to @vasilakisfil Are you still interested in this? |
||
|
||
expected = { posts: [{ | ||
id: 1, | ||
comments: [], | ||
author: { | ||
id: 1, | ||
name: 'Steve K.' | ||
}, | ||
blog: { | ||
id: 999, | ||
name: 'Custom blog' | ||
} | ||
}, { | ||
id: 2, | ||
comments: [], | ||
author: { | ||
id: 1, | ||
name: 'Steve K.' | ||
}, | ||
blog: { | ||
id: 999, | ||
name: 'Custom blog' | ||
} | ||
}] } | ||
|
||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_fields_with_no_associations_include_option | ||
serializer = ArraySerializer.new([@first_post, @second_post]) | ||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, include: []) | ||
actual = adapter.serializable_hash(fields: [:id]) | ||
|
||
expected = { posts: [{ | ||
id: 1 | ||
}, { | ||
id: 2 | ||
}] } | ||
|
||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_fields_with_associations_fields_option | ||
serializer = ArraySerializer.new([@first_post, @second_post]) | ||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, include: [:author]) | ||
actual = adapter.serializable_hash(fields: [:id, author: [:name]]) | ||
|
||
expected = { posts: [{ | ||
id: 1, | ||
author: { | ||
name: 'Steve K.' | ||
} | ||
}, { | ||
id: 2, | ||
author: { | ||
name: 'Steve K.' | ||
} | ||
}] } | ||
|
||
assert_equal(expected, actual) | ||
end | ||
end | ||
end | ||
end | ||
|
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.
If you do that, not providing the
fields
option to the adapter will result in no attribute being serialized.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.
not the case since this runs only for associations. The case you said would only happen with the following (given that there is a user association):
Plus, test would cry out if that was true.
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.
Sorry that was an oversight.