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

Fix: Don't pass serializer option to associated serializers #949

Merged
merged 3 commits into from
Jun 13, 2015
Merged
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
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def each_association(&block)
if serializer_class
serializer = serializer_class.new(
association_value,
options.merge(serializer_from_options(association_options))
options.except(:serializer).merge(serializer_from_options(association_options))
Copy link
Member

Choose a reason for hiding this comment

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

This is the simplest thing that worked without doing a bunch of refactoring. (We considered adding a ::new_association_serializer that wraps ::new but it wasn't a quick win.)

)
elsif !association_value.nil? && !association_value.instance_of?(Object)
association_options[:association_options][:virtual_value] = association_value
Expand Down
4 changes: 2 additions & 2 deletions test/action_controller/adapter_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActionController
module Serialization
class AdapterSelectorTest < ActionController::TestCase
class MyController < ActionController::Base
class AdapterSelectorTestController < ActionController::Base
def render_using_default_adapter
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
Expand All @@ -20,7 +20,7 @@ def render_skipping_adapter
end
end

tests MyController
tests AdapterSelectorTestController

def test_render_using_default_adapter
get :render_using_default_adapter
Expand Down
30 changes: 28 additions & 2 deletions test/action_controller/explicit_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActionController
module Serialization
class ExplicitSerializerTest < ActionController::TestCase
class MyController < ActionController::Base
class ExplicitSerializerTestController < ActionController::Base
def render_using_explicit_serializer
@profile = Profile.new(name: 'Name 1',
description: 'Description 1',
Expand Down Expand Up @@ -55,9 +55,16 @@ def render_array_using_explicit_serializer_and_custom_serializers

render json: [@post], each_serializer: PostPreviewSerializer
end

def render_using_explicit_each_serializer
location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
place = Place.new(id: 1337, name: 'Amazing Place', locations: [location])

render json: place, each_serializer: PlaceSerializer
end
end

tests MyController
tests ExplicitSerializerTestController

def test_render_using_explicit_serializer
get :render_using_explicit_serializer
Expand Down Expand Up @@ -105,6 +112,25 @@ def test_render_array_using_explicit_serializer_and_custom_serializers

assert_equal expected.to_json, @response.body
end

def test_render_using_explicit_each_serializer
get :render_using_explicit_each_serializer

expected = {
id: 1337,
name: "Amazing Place",
locations: [
{
id: 42,
lat: "-23.550520",
lng: "-46.633309",
place: "Nowhere" # is a virtual attribute on LocationSerializer
}
]
}

assert_equal expected.to_json, response.body
end
end
end
end
4 changes: 2 additions & 2 deletions test/action_controller/json_api_linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActionController
module Serialization
class JsonApiLinkedTest < ActionController::TestCase
class MyController < ActionController::Base
class JsonApiLinkedTestController < ActionController::Base
def setup_post
ActionController::Base.cache_store.clear
@role1 = Role.new(id: 1, name: 'admin')
Expand Down Expand Up @@ -78,7 +78,7 @@ def render_collection_with_include
end
end

tests MyController
tests JsonApiLinkedTestController

def test_render_resource_without_include
get :render_resource_without_include
Expand Down
4 changes: 2 additions & 2 deletions test/action_controller/rescue_from_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActionController
module Serialization
class RescueFromTest < ActionController::TestCase
class MyController < ActionController::Base
class RescueFromTestController < ActionController::Base
rescue_from Exception, with: :handle_error

def render_using_raise_error_serializer
Expand All @@ -16,7 +16,7 @@ def handle_error(exception)
end
end

tests MyController
tests RescueFromTestController

def test_rescue_from
get :render_using_raise_error_serializer
Expand Down
4 changes: 2 additions & 2 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActionController
module Serialization
class ImplicitSerializerTest < ActionController::TestCase
class MyController < ActionController::Base
class ImplicitSerializationTestController < ActionController::Base
def render_using_implicit_serializer
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
Expand Down Expand Up @@ -152,7 +152,7 @@ def with_adapter(adapter)
end
end

tests MyController
tests ImplicitSerializationTestController

# We just have Null for now, this will change
def test_render_using_implicit_serializer
Expand Down