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

Ticket #8798: ARES_test_and_fix_for_undefined_method_collect_on_hash_error.patch

File ARES_test_and_fix_for_undefined_method_collect_on_hash_error.patch, 3.6 kB (added by JasonRoelofs, 2 years ago)

This is the most recent fix and test of this bug.

  • lib/active_resource/base.rb

    old new  
    462462        end 
    463463         
    464464        def instantiate_collection(collection, prefix_options = {}) 
    465           collection.collect! { |record| instantiate_record(record, prefix_options) } 
     465          if collection.is_a?(Hash) && collection.size == 1 
     466            value = collection.values.first 
     467            if value.is_a?(Array) 
     468              value.collect! { |record| instantiate_record(record, prefix_options) } 
     469            else 
     470              [ instantiate_record(value, prefix_options) ] 
     471            end 
     472          elsif collection.is_a?(Hash) 
     473            instantiate_record(collection, prefix_options) 
     474          else 
     475            collection.collect! { |record| instantiate_record(record, prefix_options) } 
     476          end 
    466477        end 
    467478 
    468479        def instantiate_record(record, prefix_options = {}) 
  • test/base_test.rb

    old new  
    1313    @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') 
    1414    @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') 
    1515    @addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses') 
    16      
     16 
    1717    ActiveResource::HttpMock.respond_to do |mock| 
    1818      mock.get    "/people/1.xml",             {}, @matz 
    1919      mock.get    "/people/2.xml",             {}, @david 
     
    441441    matz = Person.find(1) 
    442442    assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n  <name>Matz</name>\n  <id type=\"integer\">1</id>\n</person>\n", matz.to_xml 
    443443  end 
     444 
     445  def test_unspecified_collection_types 
     446  # Tests and data to prove that we've fixed the "undefined method 'collect!' for Hash" problem 
     447    @people_xml = <<XML 
     448<?xml version="1.0" encoding="UTF-8"?> 
     449<people> 
     450  <person> 
     451    <name>Matz</name> 
     452    <id type="integer">1</id> 
     453  </person> 
     454  <person> 
     455    <name>David</name> 
     456    <id type="integer">2</id> 
     457  </person> 
     458</people> 
     459XML 
     460 
     461    @people_david_xml = <<XML 
     462<?xml version="1.0" encoding="UTF-8"?> 
     463<people> 
     464  <person> 
     465    <name>David</name> 
     466    <id type="integer">2</id> 
     467  </person> 
     468</people> 
     469XML 
     470 
     471    @problematic = <<XML 
     472<?xml version="1.0" encoding="UTF-8"?>  
     473<account> 
     474  <id type="integer">1</id>  
     475  <account-number>1234</account-number>  
     476  <name>Checking</name> 
     477  <txactions> 
     478    <txaction> 
     479      <date>2007-05-03</date>  
     480      <amount>-89.72</amount>  
     481      <merchant> 
     482        <name>Berkeley Bowl</name> <rating>Fan</rating> 
     483      </merchant> 
     484    </txaction> 
     485  </txactions> 
     486</account>  
     487XML 
     488 
     489    ActiveResource::HttpMock.respond_to { |m|  
     490      m.get "/firms/1/people.xml", {}, @people_xml  
     491      m.get "/partners/1/people.xml", {}, @people_david_xml 
     492      m.get "/problems/1/people.xml", {}, @problematic 
     493    } 
     494 
     495    people = Person.find(:all, :from => "/firms/1/people.xml") 
     496     
     497    assert_equal 2, people.size 
     498    assert_kind_of Person, people.first 
     499    assert_equal "Matz", people.first.name 
     500    assert_equal "David", people.last.name 
     501 
     502    people = Person.find(:all, :from => "/partners/1/people.xml") 
     503    assert_equal 1, people.size 
     504    assert_equal "David", people.first.name 
     505 
     506    got = Person.find(:all, :from => "/problems/1/people.xml") 
     507    assert_equal "Checking", got.name 
     508    assert_equal "-89.72", got.txactions.txaction.amount 
     509  end 
    444510end