Implement embed in root

This commit is contained in:
Santiago Pastorino
2013-08-28 13:48:25 -03:00
parent af34adc7b5
commit 0e0341effc
5 changed files with 79 additions and 53 deletions

View File

@@ -3,11 +3,19 @@ module ActiveModel
def as_json(options={})
if root = options[:root] || self.root
hash = { root.to_s => serializable_object }
hash[meta_key.to_s] = meta if meta
hash.merge!(serializable_data)
hash
else
serializable_object
end
end
def serializable_data
if respond_to?(:meta) && meta
{ meta_key.to_s => meta }
else
{}
end
end
end
end

View File

@@ -70,15 +70,20 @@ module ActiveModel
def initialize(object, options={})
@object = object
@scope = options[:scope]
@root = options[:root] || self.class._root
@root = self.class.root_name if @root == true
self.root = options[:root]
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
end
attr_accessor :object, :scope, :root, :meta_key, :meta
attr_accessor :object, :scope, :meta_key, :meta
attr_reader :root
alias read_attribute_for_serialization send
def root=(root)
@root = root || self.class._root
@root = self.class.root_name if auto_assign_root?
end
def attributes
self.class._attributes.each_with_object({}) do |name, hash|
hash[name] = send(name)
@@ -89,8 +94,19 @@ module ActiveModel
self.class._associations.each_with_object({}) do |association, hash|
if association.embed_ids?
hash[association.key] = serialize_ids association
elsif association.embed_objects?
hash[association.embedded_key] = serialize association
end
if association.embed_objects?
end
end
def serializable_data
embedded_in_root_associations.merge!(super)
end
def embedded_in_root_associations
self.class._associations.each_with_object({}) do |association, hash|
if association.embed_in_root?
hash[association.embedded_key] = serialize association
end
end
@@ -120,5 +136,11 @@ module ActiveModel
hash.merge! associations
end
alias serializable_object serializable_hash
private
def auto_assign_root?
@root == true || !@root && self.class._associations.any? { |a| a.embed_in_root? }
end
end
end

View File

@@ -7,20 +7,20 @@ module ActiveModel
@name = name
@options = options
self.embed = options[:embed]
@embed_key = options[:embed_key] || :id
@include = options[:include]
@key = options[:key]
@embedded_key = options[:root]
self.embed = options[:embed]
@embed_in_root = @embed_ids && options[:include]
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root]
self.serializer_class = @options[:serializer]
end
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :serializer_class, :options
attr_accessor :include, :key, :embedded_key
attr_reader :name, :embed_ids, :embed_objects, :serializer_class
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :options
alias embed_ids? embed_ids
alias embed_objects? embed_objects
alias include? include
alias embed_in_root? embed_in_root
def serializer_class=(serializer)
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
@@ -31,10 +31,6 @@ module ActiveModel
@embed_objects = embed == :object || embed == :objects
end
def embed_objects?
@embed_objects || @embed_ids && @include
end
def build_serializer(object)
@serializer_class ||= Serializer.serializer_for(object)