Skip to content

Commit

Permalink
Allow serializer_for to accept String instead of just class objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Physium committed May 5, 2023
1 parent ff1a36f commit 460d4c5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ def build_serializer_class(resource, options)
"".tap do |klass_name|
klass_name << "#{options[:namespace]}::" if options[:namespace]
klass_name << options[:prefix].to_s.classify if options[:prefix]
klass_name << "#{resource.class.name}Serializer"
if resource.is_a?(String)
klass_name << "#{resource}Serializer"
else
klass_name << "#{resource.class.name}Serializer"
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_serializer_for_array_returns_appropriate_type
def object.serializer_class; CustomSerializer; end

assert_equal CustomSerializer, Serializer.serializer_for(object)
assert_equal CustomSerializer, Serializer.serializer_for('Custom')
end
end

Expand Down

0 comments on commit 460d4c5

Please sign in to comment.