From 2abb2e617af8e3353d4411a8bd51d03256e0274a Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 19:22:16 +0200 Subject: Add initial support for embed API --- activemodel/lib/active_model/serializer.rb | 22 ++++++-- activemodel/test/cases/serializer_test.rb | 86 ++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/activemodel/lib/active_model/serializer.rb b/activemodel/lib/active_model/serializer.rb index 98801ae633..6d0746a3e8 100644 --- a/activemodel/lib/active_model/serializer.rb +++ b/activemodel/lib/active_model/serializer.rb @@ -21,7 +21,7 @@ module ActiveModel def serialize_ids(collection, scope) # use named scopes if they are present - return collection.ids if collection.respond_to?(:ids) + #return collection.ids if collection.respond_to?(:ids) collection.map do |item| item.read_attribute_for_serialization(:id) @@ -47,6 +47,9 @@ module ActiveModel self._associations = [] class_attribute :_root + class_attribute :_embed + self._embed = :objects + class_attribute :_root_embed class << self def attributes(*attrs) @@ -73,6 +76,11 @@ module ActiveModel associate(Associations::HasOne, attrs) end + def embed(type, options={}) + self._embed = type + self._root_embed = true if options[:include] + end + def root(name) self._root = name end @@ -97,14 +105,22 @@ module ActiveModel def as_json(*) if _root - { _root => serializable_hash } + hash = { _root => serializable_hash } + hash.merge!(associations) if _root_embed + hash else serializable_hash end end def serializable_hash - attributes.merge(associations) + if _embed == :ids + attributes.merge(association_ids) + elsif _embed == :objects + attributes.merge(associations) + else + attributes + end end def associations diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 044c184829..e6a0d0cdcf 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -226,10 +226,12 @@ class SerializerTest < ActiveModel::TestCase attributes :title, :body has_many :comments, :serializer => CommentSerializer - define_method :serializable_hash do - post_hash = attributes - post_hash.merge!(send(type)) - post_hash + if type != :super + define_method :serializable_hash do + post_hash = attributes + post_hash.merge!(send(type)) + post_hash + end end end end @@ -325,4 +327,80 @@ class SerializerTest < ActiveModel::TestCase serializer = Class.new(serializer) assert_equal({ :author => nil }, serializer.new(blog, user).as_json) end + + def test_embed_ids + serializer = post_serializer(:super) + + serializer.class_eval do + root :post + embed :ids + end + + post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") + comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post.comments = comments + + serializer = serializer.new(post, nil) + + assert_equal({ + :post => { + :title => "New Post", + :body => "Body of new post", + :comments => [1, 2] + } + }, serializer.as_json) + end + + def test_embed_ids_include_true + serializer = post_serializer(:super) + + serializer.class_eval do + root :post + embed :ids, :include => true + end + + post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") + comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post.comments = comments + + serializer = serializer.new(post, nil) + + assert_equal({ + :post => { + :title => "New Post", + :body => "Body of new post", + :comments => [1, 2] + }, + :comments => [ + { :title => "Comment1" }, + { :title => "Comment2" } + ] + }, serializer.as_json) + end + + def test_embed_objects + serializer = post_serializer(:super) + + serializer.class_eval do + root :post + embed :objects + end + + post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") + comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post.comments = comments + + serializer = serializer.new(post, nil) + + assert_equal({ + :post => { + :title => "New Post", + :body => "Body of new post", + :comments => [ + { :title => "Comment1" }, + { :title => "Comment2" } + ] + } + }, serializer.as_json) + end end -- cgit v1.2.3