aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/serializers
diff options
context:
space:
mode:
authorJohn Firebaugh <john_firebaugh@us.ibm.com>2011-02-21 22:55:49 -0800
committerJohn Firebaugh <john_firebaugh@us.ibm.com>2011-07-17 11:34:07 -0700
commit4860143ee4ccafef474f14f40b8f70c2b6b54656 (patch)
tree1fa5a120a4b1d6f79feaf4b7dc18c3c41de6e523 /activemodel/test/cases/serializers
parent1723a7a6c6098eaa61ce964bebca2ed5f8f947b7 (diff)
downloadrails-4860143ee4ccafef474f14f40b8f70c2b6b54656.tar.gz
rails-4860143ee4ccafef474f14f40b8f70c2b6b54656.tar.bz2
rails-4860143ee4ccafef474f14f40b8f70c2b6b54656.zip
ActiveModel support for the :include serialization option
This commit moves support for the :include serialization option for serializing associated objects out of ActiveRecord in into ActiveModel. The following methods support the :include option: * serializable_hash * to_json * to_xml Instances must respond to methods named by the values of the :includes array (or keys of the :includes hash). If an association method returns an object that is_a?(Enumerable) (which AR has_many associations do), it is assumed to be a collection association, and its elements must respond to :serializable_hash. Otherwise it must respond to :serializable_hash itself. While here, fix #858, XmlSerializer should not singularize already singular association names.
Diffstat (limited to 'activemodel/test/cases/serializers')
-rw-r--r--activemodel/test/cases/serializers/xml_serialization_test.rb52
1 files changed, 50 insertions, 2 deletions
diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb
index f978191d22..a38ef8e223 100644
--- a/activemodel/test/cases/serializers/xml_serialization_test.rb
+++ b/activemodel/test/cases/serializers/xml_serialization_test.rb
@@ -7,9 +7,11 @@ class Contact
extend ActiveModel::Naming
include ActiveModel::Serializers::Xml
+ attr_accessor :address, :friends
+
def attributes
- instance_values
- end unless method_defined?(:attributes)
+ instance_values.except("address", "friends")
+ end
end
module Admin
@@ -20,6 +22,17 @@ 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 XmlSerializationTest < ActiveModel::TestCase
def setup
@contact = Contact.new
@@ -30,6 +43,12 @@ class XmlSerializationTest < ActiveModel::TestCase
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
@@ -138,4 +157,33 @@ class XmlSerializationTest < ActiveModel::TestCase
assert_match %r{<contact type="Contact">}, xml
assert_match %r{<name>aaron stack</name>}, 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{<friends type="array">}, xml
+ assert_match %r{<friend type="Contact">}, 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{<friends type="array">}, xml
+ assert_match %r{<friend type="Contact">}, xml
+ end
+
+ test "include with options" do
+ xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => { :address => { :only => :city } }
+ assert xml.include?(%(><address><city>Springfield</city></address>))
+ end
+
+ test "propagates skip_types option to included associations" do
+ xml = @contact.to_xml :include => :friends, :indent => 0, :skip_types => true
+ assert_match %r{<friends>}, xml
+ assert_match %r{<friend>}, xml
+ end
end