forked from svenfuchs/routing-filter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the routers to add the proper one for each rails version
Rails 3.0 and 3.1 use Rack::Mount, Rails 3.2 uses the new Journey Router
- Loading branch information
1 parent
ca19a40
commit f272403
Showing
3 changed files
with
65 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'journey/routes' | ||
require 'journey/router' | ||
|
||
Journey::Routes.class_eval do | ||
def filters | ||
@filters || RoutingFilter::Chain.new.tap { |f| @filters = f unless frozen? } | ||
end | ||
end | ||
|
||
Journey::Router.class_eval do | ||
def find_routes_with_filtering env | ||
path, filter_parameters = env['PATH_INFO'], {} | ||
|
||
@routes.filters.run(:around_recognize, path, env) do | ||
filter_parameters | ||
end | ||
|
||
find_routes_without_filtering(env).map do |match, parameters, route| | ||
[ match, parameters.merge(filter_parameters), route ] | ||
end | ||
end | ||
alias_method_chain :find_routes, :filtering | ||
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,37 @@ | ||
require 'action_dispatch' | ||
require 'rack/mount/route_set' | ||
require 'rack/mount/code_generation' | ||
|
||
Rack::Mount::RouteSet.class_eval do | ||
def filters | ||
@filters || RoutingFilter::Chain.new.tap { |f| @filters = f unless frozen? } | ||
end | ||
end | ||
|
||
# gah. so who's hoped monkeypatching optimized code wouldn't be necessary with rails 3 anymore? | ||
Rack::Mount::CodeGeneration.class_eval do | ||
def optimize_recognize_with_filtering! | ||
optimize_recognize_without_filtering! | ||
(class << self; self; end).class_eval do | ||
alias_method_chain :recognize, :filtering | ||
end | ||
end | ||
alias :optimize_recognize_without_filtering! :optimize_recognize! | ||
alias :optimize_recognize! :optimize_recognize_with_filtering! | ||
|
||
# note: if you overly and unnecessarily use blocks in your lowlevel libraries you make it fricking | ||
# hard for your users to hook in anywhere | ||
def recognize_with_filtering(request, &block) | ||
path, route, matches, params = request.env['PATH_INFO'], nil, nil, nil | ||
original_path = path.dup | ||
|
||
filters.run(:around_recognize, path, request.env) do | ||
route, matches, params = recognize_without_filtering(request) | ||
params || {} | ||
end | ||
|
||
request.env['PATH_INFO'] = original_path # hmm ... | ||
block.call(route, matches, params) if route | ||
end | ||
end | ||
|