diff options
author | José Valim <jose.valim@gmail.com> | 2010-04-29 12:27:25 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-04-29 12:42:42 +0200 |
commit | 2e9af3638d950ef840e1287f99e323887ec6a4c4 (patch) | |
tree | eb08bcf15f3096f15ffc705190df649cc208b071 /activerecord | |
parent | 1bea5c755c25cd90171477fe725e64b26395b810 (diff) | |
download | rails-2e9af3638d950ef840e1287f99e323887ec6a4c4.tar.gz rails-2e9af3638d950ef840e1287f99e323887ec6a4c4.tar.bz2 rails-2e9af3638d950ef840e1287f99e323887ec6a4c4.zip |
Move several configuration values from Hash to ActiveSupport::XmlMini, which both Hash and Array depends on.
Also, refactored ActiveModel serializers to just use ActiveSupport::XmlMini.to_tag. As consequence, if a serialized attribute is an array or a hash, it's not encoded as yaml, but as a hash or array.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/serializers/xml_serializer.rb | 85 | ||||
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/xml_serialization_test.rb | 4 |
4 files changed, 40 insertions, 56 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index fcb0e31f79..7864d735ad 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0.0 [beta 4/release candidate] (unreleased)* +* Serialized attributes are not converted to YAML if they are any of the formats that can be serialized to XML (like Hash, Array and Strings). [José Valim] + * Destroy uses optimistic locking. If lock_version on the record you're destroying doesn't match lock_version in the database, a StaleObjectError is raised. #1966 [Curtis Hawthorne] * PostgreSQL: drop support for old postgres driver. Use pg 0.9.0 or later. [Jeremy Kemper] diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 255b03433d..ed5964d923 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -182,16 +182,31 @@ module ActiveRecord #:nodoc: options[:except] |= Array.wrap(@serializable.class.inheritance_column) end + def add_extra_behavior + add_includes + end + + def add_includes + procs = options.delete(:procs) + @serializable.send(:serializable_add_includes, options) do |association, records, opts| + add_associations(association, records, opts) + end + options[:procs] = procs + end + + # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well. def add_associations(association, records, opts) + association_name = association.to_s.singularize + merged_options = options.merge(opts).merge!(:root => association_name) + if records.is_a?(Enumerable) - tag = reformat_name(association.to_s) - type = options[:skip_types] ? {} : {:type => "array"} + tag = ActiveSupport::XmlMini.rename_key(association.to_s, options) + type = options[:skip_types] ? { } : {:type => "array"} if records.empty? - builder.tag!(tag, type) + @builder.tag!(tag, type) else - builder.tag!(tag, type) do - association_name = association.to_s.singularize + @builder.tag!(tag, type) do records.each do |record| if options[:skip_types] record_type = {} @@ -200,60 +215,30 @@ module ActiveRecord #:nodoc: record_type = {:type => record_class} end - record.to_xml opts.merge(:root => association_name).merge(record_type) + record.to_xml merged_options.merge(record_type) end end end - else - if record = @serializable.send(association) - record.to_xml(opts.merge(:root => association)) - end - end - end - - def serialize - args = [root] - if options[:namespace] - args << {:xmlns=>options[:namespace]} - end - - if options[:type] - args << {:type=>options[:type]} - end - - builder.tag!(*args) do - add_attributes - procs = options.delete(:procs) - @serializable.send(:serializable_add_includes, options) { |association, records, opts| - add_associations(association, records, opts) - } - options[:procs] = procs - add_procs - yield builder if block_given? + elsif record = @serializable.send(association) + record.to_xml(merged_options) end end class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc: - protected - def compute_type - type = @serializable.class.serialized_attributes.has_key?(name) ? :yaml : @serializable.class.columns_hash[name].type - - case type - when :text - :string - when :time - :datetime - else - type - end - end - end + def compute_type + type = @serializable.class.serialized_attributes.has_key?(name) ? + super : @serializable.class.columns_hash[name].type - class MethodAttribute < Attribute #:nodoc: - protected - def compute_type - Hash::XML_TYPE_NAMES[@serializable.send(name).class.name] || :string + case type + when :text + :string + when :time + :datetime + else + type end + end + protected :compute_type end end end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 2f4243a6aa..3623680de9 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -2085,6 +2085,7 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "topic", xml.root.name assert_equal "The First Topic" , xml.elements["//title"].text assert_equal "David" , xml.elements["//author-name"].text + assert_match "Have a nice day", xml.elements["//content"].text assert_equal "1", xml.elements["//id"].text assert_equal "integer" , xml.elements["//id"].attributes['type'] @@ -2095,10 +2096,6 @@ class BasicsTest < ActiveRecord::TestCase assert_equal written_on_in_current_timezone, xml.elements["//written-on"].text assert_equal "datetime" , xml.elements["//written-on"].attributes['type'] - assert_match(/^--- Have a nice day\n/ , xml.elements["//content"].text) - assert_equal 'Have a nice day' , YAML.load(xml.elements["//content"].text) - assert_equal "yaml" , xml.elements["//content"].attributes['type'] - assert_equal "david@loudthinking.com", xml.elements["//author-email-address"].text assert_equal nil, xml.elements["//parent-id"].text diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index b1c75ec8cd..bd29ae2612 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -79,8 +79,8 @@ class DefaultXmlSerializationTest < ActiveRecord::TestCase assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml end - def test_should_serialize_yaml - assert_match %r{<preferences type=\"yaml\">---\s?\n:gem: ruby\n</preferences>}, @xml + def test_should_serialize_hash + assert_match %r{<preferences>\s*<gem>ruby</gem>\s*</preferences>}m, @xml end end |