Skip to content

Commit

Permalink
Merge pull request #813 from jtomaszewski/feature/custom-serializer-c…
Browse files Browse the repository at this point in the history
…lass

Allow to define custom serializer for given class
  • Loading branch information
kurko committed Mar 9, 2015
2 parents 1a7395b + d8b78a3 commit 55210ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/active_model/serializer.rb
Expand Up @@ -56,7 +56,9 @@ def format_keys(format)
attr_reader :key_format

def 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)
if Object.constants.include?(:ArraySerializer)
::ArraySerializer
else
Expand Down
17 changes: 17 additions & 0 deletions test/unit/active_model/array_serializer/serialization_test.rb
Expand Up @@ -35,6 +35,23 @@ def test_serializer_for_array_returns_appropriate_type
end
end

class CustomSerializerClassTest < Minitest::Test
def setup
Object.const_set(:CustomSerializer, Class.new)
end

def teardown
Object.send(:remove_const, :CustomSerializer)
end

def test_serializer_for_array_returns_appropriate_type
object = {}
def object.serializer_class; CustomSerializer; end

assert_equal CustomSerializer, Serializer.serializer_for(object)
end
end

class ModelSerializationTest < Minitest::Test
def test_array_serializer_serializes_models
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Expand Down

0 comments on commit 55210ab

Please sign in to comment.