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

Changeset 7519

Show
Ignore:
Timestamp:
09/20/07 23:22:30 (1 year ago)
Author:
david
Message:

Added ActiveRecord::Base#to_json/from_json (currently does not support :include like to_xml) [DHH]. Added ActiveRecord::Base#from_xml [DHH]

Files:

Legend:

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

    r7510 r7519  
    11*SVN* 
     2 
     3* Added ActiveRecord::Base#to_json/from_json (currently does not support :include like to_xml) [DHH] 
     4 
     5* Added ActiveRecord::Base#from_xml [DHH]. Example: 
     6 
     7    xml = "<person><name>David</name></person>" 
     8    Person.new.from_xml(xml).name # => "David" 
    29 
    310* Define dynamic finders as real methods after first usage. [bscofield] 
  • trunk/activerecord/lib/active_record.rb

    r7498 r7519  
    5050require 'active_record/schema' 
    5151require 'active_record/calculations' 
    52 require 'active_record/xml_serialization' 
     52require 'active_record/serialization' 
    5353require 'active_record/attribute_methods' 
    5454 
     
    6666  include ActiveRecord::Reflection 
    6767  include ActiveRecord::Calculations 
    68   include ActiveRecord::XmlSerialization 
     68  include ActiveRecord::Serialization 
    6969  include ActiveRecord::AttributeMethods 
    7070end 
  • trunk/activerecord/lib/active_record/associations/association_collection.rb

    r7511 r7519  
    158158        end 
    159159      end 
     160 
    160161 
    161162      protected 
  • trunk/activerecord/lib/active_record/serializers/xml_serializer.rb

    r7367 r7519  
    11module ActiveRecord #:nodoc: 
    2   module XmlSerialization 
     2  module Serialization 
    33    # Builds an XML document to represent the model.   Some configuration is 
    44    # availble through +options+, however more complicated cases should use  
     
    125125      block_given? ? serializer.to_s(&block) : serializer.to_s 
    126126    end 
     127 
     128    def from_xml(xml) 
     129      self.attributes = Hash.from_xml(xml).values.first 
     130      self 
     131    end 
    127132  end 
    128133 
    129   class XmlSerializer #:nodoc: 
    130     attr_reader :options 
    131      
    132     def initialize(record, options = {}) 
    133       @record, @options = record, options.dup 
    134     end 
    135      
     134  class XmlSerializer < ActiveRecord::Serialization::Serializer #:nodoc: 
    136135    def builder 
    137136      @builder ||= begin 
     
    165164    # :only is set, always delete :except. 
    166165    def serializable_attributes 
    167       attribute_names = @record.attribute_names 
    168  
    169       if options[:only] 
    170         options.delete(:except) 
    171         attribute_names = attribute_names & Array(options[:only]).collect { |n| n.to_s } 
    172       else 
    173         options[:except] = Array(options[:except]) | Array(@record.class.inheritance_column) 
    174         attribute_names = attribute_names - options[:except].collect { |n| n.to_s } 
    175       end 
    176        
    177       attribute_names.collect { |name| Attribute.new(name, @record) } 
     166      serializable_attribute_names.collect { |name| Attribute.new(name, @record) } 
    178167    end 
    179168 
     
    266255      end 
    267256    end         
    268      
    269     alias_method :to_s, :serialize 
    270257 
    271258    class Attribute #:nodoc: 
  • trunk/activerecord/test/xml_serialization_test.rb

    r7173 r7519  
    11require 'abstract_unit' 
     2require 'fixtures/contact' 
    23require 'fixtures/post' 
    34require 'fixtures/author' 
    45require 'fixtures/tagging' 
    56require 'fixtures/comment' 
    6  
    7 class Contact < ActiveRecord::Base 
    8   # mock out self.columns so no pesky db is needed for these tests 
    9   def self.columns() @columns ||= []; end 
    10   def self.column(name, sql_type = nil, default = nil, null = true) 
    11     columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) 
    12   end 
    13  
    14   column :name,        :string 
    15   column :age,         :integer 
    16   column :avatar,      :binary 
    17   column :created_at,  :datetime 
    18   column :awesome,     :boolean 
    19   column :preferences, :string 
    20    
    21   serialize :preferences 
    22 end 
    237 
    248class XmlSerializationTest < Test::Unit::TestCase 
     
    4832  end 
    4933 
    50   def test_should_allow_attribute_filtering 
    51     @xml = Contact.new.to_xml :only => [:age, :name] 
    52     assert_match %r{<name},          @xml 
    53     assert_match %r{<age},           @xml 
    54     assert_no_match %r{<created-at}, @xml 
    55      
    56     @xml = Contact.new.to_xml :except => [:age, :name] 
    57     assert_no_match %r{<name},    @xml 
    58     assert_no_match %r{<age},     @xml 
    59     assert_match %r{<created-at}, @xml 
    60   end 
    61    
    6234  def test_should_include_yielded_additions 
    6335    @xml = Contact.new.to_xml do |xml|