Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Do not shadow helpers with same method but more params #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/grape-route-helpers/all_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module GrapeRouteHelpers
module AllRoutes
def decorated_routes
# memoize so that construction of decorated routes happens once
@decorated_routes ||= all_routes.map { |r| DecoratedRoute.new(r) }
@decorated_routes ||= all_routes
.map { |r| DecoratedRoute.new(r) }
.sort_by { |r| -r.dynamic_path_segments.count }
end

def all_routes
Expand Down
8 changes: 8 additions & 0 deletions spec/grape_route_helpers/named_route_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@
index_path = api_v1_cats_path
expect(index_path).to eq('/api/v1/cats.json')
end

it 'does not get shadowed by another route with less segments' do
show_path = api_v1_cats_owners_path('id' => 1)
expect(show_path).to eq('/api/v1/cats/1/owners.json')

show_path = api_v1_cats_owners_path('id' => 1, 'owner_id' => 1)
expect(show_path).to eq('/api/v1/cats/1/owners/1.json')
end
end

context 'when query params are passed in' do
Expand Down
8 changes: 8 additions & 0 deletions spec/support/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class API < Grape::API
'cat'
end
end

get ':id/owners' do
%w(owner1 owner2)
end

get ':id/owners/:owner_id' do
'owner'
end
end

route :any, '*path' do
Expand Down