If an existing association exists, use it to

get the value if none was provided.
This commit is contained in:
Yehuda Katz 2012-01-11 13:06:12 -07:00
parent dd0b56c748
commit 72b8213bee
2 changed files with 89 additions and 53 deletions

View File

@ -80,6 +80,16 @@ module ActiveModel
options[:key] || name options[:key] || name
end end
def associated_object(serializer)
options[:value] || serializer.send(name)
end
def with_options(options)
config = dup
config.options.merge!(options)
config
end
protected protected
def find_serializable(object, scope, context, options) def find_serializable(object, scope, context, options)
@ -96,18 +106,18 @@ module ActiveModel
class HasMany < Config #:nodoc: class HasMany < Config #:nodoc:
alias plural_key key alias plural_key key
def serialize(collection, scope, context, options) def serialize(serializer, scope)
collection.map do |item| associated_object(serializer).map do |item|
find_serializable(item, scope, context, options).as_json(:root => false) find_serializable(item, scope, serializer, options).as_json(:root => false)
end end
end end
alias serialize_many serialize alias serialize_many serialize
def serialize_ids(collection, scope) def serialize_ids(serializer, scope)
# Use pluck or select_columns if available # Use pluck or select_columns if available
# return collection.ids if collection.respond_to?(:ids) # return collection.ids if collection.respond_to?(:ids)
collection.map do |item| associated_object(serializer).map do |item|
item.read_attribute_for_serialization(:id) item.read_attribute_for_serialization(:id)
end end
end end
@ -118,17 +128,19 @@ module ActiveModel
key.to_s.pluralize.to_sym key.to_s.pluralize.to_sym
end end
def serialize(object, scope, context, options) def serialize(serializer, scope)
object && find_serializable(object, scope, context, options).as_json(:root => false) object = associated_object(serializer)
object && find_serializable(object, scope, serializer, options).as_json(:root => false)
end end
def serialize_many(object, scope, context, options) def serialize_many(serializer, scope)
value = object && find_serializable(object, scope, context, options).as_json(:root => false) object = associated_object(serializer)
value = object && find_serializable(object, scope, serializer, options).as_json(:root => false)
value ? [value] : [] value ? [value] : []
end end
def serialize_ids(object, scope) def serialize_ids(serializer, scope)
if object if object = associated_object(serializer)
object.read_attribute_for_serialization(:id) object.read_attribute_for_serialization(:id)
else else
nil nil
@ -202,8 +214,7 @@ module ActiveModel
# { :name => :string, :age => :integer } # { :name => :string, :age => :integer }
# #
# The +associations+ hash looks like this: # The +associations+ hash looks like this:
# { :posts => { :has_many => :posts } }
# { :posts => { :has_many => :posts } }
# #
# If :key is used: # If :key is used:
# #
@ -307,7 +318,7 @@ module ActiveModel
end end
end end
def include!(key, options={}) def include!(name, options={})
embed = options[:embed] embed = options[:embed]
root_embed = options[:include] root_embed = options[:include]
hash = options[:hash] hash = options[:hash]
@ -316,20 +327,26 @@ module ActiveModel
serializer = options[:serializer] serializer = options[:serializer]
scope = options[:scope] scope = options[:scope]
if value.respond_to?(:to_ary) association = _associations.find do |a|
association = Associations::HasMany.new(key, :serializer => serializer) a.name == name
end
association = association.with_options(options) if association
association ||= if value.respond_to?(:to_ary)
Associations::HasMany.new(name, options)
else else
association = Associations::HasOne.new(key, :serializer => serializer) Associations::HasOne.new(name, options)
end end
if embed == :ids if embed == :ids
node[key] = association.serialize_ids(value, scope) node[association.key] = association.serialize_ids(self, scope)
if root_embed if root_embed
merge_association hash, association.plural_key, association.serialize_many(value, scope, self, {}) merge_association hash, association.plural_key, association.serialize_many(self, scope)
end end
elsif embed == :objects elsif embed == :objects
node[key] = association.serialize(value, scope) node[association.key] = association.serialize(self, scope)
end end
end end
@ -355,8 +372,8 @@ module ActiveModel
hash = {} hash = {}
_associations.each do |association| _associations.each do |association|
associated_object = send(association.name) association = association.with_options(:hash => @hash)
hash[association.key] = association.serialize(associated_object, scope, self, :hash => @hash) hash[association.key] = association.serialize(self, scope)
end end
hash hash
@ -366,8 +383,8 @@ module ActiveModel
hash = {} hash = {}
_associations.each do |association| _associations.each do |association|
associated_object = send(association.name) association = association.with_options(:hash => @hash)
hash[association.plural_key] = association.serialize_many(associated_object, scope, self, :hash => @hash) hash[association.plural_key] = association.serialize_many(self, scope)
end end
hash hash
@ -379,8 +396,7 @@ module ActiveModel
hash = {} hash = {}
_associations.each do |association| _associations.each do |association|
associated_object = send(association.name) hash[association.key] = association.serialize_ids(self, scope)
hash[association.key] = association.serialize_ids(associated_object, scope)
end end
hash hash

View File

@ -49,41 +49,61 @@ class AssociationTest < ActiveModel::TestCase
@root_hash = {} @root_hash = {}
end end
def test_include_bang_has_many_associations def include!(key, options={})
@post_serializer.include! :comments, @post_serializer.include! key, options.merge(
:embed => :ids, :embed => :ids,
:include => true, :include => true,
:hash => @root_hash, :hash => @root_hash,
:node => @hash, :node => @hash,
:value => @post.comments,
:serializer => @comment_serializer_class :serializer => @comment_serializer_class
)
assert_equal({
:comments => [ 1 ]
}, @hash)
assert_equal({
:comments => [
{ :body => "ZOMG A COMMENT" }
]
}, @root_hash)
end end
def test_include_bang_has_one_associations class NoDefaults < AssociationTest
@post_serializer.include! :comment, def test_include_bang_has_many_associations
:embed => :ids, include! :comments, :value => @post.comments
:include => true,
:hash => @root_hash,
:node => @hash,
:value => @post.comment,
:serializer => @comment_serializer_class
assert_equal({ assert_equal({
:comment => 1 :comments => [ 1 ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [{ :body => "ZOMG A COMMENT" }] :comments => [
}, @root_hash) { :body => "ZOMG A COMMENT" }
]
}, @root_hash)
end
def test_include_bang_has_one_associations
include! :comment, :value => @post.comment
assert_equal({
:comment => 1
}, @hash)
assert_equal({
:comments => [{ :body => "ZOMG A COMMENT" }]
}, @root_hash)
end
end
class DefaultsTest < AssociationTest
def test_with_default_has_many
@post_serializer_class.class_eval do
has_many :comments
end
include! :comments
assert_equal({
:comments => [ 1 ]
}, @hash)
assert_equal({
:comments => [
{ :body => "ZOMG A COMMENT" }
]
}, @root_hash)
end
end end
end end