diff options
author | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 17:01:08 +0200 |
---|---|---|
committer | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 18:40:38 +0200 |
commit | 22c322f056f42d95b0421e6608f404134463de13 (patch) | |
tree | c35a15a73d606750a8ac57f8f18c330bbb1e5e2b /activemodel/test | |
parent | 776da539d72c98d077f97789a1265dd23a79711f (diff) | |
download | rails-22c322f056f42d95b0421e6608f404134463de13.tar.gz rails-22c322f056f42d95b0421e6608f404134463de13.tar.bz2 rails-22c322f056f42d95b0421e6608f404134463de13.zip |
Add support for overriding associations, mostly used for authorization
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/serializer_test.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 37a675bc20..08dd73e03b 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -174,4 +174,30 @@ class SerializerTest < ActiveModel::TestCase } }, json) end + + def test_overridden_associations + author_serializer = Class.new(ActiveModel::Serializer) do + attributes :first_name + end + + blog_serializer = Class.new(ActiveModel::Serializer) do + const_set(:PersonSerializer, author_serializer) + has_one :person + + def person + object.author + end + end + + user = User.new + blog = Blog.new + blog.author = user + + json = blog_serializer.new(blog, user).as_json + assert_equal({ + :person => { + :first_name => "Jose" + } + }, json) + end end |