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

Ticket #8566: handle_complex_xml_types_in_activeresource_updated.diff

File handle_complex_xml_types_in_activeresource_updated.diff, 4.9 kB (added by mmmultiworks, 2 years ago)
  • 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 
     
    266267    assert_equal "David", david.name 
    267268  end 
    268269 
     270  def test_find_one_complex 
     271    side_1    = { :id => 1, :length => 10 } 
     272    side_2    = { :id => 2, :length => 20 } 
     273    rectangle = { :id => 2, :name => 'square', :sides => [ side_1, side_2 ]}.to_xml(:root => 'shape') 
     274     
     275    ActiveResource::HttpMock.respond_to { |m| m.get "/shapes/2.xml", {}, rectangle } 
     276     
     277    shape = Shape.find(2) 
     278    assert_equal 2, shape.id 
     279    assert_equal 'square', shape.name 
     280    assert_equal 2, shape.sides.length 
     281    assert_equal side_1, shape.sides[0].attributes.symbolize_keys 
     282    assert_equal side_2, shape.sides[1].attributes.symbolize_keys 
     283  end 
     284 
     285  def test_find_one_complex_from_xml 
     286    shapes_xml = "<shapes><polygons><square>blue</square><square>gold</square></polygons></shapes>" 
     287     
     288    ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes_xml } 
     289     
     290    shapes = Shape.find(:all) 
     291    assert_equal 1, shapes.length 
     292    assert_kind_of Array, shapes.first.square 
     293    assert_equal 2, shapes.first.square.length 
     294    assert shapes.first.square.include?('blue') 
     295    assert shapes.first.square.include?('gold') 
     296  end 
     297 
     298  def test_find_all_complex 
     299    circle    = { :id => 1, :name => 'circle' } 
     300    square    = { :id => 2, :name => 'square' } 
     301    rectangle = { :id => 3, :name => 'rectangle' } 
     302    shapes    = { :circle => circle, :polygons => [square, rectangle] }.to_xml(:root => 'shapes') 
     303     
     304    ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes } 
     305     
     306    shapes = Shape.find(:all) 
     307    assert_equal 2, shapes.length 
     308    assert_equal circle,    shapes[0].attributes.symbolize_keys 
     309    assert_equal 2, shapes[1].length 
     310    assert_equal square,    shapes[1][0].attributes.symbolize_keys 
     311    assert_equal rectangle, shapes[1][1].attributes.symbolize_keys 
     312  end 
     313 
     314  def test_find_all_complex_from_xml 
     315    shapes_xml = "<shapes><polygons><square>blue</square><square>gold</square></polygons><circles><round>big</round></circles></shapes>" 
     316     
     317    ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes_xml } 
     318     
     319    shapes = Shape.find(:all) 
     320    assert_equal 2, shapes.length 
     321    assert_kind_of String, shapes[0].round 
     322    assert_equal 'big', shapes[0].round 
     323    assert_kind_of Array, shapes[1].square 
     324    assert shapes[1].square.include?('blue') 
     325    assert shapes[1].square.include?('gold') 
     326  end 
     327   
    269328  def test_save 
    270329    rick = Person.new 
    271330    assert_equal true, rick.save 
  • lib/active_resource/base.rb

    old new  
    436436        end 
    437437         
    438438        def instantiate_collection(collection, prefix_options = {}) 
    439           collection.collect! { |record| instantiate_record(record, prefix_options) } 
     439          case collection 
     440          when Array 
     441            collection.collect! { |record| instantiate_record(record, prefix_options) } 
     442          when Hash 
     443            objects = [] 
     444            collection.each_pair do |key, value| 
     445              case value 
     446              when Hash 
     447                case value.values.first 
     448                when Array 
     449                  objects << instantiate_record(value, prefix_options) 
     450                else 
     451                  objects << instantiate_record(value, prefix_options) 
     452                end 
     453              when Array 
     454                objects << [] 
     455                value.each do |e| 
     456                  objects[-1] << instantiate_record(e, prefix_options) 
     457                end 
     458              end 
     459            end 
     460            objects 
     461          end 
    440462        end 
    441463 
    442464        def instantiate_record(record, prefix_options = {}) 
     
    722744        @attributes[key.to_s] = 
    723745          case value 
    724746            when Array 
    725               resource = find_or_create_resource_for_collection(key) 
    726               value.map { |attrs| resource.new(attrs) } 
     747              if value.first.is_a?(Hash) 
     748                resource = find_or_create_resource_for_collection(key) 
     749                value.map { |attrs| resource.new(attrs) } 
     750              else 
     751                value.dup rescue value 
     752              end 
    727753            when Hash 
    728754              resource = find_or_create_resource_for(key) 
    729755              resource.new(value)