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 remove_method :attributes if method_defined?(:attributes) 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