Skip to content

Commit

Permalink
Support the as option
Browse files Browse the repository at this point in the history
  • Loading branch information
rsamoilov committed Jul 10, 2024
1 parent 3226c02 commit 50b6966
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rage/router/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "dsl_plugins/legacy_hash_notation"
require_relative "dsl_plugins/legacy_root_notation"
require_relative "dsl_plugins/named_route_helpers"

class Rage::Router::DSL
def initialize(router)
Expand Down Expand Up @@ -50,6 +51,7 @@ def draw(&block)
# resources :posts
# end
class Handler
prepend Rage::Router::DSLPlugins::NamedRouteHelpers
prepend Rage::Router::DSLPlugins::LegacyHashNotation
prepend Rage::Router::DSLPlugins::LegacyRootNotation

Expand Down
13 changes: 13 additions & 0 deletions lib/rage/router/dsl_plugins/named_route_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
##
# Support the `as` option. As Rage currently doesn't generate named route helpers, we simply ignore it.
#
# @example
# get "/photos/:id", to: "photos#show", as: :user_photos
module Rage::Router::DSLPlugins::NamedRouteHelpers
%i(get post put patch delete).each do |action_name|
define_method(action_name) do |path, **options|
options.delete(:as)
super(path, **options)
end
end
end
12 changes: 12 additions & 0 deletions spec/router/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,16 @@ def call(env)
end
end
end

context "with the `as` option" do
it "correctly adds get handlers" do
expect(router).to receive(:on).with("GET", "/test", "test#index", a_hash_including(constraints: {}))
dsl.draw { get("/test", to: "test#index", as: :test_123) }
end

it "correctly adds legacy get handlers" do
expect(router).to receive(:on).with("GET", "/test", "test#index", a_hash_including(constraints: {}))
dsl.draw { get("/test" => "test#index", as: :test_123) }
end
end
end

0 comments on commit 50b6966

Please sign in to comment.