Skip to content

Commit

Permalink
Fix nested attribute loading from anonymous classes under Ruby 1.9
Browse files Browse the repository at this point in the history
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 d98d012
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_resource/base.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/cases/base/load_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit d98d012

Please sign in to comment.