-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from rage-rb/router-compatibility
Improve Router compatibility
- Loading branch information
Showing
7 changed files
with
336 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
## | ||
# Support the `:controller` and `:action` options. | ||
# | ||
# @example | ||
# get :admins, controller: :users | ||
# @example | ||
# post :search, action: :index | ||
module Rage::Router::DSLPlugins::ControllerActionOptions | ||
%i(get post put patch delete).each do |action_name| | ||
define_method(action_name) do |*args, **kwargs| | ||
if args.length == 1 && !kwargs.has_key?(:to) && (kwargs.has_key?(:controller) || kwargs.has_key?(:action)) | ||
path = args[0] | ||
controller = kwargs.delete(:controller) || @controllers.last || raise(ArgumentError, "Could not derive the controller value from the route definitions") | ||
action = kwargs.delete(:action) || path.split("/").last | ||
end | ||
|
||
if controller && action | ||
kwargs[:to] = "#{controller}##{action}" | ||
super(path, **kwargs) | ||
else | ||
super(*args, **kwargs) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## | ||
# Support legacy URL helpers that use hashes instead of the `:to` keyword argument. | ||
# | ||
# @example | ||
# get "/photos/:id" => "photos#show" | ||
# @example | ||
# mount Sidekiq::Web => "/sidekiq" | ||
# @example | ||
# get "search" => :index | ||
# @example | ||
# get "admin_users" => "users" | ||
module Rage::Router::DSLPlugins::LegacyHashNotation | ||
%i(get post put patch delete).each do |action_name| | ||
define_method(action_name) do |*args, **kwargs| | ||
if args.empty? && !kwargs.empty? | ||
path, handler = kwargs.first | ||
|
||
to = if handler.is_a?(Symbol) | ||
raise ArgumentError, "Could not derive the controller value from the route definitions" if @controllers.empty? | ||
"#{@controllers.last}##{handler}" | ||
elsif handler.is_a?(String) && !handler.include?("#") | ||
"#{handler}##{path.split("/").last}" | ||
elsif handler.is_a?(String) | ||
handler | ||
end | ||
end | ||
|
||
if path && to | ||
options = kwargs.except(path).merge(to: to) | ||
super(path, **options) | ||
else | ||
super(*args, **kwargs) | ||
end | ||
end | ||
end | ||
|
||
def mount(*args, **kwargs) | ||
if args.empty? && !kwargs.empty? | ||
app, at = kwargs.first | ||
options = kwargs.except(app).merge(at: at) | ||
super(app, **options) | ||
else | ||
super(*args, **kwargs) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## | ||
# Support legacy root helpers that don't use the `:to` keyword argument. | ||
# | ||
# @example | ||
# root "photos#index" | ||
module Rage::Router::DSLPlugins::LegacyRootNotation | ||
def root(*args, **kwargs) | ||
if args.length == 1 && args[0].is_a?(String) && kwargs.empty? | ||
super(to: args[0]) | ||
else | ||
super(*args, **kwargs) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |*args, **kwargs| | ||
kwargs.delete(:as) | ||
super(*args, **kwargs) | ||
end | ||
end | ||
end |
Oops, something went wrong.