Skip to content

Commit

Permalink
with ember :ids, :include => true, has_one
Browse files Browse the repository at this point in the history
associations should include plural versions at
the root, not singular ones.
  • Loading branch information
wycats committed Jan 10, 2012
1 parent 1470e3a commit 4965558
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
29 changes: 28 additions & 1 deletion lib/active_model/serializer.rb
Expand Up @@ -105,6 +105,8 @@ def serialize(collection, scope, context, options)
{ key => array }
end

alias serialize_many serialize

def serialize_ids(collection, scope)
# Use pluck or select_columns if available
# return collection.ids if collection.respond_to?(:ids)
Expand All @@ -130,6 +132,20 @@ def serialize(object, scope, context, options)
end
end

def serialize_many(object, scope, context, options)
if polymorphic?
if object
find_serializable(object, scope, context, options).as_json(:root => polymorphic_key(object))
else
{}
end
else
key = self.key.to_s.pluralize.to_sym
value = object && find_serializable(object, scope, context, options).as_json(:root => false)
value = value ? [value] : []
{ key => value }
end
end

def serialize_ids(object, scope)
if polymorphic? && object
Expand Down Expand Up @@ -312,7 +328,7 @@ def as_json(options=nil)
# object without the root.
def serializable_hash
if _embed == :ids
merge_associations(@hash, associations) if _root_embed
merge_associations(@hash, plural_associations) if _root_embed
attributes.merge(association_ids)
elsif _embed == :objects
attributes.merge(associations)
Expand Down Expand Up @@ -346,6 +362,17 @@ def associations
hash
end

def plural_associations
hash = {}

_associations.each do |association|
associated_object = send(association.name)
hash.merge! association.serialize_many(associated_object, scope, self, :hash => @hash)
end

hash
end

# Returns a hash representation of the serializable
# object associations ids.
def association_ids
Expand Down
47 changes: 37 additions & 10 deletions test/serializer_test.rb
Expand Up @@ -37,9 +37,10 @@ class Post < Model
def initialize(attributes)
super(attributes)
self.comments ||= []
self.author = nil
end

attr_accessor :comments
attr_accessor :comments, :author
def active_model_serializer; PostSerializer; end
end

Expand Down Expand Up @@ -223,6 +224,7 @@ def post_serializer(type)
Class.new(ActiveModel::Serializer) do
attributes :title, :body
has_many :comments, :serializer => CommentSerializer
has_one :author, :serializer => DefaultUserSerializer

if type != :super
define_method :serializable_hash do
Expand All @@ -244,6 +246,7 @@ def test_associations
assert_equal({
:title => "New Post",
:body => "Body of new post",
:author => nil,
:comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
Expand All @@ -256,7 +259,7 @@ def test_association_ids

serializer.class_eval do
def as_json(*)
{ :post => serializable_hash }.merge(associations)
{ :post => serializable_hash }.merge(plural_associations)
end
end

Expand All @@ -270,12 +273,14 @@ def as_json(*)
:post => {
:title => "New Post",
:body => "Body of new post",
:comments => [1, 2]
:comments => [1, 2],
:author => nil
},
:comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
]
],
:authors => []
}, serializer.as_json)
end

Expand Down Expand Up @@ -344,15 +349,16 @@ def test_embed_ids
:post => {
:title => "New Post",
:body => "Body of new post",
:comments => [1, 2]
:comments => [1, 2],
:author => nil
}
}, serializer.as_json)
end

def test_embed_ids_include_true
serializer = post_serializer(:super)
serializer_class = post_serializer(:super)

serializer.class_eval do
serializer_class.class_eval do
root :post
embed :ids, :include => true
end
Expand All @@ -361,18 +367,38 @@ def test_embed_ids_include_true
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments

serializer = serializer.new(post, nil)
serializer = serializer_class.new(post, nil)

assert_equal({
:post => {
:title => "New Post",
:body => "Body of new post",
:comments => [1, 2]
:comments => [1, 2],
:author => nil
},
:comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
]
],
:authors => []
}, serializer.as_json)

post.author = User.new(:id => 1)

serializer = serializer_class.new(post, nil)

assert_equal({
:post => {
:title => "New Post",
:body => "Body of new post",
:comments => [1, 2],
:author => 1
},
:comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
],
:authors => [{ :first_name => "Jose", :last_name => "Valim" }]
}, serializer.as_json)
end

Expand All @@ -394,6 +420,7 @@ def test_embed_objects
:post => {
:title => "New Post",
:body => "Body of new post",
:author => nil,
:comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
Expand Down

0 comments on commit 4965558

Please sign in to comment.