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

Feature/rails 5 #60

Merged
merged 2 commits into from
Feb 27, 2016
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
15 changes: 8 additions & 7 deletions lib/routing_filter/adapters/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def filter(*args)
end
end

ActionDispatch::Routing::RouteSet.class_eval do
module ActionDispatchRoutingRouteSetWithFiltering
def filters
@set.filters if @set
end
Expand All @@ -22,23 +22,24 @@ def add_filters(*names)
names.each { |name| filters.unshift(RoutingFilter.build(name, options)) }
end

def generate_with_filtering(route_key, options, recall = {})
def generate(route_key, options, recall = {})
options = options.symbolize_keys

# `around_generate` is destructive method and it breaks url. To avoid this, `dup` is required.
filters.run(:around_generate, options, &lambda{
generate_without_filtering(route_key, options, recall).map(&:dup)
super(route_key, options, recall).map(&:dup)
})
end
alias_method_chain :generate, :filtering

def clear_with_filtering!
def clear!
filters.clear if filters
clear_without_filtering!
super
end
alias_method_chain :clear!, :filtering
end

ActionDispatch::Routing::RouteSet.send(:prepend, ActionDispatchRoutingRouteSetWithFiltering)


ActionDispatch::Journey::Routes.class_eval do
def filters
@filters || RoutingFilter::Chain.new.tap { |f| @filters = f unless frozen? }
Expand Down
9 changes: 5 additions & 4 deletions lib/routing_filter/adapters/routers/journey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ def filters
end
end

ActionDispatch::Journey::Router.class_eval do
def find_routes_with_filtering(env)
module ActionDispatchJourneyRouterWithFiltering
def find_routes(env)
path = env.is_a?(Hash) ? env['PATH_INFO'] : env.path_info
filter_parameters = {}
original_path = path.dup
Expand All @@ -14,7 +14,7 @@ def find_routes_with_filtering(env)
filter_parameters
end

find_routes_without_filtering(env).map do |match, parameters, route|
super(env).map do |match, parameters, route|
[ match, parameters.merge(filter_parameters), route ]
end.tap do |match, parameters, route|
# restore the original path
Expand All @@ -25,5 +25,6 @@ def find_routes_with_filtering(env)
end
end
end
alias_method_chain :find_routes, :filtering
end

ActionDispatch::Journey::Router.send(:prepend, ActionDispatchJourneyRouterWithFiltering)
8 changes: 5 additions & 3 deletions routing-filter.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
$:.unshift File.expand_path('../lib', __FILE__)
require 'routing_filter/version'

rails_version = ['>= 4.2', '< 5.1']

Gem::Specification.new do |s|
s.name = "routing-filter"
s.version = RoutingFilter::VERSION
Expand All @@ -15,11 +17,11 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.rubyforge_project = '[none]'

s.add_dependency 'actionpack', '~> 4.2'
s.add_dependency 'activesupport', '~> 4.2'
s.add_dependency 'actionpack', rails_version
s.add_dependency 'activesupport', rails_version

s.add_development_dependency 'i18n'
s.add_development_dependency 'test_declarative'
s.add_development_dependency 'rack-test', '~> 0.6.2'
s.add_development_dependency 'rails', '~> 4.2'
s.add_development_dependency 'rails', rails_version
end