aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test
diff options
context:
space:
mode:
authorJose and Yehuda <wycats@gmail.com>2011-10-15 18:27:56 +0200
committerJose and Yehuda <wycats@gmail.com>2011-10-15 18:40:38 +0200
commit322f47898e80af3fcdc3cb3db35e177d8216a2d2 (patch)
treee77463224a32635da6e48c24dd8c1ae45b167086 /activemodel/test
parent22c322f056f42d95b0421e6608f404134463de13 (diff)
downloadrails-322f47898e80af3fcdc3cb3db35e177d8216a2d2.tar.gz
rails-322f47898e80af3fcdc3cb3db35e177d8216a2d2.tar.bz2
rails-322f47898e80af3fcdc3cb3db35e177d8216a2d2.zip
Add association_ids
Diffstat (limited to 'activemodel/test')
-rw-r--r--activemodel/test/cases/serializer_test.rb61
1 files changed, 60 insertions, 1 deletions
diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb
index 08dd73e03b..168a77838f 100644
--- a/activemodel/test/cases/serializer_test.rb
+++ b/activemodel/test/cases/serializer_test.rb
@@ -182,11 +182,12 @@ class SerializerTest < ActiveModel::TestCase
blog_serializer = Class.new(ActiveModel::Serializer) do
const_set(:PersonSerializer, author_serializer)
- has_one :person
def person
object.author
end
+
+ has_one :person
end
user = User.new
@@ -200,4 +201,62 @@ class SerializerTest < ActiveModel::TestCase
}
}, json)
end
+
+ def post_serializer(type)
+ Class.new(ActiveModel::Serializer) do
+ attributes :title, :body
+ has_many :comments, :serializer => CommentSerializer
+
+ define_method :serializable_hash do
+ post_hash = attributes
+ post_hash.merge!(send(type))
+ post_hash
+ end
+ end
+ end
+
+ def test_associations
+ post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
+ comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
+ post.comments = comments
+
+ serializer = post_serializer(:associations).new(post, nil)
+
+ assert_equal({
+ :title => "New Post",
+ :body => "Body of new post",
+ :comments => [
+ { :title => "Comment1" },
+ { :title => "Comment2" }
+ ]
+ }, serializer.as_json)
+ end
+
+ def test_association_ids
+ serializer = post_serializer(:association_ids)
+
+ serializer.class_eval do
+ def as_json(*)
+ { post: serializable_hash }.merge(associations)
+ end
+ 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
end