From 9192000e5f7a2f8fa26414158406d74146476071 Mon Sep 17 00:00:00 2001 From: Krzysztof Knapik Date: Wed, 14 Sep 2016 03:45:22 -0400 Subject: [PATCH] Fix handling camelized argument in `JSONAPI::Resource.resource_for` (cherry picked from commit 6dc9e96) --- lib/jsonapi/resource.rb | 1 + test/unit/resource/resource_test.rb | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index e7f68c4c0..0b51d07d8 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -423,6 +423,7 @@ def inherited(subclass) end def resource_for(type) + type = type.underscore type_with_module = type.include?('/') ? type : module_path + type resource_name = _resource_name_from_type(type_with_module) diff --git a/test/unit/resource/resource_test.rb b/test/unit/resource/resource_test.rb index e025d05a0..1b003c2a9 100644 --- a/test/unit/resource/resource_test.rb +++ b/test/unit/resource/resource_test.rb @@ -125,19 +125,22 @@ def test_resource_for_root_resource end end - def test_resource_for_with_namespaced_paths + def test_resource_for_resource_does_not_exist_at_root + assert_raises NameError do + ArticleResource.resource_for('related') + end + end + + def test_resource_for_with_underscored_namespaced_paths assert_equal(JSONAPI::Resource.resource_for('my_module/related'), MyModule::RelatedResource) assert_equal(PostResource.resource_for('my_module/related'), MyModule::RelatedResource) assert_equal(MyModule::MyNamespacedResource.resource_for('my_module/related'), MyModule::RelatedResource) end - def test_resource_for_resource_does_not_exist_at_root - assert_raises NameError do - ArticleResource.resource_for('related') - end - assert_raises NameError do - JSONAPI::Resource.resource_for('related') - end + def test_resource_for_with_camelized_namespaced_paths + assert_equal(JSONAPI::Resource.resource_for('MyModule::Related'), MyModule::RelatedResource) + assert_equal(PostResource.resource_for('MyModule::Related'), MyModule::RelatedResource) + assert_equal(MyModule::MyNamespacedResource.resource_for('MyModule::Related'), MyModule::RelatedResource) end def test_resource_for_namespaced_resource