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

Allow to define custom serializer for given class #897

Merged
merged 1 commit into from
May 10, 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
4 changes: 3 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def self.urls(*attrs)
end

def self.serializer_for(resource, options = {})
if resource.respond_to?(:to_ary)
if resource.respond_to?(:serializer_class)
resource.serializer_class
elsif resource.respond_to?(:to_ary)
config.array_serializer
else
options
Expand Down
9 changes: 9 additions & 0 deletions test/serializers/serializer_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ def test_overwritten_serializer_for_array
class SerializerTest < Minitest::Test
class MyProfile < Profile
end
class CustomProfile
def serializer_class; ProfileSerializer; end
end

def setup
@profile = Profile.new
@my_profile = MyProfile.new
@custom_profile = CustomProfile.new
@model = ::Model.new
end

Expand All @@ -50,6 +54,11 @@ def test_serializer_inherited_serializer
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
assert_equal ProfileSerializer, serializer
end

def test_serializer_custom_serializer
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
assert_equal ProfileSerializer, serializer
end
end
end
end
Expand Down