routing-filter
routing-filter copied to clipboard
Rails 4: around_generate sometimes not being called
With the following setup on rails 4, the around_generate method is not called for url helper methods such as products_path
. It is being called as expected for an equivalent url_for
call though.
module RoutingFilter
class Translate < Filter
...
def around_generate(params, &block)
yield.tap do |result|
puts 'should be called'
end
end
end
end
MyApp::Application.routes.draw do
filter :translate
resources :products
end
products_path # => around_generate was not called
url_for(controller: 'products', action: 'index') # => around_generate was called
It seems to be related to optimized url helper methods, and the following monkey patch makes RoutingFilter behave as expected:
ActionDispatch::Routing::RouteSet::NamedRouteCollection::UrlHelper.class_eval do
def self.optimize_helper?(route)
false
end
end
I don't understand the rails routing internals enough to know what's going on here, but hope there's a better solution than this monkey patch.
I am seeing this behaviour as well. This appears to occur OptimizedUrlHelper
calls ActionDispatch::Http::URL.url_for
directly rather than through ActionDispatch::Routing::RouteSet#url_for
, which in turn the generate
method that routing-filter hooks into. OptimizedUrlHelper
bypasses that generate
call altogether and skips right to ActionDispatch::Http::URL.url_for
.
+1
+1
Two other options:
- If you populate
default_url_options
, the optimized url helper is not used. - You can define
optimize_routes_generation?
on your controller(s) to decide dynamically whether or not the routing filter can be skipped.
:+1:
related: https://github.com/rails/rails/issues/12178