Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6444

Show
Ignore:
Timestamp:
03/18/07 07:30:09 (3 years ago)
Author:
bitsweat
Message:

Hash#to_xml supports YAML attributes; ActiveRecord::Base#to_xml support serialized attributes. Closes #7502.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6440 r6444  
    11*SVN* 
     2 
     3* Base#to_xml supports serialized attributes.  #7502 [jonathan] 
    24 
    35* Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints.  [Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/xml_serialization.rb

    r5210 r6444  
    276276      protected 
    277277        def compute_type 
    278           type = @record.class.columns_hash[name].type 
     278          type = @record.class.serialized_attributes.has_key?(name) ? :yaml : @record.class.columns_hash[name].type 
    279279 
    280280          case type 
  • trunk/activerecord/test/base_test.rb

    r6440 r6444  
    15101510    assert xml.include?(%(<replies-count type="integer">1</replies-count>)) 
    15111511    assert xml.include?(%(<written-on type="datetime">#{written_on_in_current_timezone}</written-on>)) 
    1512     assert xml.include?(%(<content>Have a nice day</content>)) 
     1512    assert xml.include?(%(<content type="yaml">--- Have a nice day\n</content>)) 
    15131513    assert xml.include?(%(<author-email-address>david@loudthinking.com</author-email-address>)) 
    15141514    assert xml.match(%(<parent-id type="integer"></parent-id>)) 
  • trunk/activerecord/test/xml_serialization_test.rb

    r5170 r6444  
    1010  end 
    1111 
    12   column :name,       :string 
    13   column :age,        :integer 
    14   column :avatar,     :binary 
    15   column :created_at, :datetime 
    16   column :awesome,    :boolean 
     12  column :name,        :string 
     13  column :age,         :integer 
     14  column :avatar,      :binary 
     15  column :created_at,  :datetime 
     16  column :awesome,     :boolean 
     17  column :preferences, :string 
     18   
     19  serialize :preferences 
    1720end 
    1821 
     
    5861class DefaultXmlSerializationTest < Test::Unit::TestCase 
    5962  def setup 
    60     @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false).to_xml 
     63    @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml 
    6164  end 
    6265 
     
    8184  def test_should_serialize_boolean 
    8285    assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml 
     86  end 
     87   
     88  def test_should_serialize_yaml 
     89    assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml 
    8390  end 
    8491end 
     
    110117    assert_match %r{<awesome type=\"boolean\"></awesome>}, @xml 
    111118  end 
     119   
     120  def test_should_serialize_yaml 
     121    assert_match %r{<preferences type=\"yaml\"></preferences>}, @xml 
     122  end 
    112123end 
    113124 
  • trunk/activesupport/CHANGELOG

    r6443 r6444  
    11*SVN* 
     2 
     3* Hash#to_xml supports YAML attributes.  #7502 [jonathan] 
    24 
    35* 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] 
  • trunk/activesupport/lib/active_support/core_ext/hash/conversions.rb

    r6378 r6444  
    3939          "date"     => Proc.new { |date| date.to_s(:db) }, 
    4040          "datetime" => Proc.new { |time| time.xmlschema }, 
    41           "binary"   => Proc.new { |binary| Base64.encode64(binary) } 
     41          "binary"   => Proc.new { |binary| Base64.encode64(binary) }, 
     42          "yaml"     => Proc.new { |yaml| yaml.to_yaml } 
    4243        } unless defined? XML_FORMATTING 
    4344 
     
    106107          def from_xml(xml) 
    107108            # TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple 
    108             undasherize_keys(typecast_xml_value(XmlSimple.xml_in(xml, 
     109            typecast_xml_value(undasherize_keys(XmlSimple.xml_in(xml, 
    109110              'forcearray'   => false, 
    110111              'forcecontent' => true, 
     
    130131                      when "datetime" then ::Time.parse(content).utc 
    131132                      when "date"     then ::Date.parse(content) 
     133                      when "yaml"     then YAML::load(content) rescue content 
    132134                      else                 content 
    133135                    end 
  • trunk/activesupport/test/core_ext/hash_ext_test.rb

    r6378 r6444  
    377377        <written-on type="date">2003-07-16</written-on> 
    378378        <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at> 
    379         <content>Have a nice day</content> 
     379        <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> 
    380380        <author-email-address>david@loudthinking.com</author-email-address> 
    381381        <parent-id></parent-id> 
     
    392392      :written_on => Date.new(2003, 7, 16), 
    393393      :viewed_at => Time.utc(2003, 7, 16, 9, 28), 
    394       :content => "Have a nice day"
     394      :content => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] }
    395395      :author_email_address => "david@loudthinking.com", 
    396396      :parent_id => nil 
     
    408408        <written-on type="date"></written-on> 
    409409        <viewed-at type="datetime"></viewed-at> 
     410        <content type="yaml"></content> 
    410411        <parent-id></parent-id> 
    411412      </topic> 
     
    417418      :approved   => nil, 
    418419      :written_on => nil, 
    419       :viewed_at  => nil,  
     420      :viewed_at  => nil, 
     421      :content    => nil,  
    420422      :parent_id  => nil 
    421423    }.stringify_keys