Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes parsing deep nested resources from XML. [#380 state:resolved]

  • Loading branch information...
commit 225065709c43dacd57e0904aef2075024ccf2744 1 parent 16a9787
Luis Hurtado authored jeremy committed
View
6 activeresource/lib/active_resource/base.rb
@@ -988,7 +988,11 @@ def find_or_create_resource_for(name)
self.class.const_get(resource_name)
end
rescue NameError
- resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
+ if self.class.const_defined?(resource_name)
+ resource = self.class.const_get(resource_name)
+ else
+ resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
+ end
resource.prefix = self.class.prefix
resource.site = self.class.site
resource
View
48 activeresource/test/base_test.rb
@@ -1,5 +1,6 @@
require 'abstract_unit'
require "fixtures/person"
+require "fixtures/customer"
require "fixtures/street_address"
require "fixtures/beast"
@@ -15,6 +16,37 @@ def setup
@people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
@addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses')
+ # - deep nested resource -
+ # - Luis (Customer)
+ # - JK (Customer::Friend)
+ # - Mateo (Customer::Friend::Brother)
+ # - Edith (Customer::Friend::Brother::Child)
+ # - Martha (Customer::Friend::Brother::Child)
+ # - Felipe (Customer::Friend::Brother)
+ # - Bryan (Customer::Friend::Brother::Child)
+ # - Luke (Customer::Friend::Brother::Child)
+ # - Eduardo (Customer::Friend)
+ # - Sebas (Customer::Friend::Brother)
+ # - Andres (Customer::Friend::Brother::Child)
+ # - Jorge (Customer::Friend::Brother::Child)
+ # - Elsa (Customer::Friend::Brother)
+ # - Natacha (Customer::Friend::Brother::Child)
+ # - Milena (Customer::Friend::Brother)
+ #
+ @luis = {:id => 1, :name => 'Luis',
+ :friends => [{:name => 'JK',
+ :brothers => [{:name => 'Mateo',
+ :children => [{:name => 'Edith'},{:name => 'Martha'}]},
+ {:name => 'Felipe',
+ :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]},
+ {:name => 'Eduardo',
+ :brothers => [{:name => 'Sebas',
+ :children => [{:name => 'Andres'},{:name => 'Jorge'}]},
+ {:name => 'Elsa',
+ :children => [{:name => 'Natacha'}]},
+ {:name => 'Milena',
+ :children => []}]}]}.to_xml(:root => 'customer')
+
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, @matz
mock.get "/people/2.xml", {}, @david
@@ -46,6 +78,8 @@ def setup
mock.head "/people/1/addresses/2.xml", {}, nil, 404
mock.head "/people/2/addresses/1.xml", {}, nil, 404
mock.head "/people/Greg/addresses/1.xml", {}, nil, 200
+ # customer
+ mock.get "/customers/1.xml", {}, @luis
end
Person.user = nil
@@ -788,4 +822,18 @@ def test_to_param_quacks_like_active_record
matz = Person.find(1)
assert_equal '1', matz.to_param
end
+
+ def test_parse_deep_nested_resources
+ luis = Customer.find(1)
+ assert_kind_of Customer, luis
+ luis.friends.each do |friend|
+ assert_kind_of Customer::Friend, friend
+ friend.brothers.each do |brother|
+ assert_kind_of Customer::Friend::Brother, brother
+ brother.children.each do |child|
+ assert_kind_of Customer::Friend::Brother::Child, child
+ end
+ end
+ end
+ end
end
View
3  activeresource/test/fixtures/customer.rb
@@ -0,0 +1,3 @@
+class Customer < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.