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

Ticket #10162: noChildProcs.diff

File noChildProcs.diff, 1.7 kB (added by Catfish, 10 months ago)
  • test/xml_serialization_test.rb

    old new  
    172172    assert types.include?('StiPost') 
    173173  end 
    174174   
     175  def test_should_allow_custom_procs 
     176    proc = Proc.new do |options| 
     177      options[:builder].tag!('wasCustomized', true)       
     178    end 
     179    @xml = authors(:david).to_xml(:procs => [proc]) 
     180    assert_match %r{<wasCustomized>true</wasCustomized>}, @xml 
     181  end 
     182   
     183  def test_custom_procs_arent_applied_to_associations 
     184    author = authors(:david) 
     185    author_proc = Proc.new do |options| 
     186      options[:builder].label(author.label) 
     187    end 
     188    @xml = author.to_xml(:procs => [author_proc], :include => :posts) 
     189    assert_match Regexp.new("<label>#{author.label}</label>"), @xml 
     190     
     191    # find the xml for the posts association: 
     192    posts_xml = %r{<posts type="array">.*</posts>}m.match(@xml).to_s 
     193    # The posts xml shouldn't have be altered by the author proc: 
     194    assert_no_match Regexp.new("<label>#{author.label}</label>"), posts_xml 
     195  end 
     196   
     197   
    175198end 
  • lib/active_record/serializers/xml_serializer.rb

    old new  
    196196    end 
    197197 
    198198    def add_associations(association, records, opts) 
     199      # associations probably aren't interested in the top-level procs, strip them out: 
     200      opts = opts.except(:procs) 
    199201      if records.is_a?(Enumerable) 
    200202        tag = association.to_s 
    201203        tag = tag.dasherize if dasherize?