From c3de52d7ed470433e9ecd44226518797c0a9f389 Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Mon, 26 Sep 2011 19:29:37 -0400 Subject: Initial implementation of ActiveModel::Serializer --- activemodel/test/cases/serializer_test.rb | 75 +++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 activemodel/test/cases/serializer_test.rb (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb new file mode 100644 index 0000000000..91ed6987a1 --- /dev/null +++ b/activemodel/test/cases/serializer_test.rb @@ -0,0 +1,75 @@ +require "cases/helper" + +class SerializerTest < ActiveModel::TestCase + class User + attr_accessor :superuser + + def super_user? + @superuser + end + + def read_attribute_for_serialization(name) + hash = { :first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password" } + hash[name] + end + end + + class UserSerializer < ActiveModel::Serializer + attributes :first_name, :last_name + end + + class User2Serializer < ActiveModel::Serializer + attributes :first_name, :last_name + + def serializable_hash(*) + attributes.merge(:ok => true).merge(scope) + end + end + + class MyUserSerializer < ActiveModel::Serializer + attributes :first_name, :last_name + + def serializable_hash(*) + hash = attributes + hash = hash.merge(:super_user => true) if my_user.super_user? + hash + end + end + + def test_attributes + user = User.new + user_serializer = UserSerializer.new(user, nil) + + hash = user_serializer.as_json + + assert_equal({ :first_name => "Jose", :last_name => "Valim" }, hash) + end + + def test_attributes_method + user = User.new + user_serializer = User2Serializer.new(user, {}) + + hash = user_serializer.as_json + + assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true }, hash) + end + + def test_serializer_receives_scope + user = User.new + user_serializer = User2Serializer.new(user, {:scope => true}) + + hash = user_serializer.as_json + + assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true, :scope => true }, hash) + end + + def test_pretty_accessors + user = User.new + user.superuser = true + user_serializer = MyUserSerializer.new(user, nil) + + hash = user_serializer.as_json + + assert_equal({ :first_name => "Jose", :last_name => "Valim", :super_user => true }, hash) + end +end -- cgit v1.2.3 From e407dfb9bf6ee3b12d699511ef05e6f260c5edf1 Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Tue, 27 Sep 2011 17:34:47 -0400 Subject: Don't require serializable_hash to take options. --- activemodel/test/cases/serializer_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 91ed6987a1..1c1da588f7 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -21,7 +21,7 @@ class SerializerTest < ActiveModel::TestCase class User2Serializer < ActiveModel::Serializer attributes :first_name, :last_name - def serializable_hash(*) + def serializable_hash attributes.merge(:ok => true).merge(scope) end end @@ -29,7 +29,7 @@ class SerializerTest < ActiveModel::TestCase class MyUserSerializer < ActiveModel::Serializer attributes :first_name, :last_name - def serializable_hash(*) + def serializable_hash hash = attributes hash = hash.merge(:super_user => true) if my_user.super_user? hash -- cgit v1.2.3 From 2a4aaae72af037715db81fda332190df62f3ec44 Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 16:20:45 +0200 Subject: Added has_one and has_many --- activemodel/test/cases/serializer_test.rb | 81 +++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 1c1da588f7..24de81d48f 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -1,17 +1,33 @@ require "cases/helper" class SerializerTest < ActiveModel::TestCase - class User + class Model + def initialize(hash) + @attributes = hash + end + + def read_attribute_for_serialization(name) + @attributes[name] + end + end + + class User < Model attr_accessor :superuser + def initialize(hash={}) + super hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password") + end + def super_user? @superuser end + end - def read_attribute_for_serialization(name) - hash = { :first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password" } - hash[name] - end + class Post < Model + attr_accessor :comments + end + + class Comment < Model end class UserSerializer < ActiveModel::Serializer @@ -36,6 +52,25 @@ class SerializerTest < ActiveModel::TestCase end end + class CommentSerializer + def initialize(comment, scope) + @comment, @scope = comment, scope + end + + def serializable_hash + { title: @comment.read_attribute_for_serialization(:title) } + end + + def as_json + { comment: serializable_hash } + end + end + + class PostSerializer < ActiveModel::Serializer + attributes :title, :body + has_many :comments, :serializer => CommentSerializer + end + def test_attributes user = User.new user_serializer = UserSerializer.new(user, nil) @@ -72,4 +107,40 @@ class SerializerTest < ActiveModel::TestCase assert_equal({ :first_name => "Jose", :last_name => "Valim", :super_user => true }, hash) end + + def test_has_many + user = User.new + + 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 + + post_serializer = PostSerializer.new(post, user) + + assert_equal({ + :title => "New Post", + :body => "Body of new post", + :comments => [ + { :title => "Comment1" }, + { :title => "Comment2" } + ] + }, post_serializer.as_json) + end + + class Blog < Model + attr_accessor :author + end + + class AuthorSerializer < ActiveModel::Serializer + attributes :first_name, :last_name + end + + class BlogSerializer < ActiveModel::Serializer + has_one :author, :serializer => AuthorSerializer + end + + def test_has_one + user = User.new + blog = Blog.new(:author => user) + end end -- cgit v1.2.3 From 776da539d72c98d077f97789a1265dd23a79711f Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 16:53:22 +0200 Subject: Add support for implicit serializers --- activemodel/test/cases/serializer_test.rb | 35 +++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'activemodel/test/cases') 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 -- cgit v1.2.3 From 22c322f056f42d95b0421e6608f404134463de13 Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 17:01:08 +0200 Subject: Add support for overriding associations, mostly used for authorization --- activemodel/test/cases/serializer_test.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'activemodel/test/cases') 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 -- cgit v1.2.3 From 322f47898e80af3fcdc3cb3db35e177d8216a2d2 Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 18:27:56 +0200 Subject: Add association_ids --- activemodel/test/cases/serializer_test.rb | 61 ++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'activemodel/test/cases') 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 -- cgit v1.2.3 From 7a28498b55913aa0fd1d3529909ab57eaf64af0e Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 18:37:12 +0200 Subject: Fix nil has_one association --- activemodel/test/cases/serializer_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 168a77838f..165c1d2490 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -259,4 +259,22 @@ class SerializerTest < ActiveModel::TestCase ] }, serializer.as_json) end + + def test_associations_with_nil_association + user = User.new + blog = Blog.new + + json = BlogSerializer.new(blog, user).as_json + assert_equal({ + :author => nil + }, json) + + serializer = Class.new(BlogSerializer) do + def serializable_hash + attributes.merge(association_ids) + end + end + + assert_equal({ :author => nil }, serializer.new(blog, user).as_json) + end end -- cgit v1.2.3 From a230f040ff61f069d46a9b86417a8e251016d5db Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 18:54:20 +0200 Subject: Add support for the root attribute --- activemodel/test/cases/serializer_test.rb | 76 +++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 14 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 165c1d2490..044c184829 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -77,7 +77,9 @@ class SerializerTest < ActiveModel::TestCase hash = user_serializer.as_json - assert_equal({ :first_name => "Jose", :last_name => "Valim" }, hash) + assert_equal({ + :user => { :first_name => "Jose", :last_name => "Valim" } + }, hash) end def test_attributes_method @@ -86,7 +88,9 @@ class SerializerTest < ActiveModel::TestCase hash = user_serializer.as_json - assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true }, hash) + assert_equal({ + :user2 => { :first_name => "Jose", :last_name => "Valim", :ok => true } + }, hash) end def test_serializer_receives_scope @@ -95,7 +99,14 @@ class SerializerTest < ActiveModel::TestCase hash = user_serializer.as_json - assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true, :scope => true }, hash) + assert_equal({ + :user2 => { + :first_name => "Jose", + :last_name => "Valim", + :ok => true, + :scope => true + } + }, hash) end def test_pretty_accessors @@ -105,7 +116,11 @@ class SerializerTest < ActiveModel::TestCase hash = user_serializer.as_json - assert_equal({ :first_name => "Jose", :last_name => "Valim", :super_user => true }, hash) + assert_equal({ + :my_user => { + :first_name => "Jose", :last_name => "Valim", :super_user => true + } + }, hash) end def test_has_many @@ -118,12 +133,14 @@ class SerializerTest < ActiveModel::TestCase post_serializer = PostSerializer.new(post, user) assert_equal({ - :title => "New Post", - :body => "Body of new post", - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } - ] + :post => { + :title => "New Post", + :body => "Body of new post", + :comments => [ + { :title => "Comment1" }, + { :title => "Comment2" } + ] + } }, post_serializer.as_json) end @@ -146,9 +163,11 @@ class SerializerTest < ActiveModel::TestCase json = BlogSerializer.new(blog, user).as_json assert_equal({ - :author => { - :first_name => "Jose", - :last_name => "Valim" + :blog => { + :author => { + :first_name => "Jose", + :last_name => "Valim" + } } }, json) end @@ -266,15 +285,44 @@ class SerializerTest < ActiveModel::TestCase json = BlogSerializer.new(blog, user).as_json assert_equal({ - :author => nil + :blog => { :author => nil } }, json) serializer = Class.new(BlogSerializer) do + root :blog + def serializable_hash attributes.merge(association_ids) end end + json = serializer.new(blog, user).as_json + assert_equal({ :blog => { :author => nil } }, json) + end + + def test_custom_root + user = User.new + blog = Blog.new + + serializer = Class.new(BlogSerializer) do + root :my_blog + end + + assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, user).as_json) + end + + def test_false_root + user = User.new + blog = Blog.new + + serializer = Class.new(BlogSerializer) do + root false + end + + assert_equal({ :author => nil }, serializer.new(blog, user).as_json) + + # test inherited false root + serializer = Class.new(serializer) assert_equal({ :author => nil }, serializer.new(blog, user).as_json) end end -- cgit v1.2.3 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/test/cases/serializer_test.rb | 86 +++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) (limited to 'activemodel/test/cases') 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 From afd7140b66e7cb32e1be58d9e44489e6bcbde0dc Mon Sep 17 00:00:00 2001 From: Jose and Yehuda Date: Sat, 15 Oct 2011 22:33:58 +0200 Subject: Remove 1.9 Hash syntax - tests passing on 1.8.7 --- activemodel/test/cases/serializer_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index e6a0d0cdcf..00d519dc1a 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -58,11 +58,11 @@ class SerializerTest < ActiveModel::TestCase end def serializable_hash - { title: @comment.read_attribute_for_serialization(:title) } + { :title => @comment.read_attribute_for_serialization(:title) } end def as_json - { comment: serializable_hash } + { :comment => serializable_hash } end end @@ -258,7 +258,7 @@ class SerializerTest < ActiveModel::TestCase serializer.class_eval do def as_json(*) - { post: serializable_hash }.merge(associations) + { :post => serializable_hash }.merge(associations) end end -- cgit v1.2.3 From 8896b4fdc8a543157cdf4dfc378607ebf6c10ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 23 Nov 2011 23:18:13 +0000 Subject: Implement ArraySerializer and move old serialization API to a new namespace. The following constants were renamed: ActiveModel::Serialization => ActiveModel::Serializable ActiveModel::Serializers::JSON => ActiveModel::Serializable::JSON ActiveModel::Serializers::Xml => ActiveModel::Serializable::XML The main motivation for such a change is that `ActiveModel::Serializers::JSON` was not actually a serializer, but a module that when included allows the target to be serializable to JSON. With such changes, we were able to clean up the namespace to add true serializers as the ArraySerializer. --- activemodel/test/cases/serializable/json_test.rb | 219 +++++++++++++++++++++ activemodel/test/cases/serializable/xml_test.rb | 206 +++++++++++++++++++ activemodel/test/cases/serializable_test.rb | 151 ++++++++++++++ activemodel/test/cases/serialization_test.rb | 151 -------------- activemodel/test/cases/serializer_test.rb | 65 +++++- .../cases/serializers/json_serialization_test.rb | 219 --------------------- .../cases/serializers/xml_serialization_test.rb | 206 ------------------- 7 files changed, 638 insertions(+), 579 deletions(-) create mode 100644 activemodel/test/cases/serializable/json_test.rb create mode 100644 activemodel/test/cases/serializable/xml_test.rb create mode 100644 activemodel/test/cases/serializable_test.rb delete mode 100644 activemodel/test/cases/serialization_test.rb delete mode 100644 activemodel/test/cases/serializers/json_serialization_test.rb delete mode 100644 activemodel/test/cases/serializers/xml_serialization_test.rb (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializable/json_test.rb b/activemodel/test/cases/serializable/json_test.rb new file mode 100644 index 0000000000..ad5b04091e --- /dev/null +++ b/activemodel/test/cases/serializable/json_test.rb @@ -0,0 +1,219 @@ +require 'cases/helper' +require 'models/contact' +require 'models/automobile' +require 'active_support/core_ext/object/instance_variables' + +class Contact + extend ActiveModel::Naming + include ActiveModel::Serializable::JSON + include ActiveModel::Validations + + def attributes=(hash) + hash.each do |k, v| + instance_variable_set("@#{k}", v) + end + end + + def attributes + instance_values + end unless method_defined?(:attributes) +end + +class JsonSerializationTest < ActiveModel::TestCase + def setup + @contact = Contact.new + @contact.name = 'Konata Izumi' + @contact.age = 16 + @contact.created_at = Time.utc(2006, 8, 1) + @contact.awesome = true + @contact.preferences = { 'shows' => 'anime' } + end + + test "should include root in json" do + json = @contact.to_json + + assert_match %r{^\{"contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should not include root in json (class method)" do + begin + Contact.include_root_in_json = false + json = @contact.to_json + + assert_no_match %r{^\{"contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + ensure + Contact.include_root_in_json = true + end + end + + test "should include root in json (option) even if the default is set to false" do + begin + Contact.include_root_in_json = false + json = @contact.to_json(:root => true) + assert_match %r{^\{"contact":\{}, json + ensure + Contact.include_root_in_json = true + end + end + + test "should not include root in json (option)" do + + json = @contact.to_json(:root => false) + + assert_no_match %r{^\{"contact":\{}, json + end + + test "should include custom root in json" do + json = @contact.to_json(:root => 'json_contact') + + assert_match %r{^\{"json_contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should encode all encodable attributes" do + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with only" do + json = @contact.to_json(:only => [:name, :age]) + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert_no_match %r{"awesome":true}, json + assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_no_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with except" do + json = @contact.to_json(:except => [:name, :age]) + + assert_no_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{"age":16}, json + assert_match %r{"awesome":true}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "methods are called on object" do + # Define methods on fixture. + def @contact.label; "Has cheezburger"; end + def @contact.favorite_quote; "Constraints are liberating"; end + + # Single method. + assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label) + + # Both methods. + methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote]) + assert_match %r{"label":"Has cheezburger"}, methods_json + assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json + end + + test "should return OrderedHash for errors" do + contact = Contact.new + contact.errors.add :name, "can't be blank" + contact.errors.add :name, "is too short (minimum is 2 characters)" + contact.errors.add :age, "must be 16 or over" + + hash = ActiveSupport::OrderedHash.new + hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"] + hash[:age] = ["must be 16 or over"] + assert_equal hash.to_json, contact.errors.to_json + end + + test "serializable_hash should not modify options passed in argument" do + options = { :except => :name } + @contact.serializable_hash(options) + + assert_nil options[:only] + assert_equal :name, options[:except] + end + + test "as_json should return a hash" do + json = @contact.as_json + + assert_kind_of Hash, json + assert_kind_of Hash, json['contact'] + %w(name age created_at awesome preferences).each do |field| + assert_equal @contact.send(field), json['contact'][field] + end + end + + test "from_json should set the object's attributes" do + json = @contact.to_json + result = Contact.new.from_json(json) + + assert_equal result.name, @contact.name + assert_equal result.age, @contact.age + assert_equal Time.parse(result.created_at), @contact.created_at + assert_equal result.awesome, @contact.awesome + assert_equal result.preferences, @contact.preferences + end + + test "from_json should work without a root (method parameter)" do + json = @contact.to_json(:root => false) + result = Contact.new.from_json(json, false) + + assert_equal result.name, @contact.name + assert_equal result.age, @contact.age + assert_equal Time.parse(result.created_at), @contact.created_at + assert_equal result.awesome, @contact.awesome + assert_equal result.preferences, @contact.preferences + end + + test "from_json should work without a root (class attribute)" do + begin + Contact.include_root_in_json = false + json = @contact.to_json + result = Contact.new.from_json(json) + + assert_equal result.name, @contact.name + assert_equal result.age, @contact.age + assert_equal Time.parse(result.created_at), @contact.created_at + assert_equal result.awesome, @contact.awesome + assert_equal result.preferences, @contact.preferences + ensure + Contact.include_root_in_json = true + end + end + + test "custom as_json should be honored when generating json" do + def @contact.as_json(options); { :name => name, :created_at => created_at }; end + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json + assert_no_match %r{"awesome":}, json + assert_no_match %r{"preferences":}, json + end + + test "custom as_json options should be extendible" do + def @contact.as_json(options = {}); super(options.merge(:only => [:name])); end + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json + assert_no_match %r{"awesome":}, json + assert_no_match %r{"preferences":}, json + end + +end diff --git a/activemodel/test/cases/serializable/xml_test.rb b/activemodel/test/cases/serializable/xml_test.rb new file mode 100644 index 0000000000..817ca1e736 --- /dev/null +++ b/activemodel/test/cases/serializable/xml_test.rb @@ -0,0 +1,206 @@ +require 'cases/helper' +require 'models/contact' +require 'active_support/core_ext/object/instance_variables' +require 'ostruct' + +class Contact + extend ActiveModel::Naming + include ActiveModel::Serializable::XML + + attr_accessor :address, :friends + + def attributes + instance_values.except("address", "friends") + end +end + +module Admin + class Contact < ::Contact + end +end + +class Customer < Struct.new(:name) +end + +class Address + extend ActiveModel::Naming + include ActiveModel::Serializable::XML + + attr_accessor :street, :city, :state, :zip + + def attributes + instance_values + end +end + +class SerializableContact < Contact + def serializable_hash(options={}) + super(options.merge(:only => [:name, :age])) + end +end + +class XmlSerializationTest < ActiveModel::TestCase + def setup + @contact = Contact.new + @contact.name = 'aaron stack' + @contact.age = 25 + @contact.created_at = Time.utc(2006, 8, 1) + @contact.awesome = false + customer = Customer.new + customer.name = "John" + @contact.preferences = customer + @contact.address = Address.new + @contact.address.street = "123 Lane" + @contact.address.city = "Springfield" + @contact.address.state = "CA" + @contact.address.zip = 11111 + @contact.friends = [Contact.new, Contact.new] + end + + test "should serialize default root" do + @xml = @contact.to_xml + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should serialize namespaced root" do + @xml = Admin::Contact.new(@contact.attributes).to_xml + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should serialize default root with namespace" do + @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should serialize custom root" do + @xml = @contact.to_xml :root => 'xml_contact' + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should allow undasherized tags" do + @xml = @contact.to_xml :root => 'xml_contact', :dasherize => false + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{ 'xml_contact', :camelize => true + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{ 'xml_contact', :camelize => :lower + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{aaron stack}, @xml + assert_match %r{25}, @xml + assert_no_match %r{}, @xml + end + + test "should allow skipped types" do + @xml = @contact.to_xml :skip_types => true + assert_match %r{25}, @xml + end + + test "should include yielded additions" do + @xml = @contact.to_xml do |xml| + xml.creator "David" + end + assert_match %r{David}, @xml + end + + test "should serialize string" do + assert_match %r{aaron stack}, @contact.to_xml + end + + test "should serialize nil" do + assert_match %r{}, @contact.to_xml(:methods => :pseudonyms) + end + + test "should serialize integer" do + assert_match %r{25}, @contact.to_xml + end + + test "should serialize datetime" do + assert_match %r{2006-08-01T00:00:00Z}, @contact.to_xml + end + + test "should serialize boolean" do + assert_match %r{false}, @contact.to_xml + end + + test "should serialize array" do + assert_match %r{\s*twitter\s*github\s*}, @contact.to_xml(:methods => :social) + end + + test "should serialize hash" do + assert_match %r{\s*github\s*}, @contact.to_xml(:methods => :network) + end + + test "should serialize yaml" do + assert_match %r{--- !ruby/struct:Customer(\s*)\nname: John\n}, @contact.to_xml + end + + test "should call proc on object" do + proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') } + xml = @contact.to_xml(:procs => [ proc ]) + assert_match %r{unknown}, xml + end + + test 'should supply serializable to second proc argument' do + proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) } + xml = @contact.to_xml(:procs => [ proc ]) + assert_match %r{kcats noraa}, xml + end + + test "should serialize string correctly when type passed" do + xml = @contact.to_xml :type => 'Contact' + assert_match %r{}, xml + assert_match %r{aaron stack}, xml + end + + test "include option with singular association" do + xml = @contact.to_xml :include => :address, :indent => 0 + assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) + end + + test "include option with plural association" do + xml = @contact.to_xml :include => :friends, :indent => 0 + assert_match %r{}, xml + assert_match %r{}, xml + end + + test "multiple includes" do + xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => [ :address, :friends ] + assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) + assert_match %r{}, xml + assert_match %r{}, xml + end + + test "include with options" do + xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => { :address => { :only => :city } } + assert xml.include?(%(>
Springfield
)) + end + + test "propagates skip_types option to included associations" do + xml = @contact.to_xml :include => :friends, :indent => 0, :skip_types => true + assert_match %r{}, xml + assert_match %r{}, xml + end +end diff --git a/activemodel/test/cases/serializable_test.rb b/activemodel/test/cases/serializable_test.rb new file mode 100644 index 0000000000..46ee372c6f --- /dev/null +++ b/activemodel/test/cases/serializable_test.rb @@ -0,0 +1,151 @@ +require "cases/helper" +require 'active_support/core_ext/object/instance_variables' + +class SerializationTest < ActiveModel::TestCase + class User + include ActiveModel::Serializable + + attr_accessor :name, :email, :gender, :address, :friends + + def initialize(name, email, gender) + @name, @email, @gender = name, email, gender + @friends = [] + end + + def attributes + instance_values.except("address", "friends") + end + + def foo + 'i_am_foo' + end + end + + class Address + include ActiveModel::Serializable + + attr_accessor :street, :city, :state, :zip + + def attributes + instance_values + end + end + + setup do + @user = User.new('David', 'david@example.com', 'male') + @user.address = Address.new + @user.address.street = "123 Lane" + @user.address.city = "Springfield" + @user.address.state = "CA" + @user.address.zip = 11111 + @user.friends = [User.new('Joe', 'joe@example.com', 'male'), + User.new('Sue', 'sue@example.com', 'female')] + end + + def test_method_serializable_hash_should_work + expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash + end + + def test_method_serializable_hash_should_work_with_only_option + expected = {"name"=>"David"} + assert_equal expected , @user.serializable_hash(:only => [:name]) + end + + def test_method_serializable_hash_should_work_with_except_option + expected = {"gender"=>"male", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash(:except => [:name]) + end + + def test_method_serializable_hash_should_work_with_methods_option + expected = {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash(:methods => [:foo]) + end + + def test_method_serializable_hash_should_work_with_only_and_methods + expected = {:foo=>"i_am_foo"} + assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo]) + end + + def test_method_serializable_hash_should_work_with_except_and_methods + expected = {"gender"=>"male", :foo=>"i_am_foo"} + assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo]) + end + + def test_should_not_call_methods_that_dont_respond + expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash(:methods => [:bar]) + end + + def test_should_use_read_attribute_for_serialization + def @user.read_attribute_for_serialization(n) + "Jon" + end + + expected = { "name" => "Jon" } + assert_equal expected, @user.serializable_hash(:only => :name) + end + + def test_include_option_with_singular_association + expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", + :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} + assert_equal expected , @user.serializable_hash(:include => :address) + end + + def test_include_option_with_plural_association + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected , @user.serializable_hash(:include => :friends) + end + + def test_include_option_with_empty_association + @user.friends = [] + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]} + assert_equal expected , @user.serializable_hash(:include => :friends) + end + + def test_multiple_includes + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected , @user.serializable_hash(:include => [:address, :friends]) + end + + def test_include_with_options + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :address=>{"street"=>"123 Lane"}} + assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}}) + end + + def test_nested_include + @user.friends.first.friends = [@user] + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', + :friends => [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', :friends => []}]} + assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}}) + end + + def test_only_include + expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]} + assert_equal expected , @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}}) + end + + def test_except_include + expected = {"name"=>"David", "email"=>"david@example.com", + :friends => [{"name" => 'Joe', "email" => 'joe@example.com'}, + {"name" => "Sue", "email" => 'sue@example.com'}]} + assert_equal expected , @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}}) + end + + def test_multiple_includes_with_options + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :address=>{"street"=>"123 Lane"}, + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected , @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends]) + end + +end diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb deleted file mode 100644 index b8dad9d51f..0000000000 --- a/activemodel/test/cases/serialization_test.rb +++ /dev/null @@ -1,151 +0,0 @@ -require "cases/helper" -require 'active_support/core_ext/object/instance_variables' - -class SerializationTest < ActiveModel::TestCase - class User - include ActiveModel::Serialization - - attr_accessor :name, :email, :gender, :address, :friends - - def initialize(name, email, gender) - @name, @email, @gender = name, email, gender - @friends = [] - end - - def attributes - instance_values.except("address", "friends") - end - - def foo - 'i_am_foo' - end - end - - class Address - include ActiveModel::Serialization - - attr_accessor :street, :city, :state, :zip - - def attributes - instance_values - end - end - - setup do - @user = User.new('David', 'david@example.com', 'male') - @user.address = Address.new - @user.address.street = "123 Lane" - @user.address.city = "Springfield" - @user.address.state = "CA" - @user.address.zip = 11111 - @user.friends = [User.new('Joe', 'joe@example.com', 'male'), - User.new('Sue', 'sue@example.com', 'female')] - end - - def test_method_serializable_hash_should_work - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash - end - - def test_method_serializable_hash_should_work_with_only_option - expected = {"name"=>"David"} - assert_equal expected , @user.serializable_hash(:only => [:name]) - end - - def test_method_serializable_hash_should_work_with_except_option - expected = {"gender"=>"male", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash(:except => [:name]) - end - - def test_method_serializable_hash_should_work_with_methods_option - expected = {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash(:methods => [:foo]) - end - - def test_method_serializable_hash_should_work_with_only_and_methods - expected = {:foo=>"i_am_foo"} - assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo]) - end - - def test_method_serializable_hash_should_work_with_except_and_methods - expected = {"gender"=>"male", :foo=>"i_am_foo"} - assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo]) - end - - def test_should_not_call_methods_that_dont_respond - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash(:methods => [:bar]) - end - - def test_should_use_read_attribute_for_serialization - def @user.read_attribute_for_serialization(n) - "Jon" - end - - expected = { "name" => "Jon" } - assert_equal expected, @user.serializable_hash(:only => :name) - end - - def test_include_option_with_singular_association - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", - :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} - assert_equal expected , @user.serializable_hash(:include => :address) - end - - def test_include_option_with_plural_association - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected , @user.serializable_hash(:include => :friends) - end - - def test_include_option_with_empty_association - @user.friends = [] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]} - assert_equal expected , @user.serializable_hash(:include => :friends) - end - - def test_multiple_includes - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected , @user.serializable_hash(:include => [:address, :friends]) - end - - def test_include_with_options - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane"}} - assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}}) - end - - def test_nested_include - @user.friends.first.friends = [@user] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', - :friends => [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', :friends => []}]} - assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}}) - end - - def test_only_include - expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]} - assert_equal expected , @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}}) - end - - def test_except_include - expected = {"name"=>"David", "email"=>"david@example.com", - :friends => [{"name" => 'Joe', "email" => 'joe@example.com'}, - {"name" => "Sue", "email" => 'sue@example.com'}]} - assert_equal expected , @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}}) - end - - def test_multiple_includes_with_options - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane"}, - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected , @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends]) - end - -end diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index 00d519dc1a..e99b3692ec 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -9,18 +9,34 @@ class SerializerTest < ActiveModel::TestCase def read_attribute_for_serialization(name) @attributes[name] end + + def as_json(*) + { :model => "Model" } + end end - class User < Model + class User + include ActiveModel::Serializable + attr_accessor :superuser + attr_writer :model_serializer def initialize(hash={}) - super hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password") + @model_serializer = nil + @attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password") + end + + def read_attribute_for_serialization(name) + @attributes[name] end def super_user? @superuser end + + def model_serializer + @model_serializer || super + end end class Post < Model @@ -403,4 +419,47 @@ class SerializerTest < ActiveModel::TestCase } }, serializer.as_json) end -end + + def test_array_serializer + model = Model.new + user = User.new + post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") + comments = Comment.new(:title => "Comment1", :id => 1) + post.comments = [] + + array = [model, post, comments] + serializer = ActiveModel::Serializer::Finder.find(array, user).new(array, user) + assert_equal([ + { :model => "Model" }, + { :post => { :body => "Body of new post", :comments => [], :title => "New Post" } }, + { :comment => { :title => "Comment1" } } + ], serializer.as_json) + end + + def test_array_serializer_respects_model_serializer + user = User.new(:first_name => "Jose", :last_name => "Valim") + user.model_serializer = User2Serializer + + array = [user] + serializer = ActiveModel::Serializer::Finder.find(array, user).new(array, {}) + assert_equal([ + { :user2 => { :last_name => "Valim", :first_name => "Jose", :ok => true } }, + ], serializer.as_json) + end + + def test_finder_respects_model_serializer + user = User.new(:first_name => "Jose", :last_name => "Valim") + assert_equal UserSerializer, user.model_serializer + + serializer = ActiveModel::Serializer::Finder.find(user, self).new(user, {}) + assert_equal({ + :user => { :last_name => "Valim", :first_name => "Jose"}, + }, serializer.as_json) + + user.model_serializer = User2Serializer + serializer = ActiveModel::Serializer::Finder.find(user, self).new(user, {}) + assert_equal({ + :user2 => { :last_name => "Valim", :first_name => "Jose", :ok => true }, + }, serializer.as_json) + end +end \ No newline at end of file diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb deleted file mode 100644 index a754d610b9..0000000000 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ /dev/null @@ -1,219 +0,0 @@ -require 'cases/helper' -require 'models/contact' -require 'models/automobile' -require 'active_support/core_ext/object/instance_variables' - -class Contact - extend ActiveModel::Naming - include ActiveModel::Serializers::JSON - include ActiveModel::Validations - - def attributes=(hash) - hash.each do |k, v| - instance_variable_set("@#{k}", v) - end - end - - def attributes - instance_values - end unless method_defined?(:attributes) -end - -class JsonSerializationTest < ActiveModel::TestCase - def setup - @contact = Contact.new - @contact.name = 'Konata Izumi' - @contact.age = 16 - @contact.created_at = Time.utc(2006, 8, 1) - @contact.awesome = true - @contact.preferences = { 'shows' => 'anime' } - end - - test "should include root in json" do - json = @contact.to_json - - assert_match %r{^\{"contact":\{}, json - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"awesome":true}, json - assert_match %r{"preferences":\{"shows":"anime"\}}, json - end - - test "should not include root in json (class method)" do - begin - Contact.include_root_in_json = false - json = @contact.to_json - - assert_no_match %r{^\{"contact":\{}, json - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"awesome":true}, json - assert_match %r{"preferences":\{"shows":"anime"\}}, json - ensure - Contact.include_root_in_json = true - end - end - - test "should include root in json (option) even if the default is set to false" do - begin - Contact.include_root_in_json = false - json = @contact.to_json(:root => true) - assert_match %r{^\{"contact":\{}, json - ensure - Contact.include_root_in_json = true - end - end - - test "should not include root in json (option)" do - - json = @contact.to_json(:root => false) - - assert_no_match %r{^\{"contact":\{}, json - end - - test "should include custom root in json" do - json = @contact.to_json(:root => 'json_contact') - - assert_match %r{^\{"json_contact":\{}, json - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"awesome":true}, json - assert_match %r{"preferences":\{"shows":"anime"\}}, json - end - - test "should encode all encodable attributes" do - json = @contact.to_json - - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"awesome":true}, json - assert_match %r{"preferences":\{"shows":"anime"\}}, json - end - - test "should allow attribute filtering with only" do - json = @contact.to_json(:only => [:name, :age]) - - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert_no_match %r{"awesome":true}, json - assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_no_match %r{"preferences":\{"shows":"anime"\}}, json - end - - test "should allow attribute filtering with except" do - json = @contact.to_json(:except => [:name, :age]) - - assert_no_match %r{"name":"Konata Izumi"}, json - assert_no_match %r{"age":16}, json - assert_match %r{"awesome":true}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"preferences":\{"shows":"anime"\}}, json - end - - test "methods are called on object" do - # Define methods on fixture. - def @contact.label; "Has cheezburger"; end - def @contact.favorite_quote; "Constraints are liberating"; end - - # Single method. - assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label) - - # Both methods. - methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote]) - assert_match %r{"label":"Has cheezburger"}, methods_json - assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json - end - - test "should return OrderedHash for errors" do - contact = Contact.new - contact.errors.add :name, "can't be blank" - contact.errors.add :name, "is too short (minimum is 2 characters)" - contact.errors.add :age, "must be 16 or over" - - hash = ActiveSupport::OrderedHash.new - hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"] - hash[:age] = ["must be 16 or over"] - assert_equal hash.to_json, contact.errors.to_json - end - - test "serializable_hash should not modify options passed in argument" do - options = { :except => :name } - @contact.serializable_hash(options) - - assert_nil options[:only] - assert_equal :name, options[:except] - end - - test "as_json should return a hash" do - json = @contact.as_json - - assert_kind_of Hash, json - assert_kind_of Hash, json['contact'] - %w(name age created_at awesome preferences).each do |field| - assert_equal @contact.send(field), json['contact'][field] - end - end - - test "from_json should set the object's attributes" do - json = @contact.to_json - result = Contact.new.from_json(json) - - assert_equal result.name, @contact.name - assert_equal result.age, @contact.age - assert_equal Time.parse(result.created_at), @contact.created_at - assert_equal result.awesome, @contact.awesome - assert_equal result.preferences, @contact.preferences - end - - test "from_json should work without a root (method parameter)" do - json = @contact.to_json(:root => false) - result = Contact.new.from_json(json, false) - - assert_equal result.name, @contact.name - assert_equal result.age, @contact.age - assert_equal Time.parse(result.created_at), @contact.created_at - assert_equal result.awesome, @contact.awesome - assert_equal result.preferences, @contact.preferences - end - - test "from_json should work without a root (class attribute)" do - begin - Contact.include_root_in_json = false - json = @contact.to_json - result = Contact.new.from_json(json) - - assert_equal result.name, @contact.name - assert_equal result.age, @contact.age - assert_equal Time.parse(result.created_at), @contact.created_at - assert_equal result.awesome, @contact.awesome - assert_equal result.preferences, @contact.preferences - ensure - Contact.include_root_in_json = true - end - end - - test "custom as_json should be honored when generating json" do - def @contact.as_json(options); { :name => name, :created_at => created_at }; end - json = @contact.to_json - - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json - assert_no_match %r{"awesome":}, json - assert_no_match %r{"preferences":}, json - end - - test "custom as_json options should be extendible" do - def @contact.as_json(options = {}); super(options.merge(:only => [:name])); end - json = @contact.to_json - - assert_match %r{"name":"Konata Izumi"}, json - assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json - assert_no_match %r{"awesome":}, json - assert_no_match %r{"preferences":}, json - end - -end diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb deleted file mode 100644 index fc73d9dcd8..0000000000 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ /dev/null @@ -1,206 +0,0 @@ -require 'cases/helper' -require 'models/contact' -require 'active_support/core_ext/object/instance_variables' -require 'ostruct' - -class Contact - extend ActiveModel::Naming - include ActiveModel::Serializers::Xml - - attr_accessor :address, :friends - - def attributes - instance_values.except("address", "friends") - end -end - -module Admin - class Contact < ::Contact - end -end - -class Customer < Struct.new(:name) -end - -class Address - extend ActiveModel::Naming - include ActiveModel::Serializers::Xml - - attr_accessor :street, :city, :state, :zip - - def attributes - instance_values - end -end - -class SerializableContact < Contact - def serializable_hash(options={}) - super(options.merge(:only => [:name, :age])) - end -end - -class XmlSerializationTest < ActiveModel::TestCase - def setup - @contact = Contact.new - @contact.name = 'aaron stack' - @contact.age = 25 - @contact.created_at = Time.utc(2006, 8, 1) - @contact.awesome = false - customer = Customer.new - customer.name = "John" - @contact.preferences = customer - @contact.address = Address.new - @contact.address.street = "123 Lane" - @contact.address.city = "Springfield" - @contact.address.state = "CA" - @contact.address.zip = 11111 - @contact.friends = [Contact.new, Contact.new] - end - - test "should serialize default root" do - @xml = @contact.to_xml - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should serialize namespaced root" do - @xml = Admin::Contact.new(@contact.attributes).to_xml - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should serialize default root with namespace" do - @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should serialize custom root" do - @xml = @contact.to_xml :root => 'xml_contact' - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should allow undasherized tags" do - @xml = @contact.to_xml :root => 'xml_contact', :dasherize => false - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{ 'xml_contact', :camelize => true - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{ 'xml_contact', :camelize => :lower - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{aaron stack}, @xml - assert_match %r{25}, @xml - assert_no_match %r{}, @xml - end - - test "should allow skipped types" do - @xml = @contact.to_xml :skip_types => true - assert_match %r{25}, @xml - end - - test "should include yielded additions" do - @xml = @contact.to_xml do |xml| - xml.creator "David" - end - assert_match %r{David}, @xml - end - - test "should serialize string" do - assert_match %r{aaron stack}, @contact.to_xml - end - - test "should serialize nil" do - assert_match %r{}, @contact.to_xml(:methods => :pseudonyms) - end - - test "should serialize integer" do - assert_match %r{25}, @contact.to_xml - end - - test "should serialize datetime" do - assert_match %r{2006-08-01T00:00:00Z}, @contact.to_xml - end - - test "should serialize boolean" do - assert_match %r{false}, @contact.to_xml - end - - test "should serialize array" do - assert_match %r{\s*twitter\s*github\s*}, @contact.to_xml(:methods => :social) - end - - test "should serialize hash" do - assert_match %r{\s*github\s*}, @contact.to_xml(:methods => :network) - end - - test "should serialize yaml" do - assert_match %r{--- !ruby/struct:Customer(\s*)\nname: John\n}, @contact.to_xml - end - - test "should call proc on object" do - proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') } - xml = @contact.to_xml(:procs => [ proc ]) - assert_match %r{unknown}, xml - end - - test 'should supply serializable to second proc argument' do - proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) } - xml = @contact.to_xml(:procs => [ proc ]) - assert_match %r{kcats noraa}, xml - end - - test "should serialize string correctly when type passed" do - xml = @contact.to_xml :type => 'Contact' - assert_match %r{}, xml - assert_match %r{aaron stack}, xml - end - - test "include option with singular association" do - xml = @contact.to_xml :include => :address, :indent => 0 - assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) - end - - test "include option with plural association" do - xml = @contact.to_xml :include => :friends, :indent => 0 - assert_match %r{}, xml - assert_match %r{}, xml - end - - test "multiple includes" do - xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => [ :address, :friends ] - assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) - assert_match %r{}, xml - assert_match %r{}, xml - end - - test "include with options" do - xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => { :address => { :only => :city } } - assert xml.include?(%(>
Springfield
)) - end - - test "propagates skip_types option to included associations" do - xml = @contact.to_xml :include => :friends, :indent => 0, :skip_types => true - assert_match %r{}, xml - assert_match %r{}, xml - end -end -- cgit v1.2.3 From 7fcc8c0a1f38c77b12cb6ffe81fb2887e6c60b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 23 Nov 2011 23:45:27 +0000 Subject: Rely solely on active_model_serializer and remove the fancy constant lookup. --- activemodel/test/cases/serializer_test.rb | 37 +++---------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index e99b3692ec..f6c4282d39 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -19,10 +19,8 @@ class SerializerTest < ActiveModel::TestCase include ActiveModel::Serializable attr_accessor :superuser - attr_writer :model_serializer def initialize(hash={}) - @model_serializer = nil @attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password") end @@ -33,17 +31,15 @@ class SerializerTest < ActiveModel::TestCase def super_user? @superuser end - - def model_serializer - @model_serializer || super - end end class Post < Model attr_accessor :comments + def active_model_serializer; PostSerializer; end end class Comment < Model + def active_model_serializer; CommentSerializer; end end class UserSerializer < ActiveModel::Serializer @@ -428,38 +424,11 @@ class SerializerTest < ActiveModel::TestCase post.comments = [] array = [model, post, comments] - serializer = ActiveModel::Serializer::Finder.find(array, user).new(array, user) + serializer = array.active_model_serializer.new(array, user) assert_equal([ { :model => "Model" }, { :post => { :body => "Body of new post", :comments => [], :title => "New Post" } }, { :comment => { :title => "Comment1" } } ], serializer.as_json) end - - def test_array_serializer_respects_model_serializer - user = User.new(:first_name => "Jose", :last_name => "Valim") - user.model_serializer = User2Serializer - - array = [user] - serializer = ActiveModel::Serializer::Finder.find(array, user).new(array, {}) - assert_equal([ - { :user2 => { :last_name => "Valim", :first_name => "Jose", :ok => true } }, - ], serializer.as_json) - end - - def test_finder_respects_model_serializer - user = User.new(:first_name => "Jose", :last_name => "Valim") - assert_equal UserSerializer, user.model_serializer - - serializer = ActiveModel::Serializer::Finder.find(user, self).new(user, {}) - assert_equal({ - :user => { :last_name => "Valim", :first_name => "Jose"}, - }, serializer.as_json) - - user.model_serializer = User2Serializer - serializer = ActiveModel::Serializer::Finder.find(user, self).new(user, {}) - assert_equal({ - :user2 => { :last_name => "Valim", :first_name => "Jose", :ok => true }, - }, serializer.as_json) - end end \ No newline at end of file -- cgit v1.2.3 From 28bcda4098985ae5eb2d18c5214d95cf04f3ea93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 23 Nov 2011 23:53:20 +0000 Subject: Rename UserSerializer to DefaultUserSerializer in tests. --- activemodel/test/cases/serializer_test.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/serializer_test.rb b/activemodel/test/cases/serializer_test.rb index f6c4282d39..2b2e21540f 100644 --- a/activemodel/test/cases/serializer_test.rb +++ b/activemodel/test/cases/serializer_test.rb @@ -44,16 +44,16 @@ class SerializerTest < ActiveModel::TestCase class UserSerializer < ActiveModel::Serializer attributes :first_name, :last_name - end - - class User2Serializer < ActiveModel::Serializer - attributes :first_name, :last_name def serializable_hash attributes.merge(:ok => true).merge(scope) end end + class DefaultUserSerializer < ActiveModel::Serializer + attributes :first_name, :last_name + end + class MyUserSerializer < ActiveModel::Serializer attributes :first_name, :last_name @@ -85,34 +85,34 @@ class SerializerTest < ActiveModel::TestCase def test_attributes user = User.new - user_serializer = UserSerializer.new(user, nil) + user_serializer = DefaultUserSerializer.new(user, {}) hash = user_serializer.as_json assert_equal({ - :user => { :first_name => "Jose", :last_name => "Valim" } + :default_user => { :first_name => "Jose", :last_name => "Valim" } }, hash) end def test_attributes_method user = User.new - user_serializer = User2Serializer.new(user, {}) + user_serializer = UserSerializer.new(user, {}) hash = user_serializer.as_json assert_equal({ - :user2 => { :first_name => "Jose", :last_name => "Valim", :ok => true } + :user => { :first_name => "Jose", :last_name => "Valim", :ok => true } }, hash) end def test_serializer_receives_scope user = User.new - user_serializer = User2Serializer.new(user, {:scope => true}) + user_serializer = UserSerializer.new(user, {:scope => true}) hash = user_serializer.as_json assert_equal({ - :user2 => { + :user => { :first_name => "Jose", :last_name => "Valim", :ok => true, @@ -419,15 +419,13 @@ class SerializerTest < ActiveModel::TestCase def test_array_serializer model = Model.new user = User.new - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") comments = Comment.new(:title => "Comment1", :id => 1) - post.comments = [] - array = [model, post, comments] - serializer = array.active_model_serializer.new(array, user) + array = [model, user, comments] + serializer = array.active_model_serializer.new(array, {:scope => true}) assert_equal([ { :model => "Model" }, - { :post => { :body => "Body of new post", :comments => [], :title => "New Post" } }, + { :user => { :last_name=>"Valim", :ok=>true, :first_name=>"Jose", :scope => true } }, { :comment => { :title => "Comment1" } } ], serializer.as_json) end -- cgit v1.2.3