Skip to content

Check superclasses for Serializers #681

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

Merged
merged 1 commit into from
Oct 14, 2014
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
17 changes: 15 additions & 2 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def self.serializer_for(resource)
if resource.respond_to?(:to_ary)
config.array_serializer
else
serializer_class = "#{resource.class.name}Serializer"
serializer_class.safe_constantize
get_serializer_for(resource.class)
end
end

Expand Down Expand Up @@ -110,5 +109,19 @@ def each_association(&block)
end
end
end

private

def self.get_serializer_for(klass)
serializer_class_name = "#{klass.name}Serializer"
serializer_class = serializer_class_name.safe_constantize

if serializer_class
serializer_class
elsif klass.superclass
get_serializer_for(klass.superclass)
end
end

end
end
9 changes: 9 additions & 0 deletions test/serializers/serializer_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ def test_overwritten_serializer_for_array
end

class SerializerTest < Minitest::Test
class MyProfile < Profile
end

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

Expand All @@ -41,6 +45,11 @@ def test_serializer_for_not_existing_serializer
serializer = ActiveModel::Serializer.serializer_for(@model)
assert_equal nil, serializer
end

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