diff options
author | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 16:53:22 +0200 |
---|---|---|
committer | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 18:40:38 +0200 |
commit | 776da539d72c98d077f97789a1265dd23a79711f (patch) | |
tree | 763000573de36bd09258ab0ec2f7ccb49d90fa44 /activemodel/test/cases/serializer_test.rb | |
parent | 2a4aaae72af037715db81fda332190df62f3ec44 (diff) | |
download | rails-776da539d72c98d077f97789a1265dd23a79711f.tar.gz rails-776da539d72c98d077f97789a1265dd23a79711f.tar.bz2 rails-776da539d72c98d077f97789a1265dd23a79711f.zip |
Add support for implicit serializers
Diffstat (limited to 'activemodel/test/cases/serializer_test.rb')
-rw-r--r-- | activemodel/test/cases/serializer_test.rb | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 24de81d48f..37a675bc20 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -2,7 +2,7 @@ require "cases/helper" class SerializerTest < ActiveModel::TestCase class Model - def initialize(hash) + def initialize(hash={}) @attributes = hash end @@ -141,6 +141,37 @@ class SerializerTest < ActiveModel::TestCase def test_has_one user = User.new - blog = Blog.new(:author => user) + blog = Blog.new + blog.author = user + + json = BlogSerializer.new(blog, user).as_json + assert_equal({ + :author => { + :first_name => "Jose", + :last_name => "Valim" + } + }, json) + end + + def test_implicit_serializer + author_serializer = Class.new(ActiveModel::Serializer) do + attributes :first_name + end + + blog_serializer = Class.new(ActiveModel::Serializer) do + const_set(:AuthorSerializer, author_serializer) + has_one :author + end + + user = User.new + blog = Blog.new + blog.author = user + + json = blog_serializer.new(blog, user).as_json + assert_equal({ + :author => { + :first_name => "Jose" + } + }, json) end end |