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

Changeset 6519

Show
Ignore:
Timestamp:
04/13/07 01:26:17 (2 years ago)
Author:
david
Message:

Added yielding of Builder instance for ActiveRecord::Base#to_xml calls [DHH]

Files:

Legend:

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

    r6470 r6519  
    11*SVN* 
     2 
     3* Added yielding of Builder instance for ActiveRecord::Base#to_xml calls [DHH] 
    24 
    35* Small additions and fixes for ActiveRecord documentation.  Closes #7342 [jeremymcanally] 
  • trunk/activerecord/lib/active_record/xml_serialization.rb

    r6444 r6519  
    9191    #   </firm> 
    9292    # 
     93    # Alternatively, you can also just yield the builder object as part of the to_xml call: 
     94    # 
     95    #   firm.to_xml do |xml| 
     96    #     xml.creator do 
     97    #       xml.first_name "David" 
     98    #       xml.last_name "Heinemeier Hansson" 
     99    #     end 
     100    #   end 
     101    # 
     102    #   <firm> 
     103    #     # ... normal attributes as shown above ... 
     104    #     <creator> 
     105    #       <first_name>David</first_name> 
     106    #       <last_name>Heinemeier Hansson</last_name> 
     107    #     </creator> 
     108    #   </firm> 
     109    # 
    93110    # You may override the to_xml method in your ActiveRecord::Base 
    94111    # subclasses if you need to.  The general form of doing this is 
     
    104121    #     end 
    105122    #   end 
    106     def to_xml(options = {}) 
    107       XmlSerializer.new(self, options).to_s 
     123    def to_xml(options = {}, &block) 
     124      serializer = XmlSerializer.new(self, options) 
     125      block_given? ? serializer.to_s(&block) : serializer.to_s 
    108126    end 
    109127  end 
     
    232250        add_includes 
    233251        add_procs 
     252        yield builder if block_given? 
    234253      end 
    235254    end         
  • trunk/activerecord/test/xml_serialization_test.rb

    r6444 r6519  
    5656    assert_no_match %r{<age},     @xml 
    5757    assert_match %r{<created-at}, @xml 
     58  end 
     59   
     60  def test_should_include_yielded_additions 
     61    @xml = Contact.new.to_xml do |xml| 
     62      xml.creator "David" 
     63    end 
     64 
     65    assert_match %r{<creator>David</creator>}, @xml 
    5866  end 
    5967end