Skip to content

Commit

Permalink
Extract the routers to add the proper one for each rails version
Browse files Browse the repository at this point in the history
Rails 3.0 and 3.1 use Rack::Mount, Rails 3.2 uses the new Journey
Router
  • Loading branch information
mjonuschat committed Dec 21, 2011
1 parent ca19a40 commit f272403
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
40 changes: 5 additions & 35 deletions lib/routing_filter/adapters/rails_3.rb
Expand Up @@ -39,39 +39,9 @@ def clear_with_filtering!
alias_method_chain :clear!, :filtering
end

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
case ActionPack::VERSION::MINOR
when 2
require 'routing_filter/adapters/routers/journey'
when 0,1
require 'routing_filter/adapters/routers/rack_mount'
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

23 changes: 23 additions & 0 deletions lib/routing_filter/adapters/routers/journey.rb
@@ -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
37 changes: 37 additions & 0 deletions lib/routing_filter/adapters/routers/rack_mount.rb
@@ -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

0 comments on commit f272403

Please sign in to comment.