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

Ticket #8566: handle_complex_xml_types_in_activeresource.diff

File handle_complex_xml_types_in_activeresource.diff, 3.1 kB (added by mmmultiworks, 2 years ago)

Handle Complex Types In ActiveResource

  • test/fixtures/shape.rb

    old new  
     1class Shape < ActiveResource::Base 
     2  self.site = "http://math.i:3000" 
     3end 
  • test/base_test.rb

    old new  
    22require "fixtures/person" 
    33require "fixtures/street_address" 
    44require "fixtures/beast" 
     5require "fixtures/shape" 
    56 
    67class BaseTest < Test::Unit::TestCase 
    78  def setup 
     
    262263    assert_equal "David", david.name 
    263264  end 
    264265 
     266  def test_find_one_complex 
     267    side_1    = { :id => 1, :length => 10 } 
     268    side_2    = { :id => 2, :length => 20 } 
     269    rectangle = { :id => 2, :name => 'square', :sides => [ side_1, side_2 ]}.to_xml(:root => 'shape') 
     270     
     271    ActiveResource::HttpMock.respond_to { |m| m.get "/shapes/2.xml", {}, rectangle } 
     272     
     273    shape = Shape.find(2) 
     274    assert_equal 2, shape.id 
     275    assert_equal 'square', shape.name 
     276    assert_equal 2, shape.sides.length 
     277    assert_equal side_1, shape.sides[0].attributes.symbolize_keys 
     278    assert_equal side_2, shape.sides[1].attributes.symbolize_keys 
     279  end 
     280 
     281  def test_find_all_complex 
     282    circle    = { :id => 1, :name => 'circle' } 
     283    square    = { :id => 2, :name => 'square' } 
     284    rectangle = { :id => 3, :name => 'rectangle' } 
     285    shapes    = { :circle => circle, :polygons => [square, rectangle] }.to_xml(:root => 'shapes') 
     286     
     287    ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes } 
     288     
     289    shapes = Shape.find(:all) 
     290    assert_equal 2, shapes.length 
     291    assert_equal circle,    shapes[0].attributes.symbolize_keys 
     292    assert_equal 2, shapes[1].length 
     293    assert_equal square,    shapes[1][0].attributes.symbolize_keys 
     294    assert_equal rectangle, shapes[1][1].attributes.symbolize_keys 
     295  end 
     296   
    265297  def test_save 
    266298    rick = Person.new 
    267299    assert_equal true, rick.save 
  • lib/active_resource/base.rb

    old new  
    184184        end 
    185185         
    186186        def instantiate_collection(collection, prefix_options = {}) 
    187           collection.collect! { |record| instantiate_record(record, prefix_options) } 
     187          case collection 
     188          when Array 
     189            collection.collect! { |record| instantiate_record(record, prefix_options) } 
     190          when Hash 
     191            objects = [] 
     192            collection.each_pair do |key, value| 
     193              case value 
     194              when Hash 
     195                objects << instantiate_record(value, prefix_options) 
     196              when Array 
     197                objects << [] 
     198                value.each do |e| 
     199                  objects[-1] << instantiate_record(e, prefix_options) 
     200                end 
     201              end 
     202            end 
     203            objects 
     204          end 
    188205        end 
    189206 
    190207        def instantiate_record(record, prefix_options = {})