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

Ticket #7307: dont_call_unsupported_methods_on_included_associations.diff

File dont_call_unsupported_methods_on_included_associations.diff, 2.4 kB (added by manfred, 1 year ago)

My solution for this problem.

  • test/xml_serialization_test.rb

    old new  
    11require 'abstract_unit' 
    22require 'fixtures/post' 
    33require 'fixtures/author' 
     4require 'fixtures/tagging' 
    45 
    56class Contact < ActiveRecord::Base 
    67  # mock out self.columns so no pesky db is needed for these tests 
     
    143144    assert_equal first_xml_size, second_xml_size 
    144145  end 
    145146   
    146  
    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>}, 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 
  • test/fixtures/author.rb

    old new  
    6767    @post_log = [] 
    6868  end 
    6969 
     70  def label 
     71    "#{id}-#{name}" 
     72  end 
     73 
    7074  private 
    7175    def log_before_adding(object) 
    7276      @post_log << "before_adding#{object.id}" 
  • lib/active_record/xml_serialization.rb

    old new  
    178178    end 
    179179 
    180180    def serializable_method_attributes 
    181       Array(options[:methods]).collect { |name| MethodAttribute.new(name.to_s, @record) } 
     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 
    182185    end 
    183186 
    184  
    185187    def add_attributes 
    186188      (serializable_attributes + serializable_method_attributes).each do |attribute| 
    187189        add_tag(attribute)