Skip to content

Commit

Permalink
Add the controller helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rsamoilov committed May 8, 2024
1 parent 25ef70b commit 298a18f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/rage/router/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ def defaults(defaults, &block)
@defaults.pop
end

# Scopes routes to a specific controller.
#
# @example
# controller "photos" do
# post "like"
# post "dislike"
# end
def controller(controller, &block)
@controllers << controller
instance_eval &block
@controllers.pop
end

# Add a route to the collection.
#
# @example Add a `photos/search` path instead of `photos/:photo_id/search`
Expand Down
21 changes: 21 additions & 0 deletions spec/router/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@
end
end

context "with controller scope" do
it "correctly adds handlers" do
expect(router).to receive(:on).with("POST", "/api/v1/like", "api/v1/photos#like", instance_of(Hash))
expect(router).to receive(:on).with("POST", "/api/v1/dislike", "api/v1/photos#dislike", instance_of(Hash))
expect(router).to receive(:on).with("GET", "/api/v1/index", "api/v1/all_photos#index", instance_of(Hash))

dsl.draw do
namespace "api/v1" do
scope controller: "photos" do
post "like"
post "dislike"
end

controller "all_photos" do
get "index"
end
end
end
end
end

context "with root helper inside a scope" do
it "correctly adds handlers" do
expect(router).to receive(:on).with("GET", "/api/v1/internal", "api/test#index", a_hash_including(constraints: {}))
Expand Down

0 comments on commit 298a18f

Please sign in to comment.