Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make routing-filter work with rails 3.2.0.rc1 #34

Merged
merged 2 commits into from
Dec 21, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/routing/filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'routing_filter'
40 changes: 5 additions & 35 deletions lib/routing_filter/adapters/rails_3.rb
Original file line number Diff line number Diff line change
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
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
37 changes: 37 additions & 0 deletions lib/routing_filter/adapters/routers/rack_mount.rb
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