Permalink
Browse files

Hash#to_xml supports YAML attributes; ActiveRecord::Base#to_xml suppo…

…rt serialized attributes. Closes #7502.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6444 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
jeremy committed Mar 18, 2007
1 parent 3202fba commit 27ba5edef1c4264a8d1c0e54675723d37a391dd8
View
@@ -1,5 +1,7 @@
*SVN*
* Base#to_xml supports serialized attributes. #7502 [jonathan]
* Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints. [Jeremy Kemper]
* Remove deprecated object transactions. People relying on this functionality should install the object_transactions plugin at http://code.bitsweat.net/svn/object_transactions. Closes #5637 [Koz, Jeremy Kemper]
@@ -275,7 +275,7 @@ def decorations(include_types = true)
protected
def compute_type
type = @record.class.columns_hash[name].type
type = @record.class.serialized_attributes.has_key?(name) ? :yaml : @record.class.columns_hash[name].type
case type
when :text
@@ -1509,7 +1509,7 @@ def test_to_xml
assert xml.include?(%(<id type="integer">1</id>))
assert xml.include?(%(<replies-count type="integer">1</replies-count>))
assert xml.include?(%(<written-on type="datetime">#{written_on_in_current_timezone}</written-on>))
assert xml.include?(%(<content>Have a nice day</content>))
assert xml.include?(%(<content type="yaml">--- Have a nice day\n</content>))
assert xml.include?(%(<author-email-address>david@loudthinking.com</author-email-address>))
assert xml.match(%(<parent-id type="integer"></parent-id>))
if current_adapter?(:SybaseAdapter, :SQLServerAdapter, :OracleAdapter)
@@ -9,11 +9,14 @@ def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
column :name, :string
column :age, :integer
column :avatar, :binary
column :created_at, :datetime
column :awesome, :boolean
column :name, :string
column :age, :integer
column :avatar, :binary
column :created_at, :datetime
column :awesome, :boolean
column :preferences, :string
serialize :preferences
end
class XmlSerializationTest < Test::Unit::TestCase
@@ -57,7 +60,7 @@ def test_should_allow_attribute_filtering
class DefaultXmlSerializationTest < Test::Unit::TestCase
def setup
@xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false).to_xml
@xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
end
def test_should_serialize_string
@@ -81,6 +84,10 @@ def test_should_serialize_datetime
def test_should_serialize_boolean
assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
end
def test_should_serialize_yaml
assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
end
end
class NilXmlSerializationTest < Test::Unit::TestCase
@@ -109,6 +116,10 @@ def test_should_serialize_datetime
def test_should_serialize_boolean
assert_match %r{<awesome type=\"boolean\"></awesome>}, @xml
end
def test_should_serialize_yaml
assert_match %r{<preferences type=\"yaml\"></preferences>}, @xml
end
end
class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
View
@@ -1,5 +1,7 @@
*SVN*
* Hash#to_xml supports YAML attributes. #7502 [jonathan]
* Refactor ActiveSupport::JSON to be less obtuse. Add support for JSON decoding by way of Syck with ActiveSupport::JSON.decode(json_string). Prevent hash keys that are JavaScript reserved words from being unquoted during encoding. [Sam Stephenson]
* alias_method_chain preserves the original method's visibility. #7854 [Jonathan Viney]
@@ -38,7 +38,8 @@ module Conversions
XML_FORMATTING = {
"date" => Proc.new { |date| date.to_s(:db) },
"datetime" => Proc.new { |time| time.xmlschema },
"binary" => Proc.new { |binary| Base64.encode64(binary) }
"binary" => Proc.new { |binary| Base64.encode64(binary) },
"yaml" => Proc.new { |yaml| yaml.to_yaml }
} unless defined? XML_FORMATTING
def self.included(klass)
@@ -105,7 +106,7 @@ def to_xml(options = {})
module ClassMethods
def from_xml(xml)
# TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple
undasherize_keys(typecast_xml_value(XmlSimple.xml_in(xml,
typecast_xml_value(undasherize_keys(XmlSimple.xml_in(xml,
'forcearray' => false,
'forcecontent' => true,
'keeproot' => true,
@@ -129,6 +130,7 @@ def typecast_xml_value(value)
when "boolean" then content.strip == "true"
when "datetime" then ::Time.parse(content).utc
when "date" then ::Date.parse(content)
when "yaml" then YAML::load(content) rescue content
else content
end
else
@@ -376,7 +376,7 @@ def test_single_record_from_xml
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<content type="yaml">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id></parent-id>
</topic>
@@ -391,7 +391,7 @@ def test_single_record_from_xml
:replies_close_in => 2592000000,
:written_on => Date.new(2003, 7, 16),
:viewed_at => Time.utc(2003, 7, 16, 9, 28),
:content => "Have a nice day",
:content => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
:author_email_address => "david@loudthinking.com",
:parent_id => nil
}.stringify_keys
@@ -407,6 +407,7 @@ def test_single_record_from_xml_with_nil_values
<approved type="boolean"></approved>
<written-on type="date"></written-on>
<viewed-at type="datetime"></viewed-at>
<content type="yaml"></content>
<parent-id></parent-id>
</topic>
EOT
@@ -416,7 +417,8 @@ def test_single_record_from_xml_with_nil_values
:id => nil,
:approved => nil,
:written_on => nil,
:viewed_at => nil,
:viewed_at => nil,
:content => nil,
:parent_id => nil
}.stringify_keys

0 comments on commit 27ba5ed

Please sign in to comment.