Skip to content
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

Pass fields down from constructor (MERGED) #1287

Closed
wants to merge 1 commit 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 lib/active_model/serializer/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(serializer, options = {})

def serializable_hash(options = nil)
options ||= {}
options[:fields] = instance_options[:fields] if options[:fields].nil?
Copy link
Member

Choose a reason for hiding this comment

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

do you mean options[:fields] ||= instance_options[:fields]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah that's better I will fix it!

Copy link
Member

Choose a reason for hiding this comment

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

@vasilakisfil let's finish this?


if serializer.respond_to?(:each)
serializable_hash_for_collection(options)
Expand Down
14 changes: 14 additions & 0 deletions test/adapter/json/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ def test_include_option

assert_equal(expected, actual)
end

def test_fields_with_no_associations_include_option
actual = ActiveModel::SerializableResource.new(
[@first_post, @second_post], adapter: :json, fields: [:id], include: []
).as_json

expected = { posts: [{
id: 1
}, {
id: 2
}] }

assert_equal(expected, actual)
end
end
end
end
Expand Down