Skip to content

Commit

Permalink
Fix handling camelized argument in JSONAPI::Resource.resource_for
Browse files Browse the repository at this point in the history
(cherry picked from commit 6dc9e96)
  • Loading branch information
knapo authored and lgebhardt committed Oct 5, 2016
1 parent a85c1d0 commit 9192000
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9192000

Please sign in to comment.