Skip to content

Commit

Permalink
Fix alias_method_chain deprecation warnings in Rails 5
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesanchez committed Feb 27, 2016
1 parent 482bfd1 commit 517460d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
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)

0 comments on commit 517460d

Please sign in to comment.