Permalink
Browse files

Fix nested attribute loading from anonymous classes under Ruby 1.9

In Ruby 1.8, anonymous classes have an empty string for a name. In Ruby 1.9, name is nil. This causes an error when loading nested attributes from an anonymous class as we determine its namespace. Stringify the name before we try to split it.
  • Loading branch information...
packagethief committed Sep 18, 2012
1 parent 720f9e5 commit d98d0124c62ba8ab2a9cd6ed71e916bfbe677888
Showing with 7 additions and 1 deletion.
  1. +1 −1 lib/active_resource/base.rb
  2. +6 −0 test/cases/base/load_test.rb
@@ -1490,7 +1490,7 @@ def find_or_create_resource_for(name)
if self.class.const_defined?(*const_args)
self.class.const_get(*const_args)
else
- ancestors = self.class.name.split("::")
+ ancestors = self.class.name.to_s.split("::")
if ancestors.size > 1
find_or_create_resource_in_modules(resource_name, ancestors)
else
@@ -108,6 +108,12 @@ def test_load_one_with_unknown_resource
assert_equal @first_address.values.first.stringify_keys, address.attributes
end
+ def test_load_one_with_unknown_resource_from_anonymous_subclass
+ subclass = Class.new(Person).tap { |c| c.element_name = 'person' }
+ address = silence_warnings { subclass.new.load(@first_address).address }
+ assert_kind_of subclass::Address, address
+ end
+
def test_load_collection_with_existing_resource
addresses = @person.load(@addresses_from_json).street_addresses
assert_kind_of Array, addresses

0 comments on commit d98d012

Please sign in to comment.