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) |
|---|
-
test/fixtures/shape.rb
old new 1 class Shape < ActiveResource::Base 2 self.site = "http://math.i:3000" 3 end -
test/base_test.rb
old new 2 2 require "fixtures/person" 3 3 require "fixtures/street_address" 4 4 require "fixtures/beast" 5 require "fixtures/shape" 5 6 6 7 class BaseTest < Test::Unit::TestCase 7 8 def setup … … 262 263 assert_equal "David", david.name 263 264 end 264 265 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 265 297 def test_save 266 298 rick = Person.new 267 299 assert_equal true, rick.save -
lib/active_resource/base.rb
old new 184 184 end 185 185 186 186 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 188 205 end 189 206 190 207 def instantiate_record(record, prefix_options = {})