Skip to content

Commit

Permalink
Add the member route helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rsamoilov committed May 7, 2024
1 parent 4f3d70a commit 280e8b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/rage/router/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,27 @@ def collection(&block)
@path_prefixes = orig_path_prefixes
end

# Add a member route.
#
# @example Add a `photos/:id/preview` path instead of `photos/:photo_id/preview`
# resources :photos do
# member do
# get "preview"
# end
# end
def member(&block)
orig_path_prefixes = @path_prefixes

if (param_prefix = @path_prefixes.last)&.start_with?(":") && @controllers.any?
member_prefix = param_prefix.delete_prefix(":#{to_singular(@controllers.last)}_")
@path_prefixes = [*@path_prefixes[0...-1], ":#{member_prefix}"]
end

instance_eval &block

@path_prefixes = orig_path_prefixes
end

# Automatically create REST routes for a resource.
#
# @example Create five REST routes, all mapping to the `Photos` controller:
Expand Down
30 changes: 30 additions & 0 deletions spec/router/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,31 @@ def call(env)
end
end

it "correctly creates nested routes on member" do
expect(router).to receive(:on).with("GET", "/photos/:id", "photos#show", instance_of(Hash))
expect(router).to receive(:on).with("POST", "/photos/:id/like", "photos#like", instance_of(Hash))

dsl.draw do
resources :photos, only: :show do
member do
post :like
end
end
end
end

it "correctly creates nested routes on member with a custom param" do
expect(router).to receive(:on).with("POST", "/photos/:photo_uuid/like", "photos#like", instance_of(Hash))

dsl.draw do
resources :photos, only: [], param: :photo_uuid do
member do
post :like
end
end
end
end

it "correctly creates nested resources" do
expect(router).to receive(:on).with("GET", "/albums", "albums#index", instance_of(Hash))
expect(router).to receive(:on).with("POST", "/albums", "albums#create", instance_of(Hash))
Expand Down Expand Up @@ -606,6 +631,7 @@ def call(env)
expect(router).to receive(:on).with("DELETE", "/albums/:album_slug/my_photos/:id", "photos#destroy", instance_of(Hash))
expect(router).to receive(:on).with("POST", "/albums/:album_slug/my_photos/:photo_id/add_to_album", "photos#add_to_album", instance_of(Hash))
expect(router).to receive(:on).with("POST", "/albums/:album_slug/my_photos/like_all", "photo_likes#create", instance_of(Hash))
expect(router).to receive(:on).with("GET", "/albums/:album_slug/my_photos/:id/keywords", "photos#keywords", instance_of(Hash))

expect(router).to receive(:on).with("POST", "/albums/sort", "albums#sort", instance_of(Hash))
expect(router).to receive(:on).with("PATCH", "/albums/:album_slug/tag", "albums#tag", instance_of(Hash))
Expand All @@ -618,6 +644,10 @@ def call(env)
collection do
post "like_all", to: "photo_likes#create"
end

member do
get :keywords
end
end

collection do
Expand Down

0 comments on commit 280e8b9

Please sign in to comment.