Skip to content

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

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
13 changes: 12 additions & 1 deletion lib/active_model/serializer/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ def serializable_hash_for_single_resource(options)

def resource_relationships(options)
relationships = {}

association_fields_hash = {}
if options[:fields]
association_fields_hash = Hash[
*options[:fields].select { |s| s.is_a? Hash }
]
end

serializer.associations(@include_tree).each do |association|
relationships[association.key] = relationship_value_for(association, options)
relationships[association.key] = relationship_value_for(
association,
options.merge(fields: association_fields_hash[association.name])
Copy link
Contributor

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.

Copy link
Contributor Author

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):

render json: @videos.limit(1), each_serializer: VideoSerializer,  fields: [:id, user: []]

Plus, test would cry out if that was true.

Copy link
Contributor

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.

)
end

relationships
Expand Down
66 changes: 66 additions & 0 deletions test/adapter/json/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Member

Choose a reason for hiding this comment

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

Looking good

  1. this needs an integration (controller) test
  2. Please use ActiveModel::SerializableResource.new([@first_post, @second_post], fields: [:id])

Please don't test passing options into serializable_hash since only the renderer would do that

Which is to say, this isn't testing how the library is used

Copy link
Member

Choose a reason for hiding this comment

The 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 render json: foo, .... are passed to AMS then to the renderer (unless we mutate them))

@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
Expand Down