Skip to content
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 @@ -6,6 +6,7 @@ Breaking changes:

Fixes:

- [#1974](https://github.com/rails-api/active_model_serializers/pull/1974) Support namespace option in collections (@groyoh, @NullVoxPopuli)
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def serializers_from_resources
end

def serializer_from_resource(resource, serializer_context_class, options)
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource, options) }

if serializer_class.nil?
ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}"
Expand Down
20 changes: 20 additions & 0 deletions test/action_controller/namespace_lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def implicit_namespaced_serializer
render json: book
end

def implicit_namespaced_collection_serializer
writer = Writer.new(name: 'Bob')
book = Book.new(title: 'New Post', body: 'Body', writer: writer)
book2 = Book.new(title: 'New Post2', body: 'Body', writer: writer)

render json: [book, book2]
end

def explicit_namespace_as_module
book = Book.new(title: 'New Post', body: 'Body')

Expand Down Expand Up @@ -90,6 +98,18 @@ def namespace_set_in_before_filter
assert_equal expected, actual
end

test 'implicitly uses namespaced serializer for collection' do
get :implicit_namespaced_collection_serializer

expected = [
{ 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } },
{ 'title' => 'New Post2', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } }
]
actual = JSON.parse(@response.body)

assert_equal expected, actual
end

test 'explicit namespace as module' do
get :explicit_namespace_as_module

Expand Down