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] SETTINGS[:include] = true if options[:include]
end end
def serializer_for(resource) if RUBY_VERSION >= '2.0'
if resource.respond_to?(:to_ary) def serializer_for(resource)
ArraySerializer if resource.respond_to?(:to_ary)
else ArraySerializer
"#{resource.class.name}Serializer".safe_constantize 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
end end