diff options
author | José Valim <jose.valim@gmail.com> | 2011-11-23 23:18:13 +0000 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-11-23 23:18:15 +0000 |
commit | 8896b4fdc8a543157cdf4dfc378607ebf6c10ab0 (patch) | |
tree | 5deca0d54d2c103d0a9e6167a756d638fc25e9ec /activemodel/test | |
parent | 0536ea8c7855222111fad6df71d0d09b77ea4317 (diff) | |
download | rails-8896b4fdc8a543157cdf4dfc378607ebf6c10ab0.tar.gz rails-8896b4fdc8a543157cdf4dfc378607ebf6c10ab0.tar.bz2 rails-8896b4fdc8a543157cdf4dfc378607ebf6c10ab0.zip |
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.
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/serializable/json_test.rb (renamed from activemodel/test/cases/serializers/json_serialization_test.rb) | 2 | ||||
-rw-r--r-- | activemodel/test/cases/serializable/xml_test.rb (renamed from activemodel/test/cases/serializers/xml_serialization_test.rb) | 4 | ||||
-rw-r--r-- | activemodel/test/cases/serializable_test.rb (renamed from activemodel/test/cases/serialization_test.rb) | 4 | ||||
-rw-r--r-- | activemodel/test/cases/serializer_test.rb | 65 |
4 files changed, 67 insertions, 8 deletions
diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializable/json_test.rb index a754d610b9..ad5b04091e 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializable/json_test.rb @@ -5,7 +5,7 @@ require 'active_support/core_ext/object/instance_variables' class Contact extend ActiveModel::Naming - include ActiveModel::Serializers::JSON + include ActiveModel::Serializable::JSON include ActiveModel::Validations def attributes=(hash) diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializable/xml_test.rb index fc73d9dcd8..817ca1e736 100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializable/xml_test.rb @@ -5,7 +5,7 @@ require 'ostruct' class Contact extend ActiveModel::Naming - include ActiveModel::Serializers::Xml + include ActiveModel::Serializable::XML attr_accessor :address, :friends @@ -24,7 +24,7 @@ end class Address extend ActiveModel::Naming - include ActiveModel::Serializers::Xml + include ActiveModel::Serializable::XML attr_accessor :street, :city, :state, :zip diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serializable_test.rb index b8dad9d51f..46ee372c6f 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serializable_test.rb @@ -3,7 +3,7 @@ require 'active_support/core_ext/object/instance_variables' class SerializationTest < ActiveModel::TestCase class User - include ActiveModel::Serialization + include ActiveModel::Serializable attr_accessor :name, :email, :gender, :address, :friends @@ -22,7 +22,7 @@ class SerializationTest < ActiveModel::TestCase end class Address - include ActiveModel::Serialization + include ActiveModel::Serializable attr_accessor :street, :city, :state, :zip 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 |