Skip to content

Commit

Permalink
This is a minimal fix for the parent class issue in the case of a nam…
Browse files Browse the repository at this point in the history
…espaced controller and a non-namespaced model. Deliberately makes the minimal change to existing code to address the issue.
  • Loading branch information
petergoldstein committed Sep 29, 2014
1 parent 38009de commit 159670e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/inherited_resources/class_methods.rb
Expand Up @@ -182,8 +182,9 @@ def belongs_to(*symbols, &block)

klass = model_name
while namespace != ''
klass = "#{namespace}::#{model_name}"
if klass.safe_constantize
new_klass = "#{namespace}::#{model_name}"
if new_klass.safe_constantize
klass = new_klass
break
else
namespace = namespace.deconstantize
Expand Down
5 changes: 5 additions & 0 deletions test/class_methods_test.rb
Expand Up @@ -148,6 +148,11 @@ def test_belongs_to_for_namespaced_controller_and_namespaced_model_fetches_model
assert_equal Library::Category, Library::SubcategoriesController.resources_configuration[:category][:parent_class]
end

def test_belongs_to_for_namespaced_controller_and_non_namespaced_model_sets_parent_class_properly
Library::SubcategoriesController.send(:belongs_to, :book)
assert_equal Book, Library::SubcategoriesController.resources_configuration[:book][:parent_class]
end

def test_belongs_to_without_namespace_sets_parent_class_properly
FoldersController.send(:belongs_to, :book)
assert_equal Book, FoldersController.resources_configuration[:book][:parent_class]
Expand Down

0 comments on commit 159670e

Please sign in to comment.