Make Serializer reuse Serializable

This commit is contained in:
Santiago Pastorino 2013-05-08 13:02:03 -07:00
parent 0e876624ec
commit 76fead041f
2 changed files with 12 additions and 32 deletions

View File

@ -8,11 +8,11 @@ module ActiveModel
hash[meta_key] = options[:meta] if options.has_key?(:meta) hash[meta_key] = options[:meta] if options.has_key?(:meta)
end end
def as_json(*args) def as_json(args={})
options[:hash] = hash = {} if root = args[:root] || options[:root]
options[:unique_values] = {} options[:hash] = hash = {}
options[:unique_values] = {}
if root = options[:root]
hash.merge!(root => serialize) hash.merge!(root => serialize)
include_meta hash include_meta hash
hash hash

View File

@ -1,3 +1,4 @@
require 'active_model/serializable'
require "active_support/core_ext/class/attribute" require "active_support/core_ext/class/attribute"
require "active_support/core_ext/module/anonymous" require "active_support/core_ext/module/anonymous"
require 'active_support/dependencies' require 'active_support/dependencies'
@ -40,6 +41,8 @@ module ActiveModel
class Serializer class Serializer
extend ActiveSupport::DescendantsTracker extend ActiveSupport::DescendantsTracker
include ActiveModel::Serializable
INCLUDE_METHODS = {} INCLUDE_METHODS = {}
INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" } INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" }
@ -316,37 +319,14 @@ module ActiveModel
@options[:url_options] || {} @options[:url_options] || {}
end end
def meta_key
@options[:meta_key].try(:to_sym) || :meta
end
def include_meta(hash)
hash[meta_key] = @options[:meta] if @options.has_key?(:meta)
end
def to_json(*args)
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
super
end
else
super
end
end
# Returns a json representation of the serializable # Returns a json representation of the serializable
# object including the root. # object including the root.
def as_json(options={}) def as_json(args={})
if root = options.fetch(:root, @options.fetch(:root, root_name)) super(root: args.fetch(:root, options.fetch(:root, root_name)))
@options[:hash] = hash = {} end
@options[:unique_values] = {}
hash.merge!(root => serializable_hash) def serialize
include_meta hash serializable_hash
hash
else
serializable_hash
end
end end
# Returns a hash representation of the serializable # Returns a hash representation of the serializable