Create DefaultSerializer so that as_json uses same interface.

This is to ensure that PORO's as_json is called if no serializer
is specified.

Original behaviour was that serializable_hash was being called,
overriding the as_json method.
This commit is contained in:
Adam Stanton
2013-02-10 09:06:24 -08:00
parent 3e9b366a45
commit 8f6218c587
4 changed files with 13 additions and 4 deletions

View File

@@ -1,6 +1,15 @@
require "active_support/core_ext/class/attribute"
module ActiveModel
class DefaultSerializer
attr_reader :object
def initialize(object)
@object = object
end
def serializable_hash
@object.as_json
end
end
# Active Model Array Serializer
#
# It serializes an Array, checking if each element that implements
@@ -30,7 +39,7 @@ module ActiveModel
if serializer
serializer.new(item, @options)
else
item
DefaultSerializer.new(item)
end
end
end