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

Changeset 7156

Show
Ignore:
Timestamp:
06/30/07 03:31:48 (3 years ago)
Author:
nzkoz
Message:

Don't call unsupported methods on associated objects when using :include, :method with to_xml [manfred, jwilger] Closes #7307

Files:

Legend:

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

    r7137 r7156  
    11*SVN* 
     2 
     3* Don't call unsupported methods on associated objects when using :include, :method with to_xml #7307, [manfred, jwilger] 
    24 
    35* Define collection singular ids method for has_many :through associations.  #8763 [lifofifo] 
  • trunk/activerecord/lib/active_record/xml_serialization.rb

    r7144 r7156  
    179179 
    180180    def serializable_method_attributes 
    181       Array(options[:methods]).collect { |name| MethodAttribute.new(name.to_s, @record) } 
    182     end 
    183  
     181      Array(options[:methods]).inject([]) do |method_attributes, name| 
     182        method_attributes << MethodAttribute.new(name.to_s, @record) if @record.respond_to?(name.to_s) 
     183        method_attributes 
     184      end 
     185    end 
    184186 
    185187    def add_attributes 
  • trunk/activerecord/test/fixtures/author.rb

    r5305 r7156  
    6868  end 
    6969 
     70  def label 
     71    "#{id}-#{name}" 
     72  end 
     73 
    7074  private 
    7175    def log_before_adding(object) 
  • trunk/activerecord/test/xml_serialization_test.rb

    r7144 r7156  
    22require 'fixtures/post' 
    33require 'fixtures/author' 
     4require 'fixtures/tagging' 
    45 
    56class Contact < ActiveRecord::Base 
     
    143144    assert_equal first_xml_size, second_xml_size 
    144145  end 
    145    
    146146 
    147147  def test_include_uses_association_name 
    148     xml = authors(:david).to_xml :include=>:hello_posts, :indent=>
     148    xml = authors(:david).to_xml :include=>:hello_posts, :indent =>
    149149    assert_match %r{<hello-posts type="array">}, xml 
    150150    assert_match %r{<post>}, xml 
    151151    assert_match %r{<sti-post>}, xml 
    152152  end 
     153   
     154  def test_methods_are_called_on_object 
     155    xml = authors(:david).to_xml :methods => :label, :indent => 0 
     156    assert_match %r{<label>.*</label>}, xml 
     157  end 
     158   
     159  def test_should_not_call_methods_on_associations_that_dont_respond 
     160    xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2 
     161    assert !authors(:david).hello_posts.first.respond_to?(:label) 
     162    assert_match %r{^  <label>.*</label>}, xml 
     163    assert_no_match %r{^      <label>}, xml 
     164  end 
    153165end