Optimize serializer_for for Ruby >= 2.0

This commit is contained in:
Santiago Pastorino 2013-09-16 12:22:23 -03:00
parent 841f3b8181
commit cad8fafa60

View File

@ -22,11 +22,25 @@ module ActiveModel
SETTINGS[:include] = true if options[:include]
end
def serializer_for(resource)
if resource.respond_to?(:to_ary)
ArraySerializer
else
"#{resource.class.name}Serializer".safe_constantize
if RUBY_VERSION >= '2.0'
def serializer_for(resource)
if resource.respond_to?(:to_ary)
ArraySerializer
else
begin
Object.const_get "#{resource.class.name}Serializer"
rescue NameError
nil
end
end
end
else
def serializer_for(resource)
if resource.respond_to?(:to_ary)
ArraySerializer
else
"#{resource.class.name}Serializer".safe_constantize
end
end
end