routing-filter icon indicating copy to clipboard operation
routing-filter copied to clipboard

Rails 4: around_generate sometimes not being called

Open til opened this issue 11 years ago • 6 comments

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.

til avatar May 25 '13 17:05 til

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.

dark-panda avatar Jun 07 '13 20:06 dark-panda

+1

brega avatar Jan 20 '14 04:01 brega

+1

brett-bender avatar Jan 20 '14 18:01 brett-bender

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.

aripollak avatar Jul 18 '14 14:07 aripollak

:+1:

debreczeni avatar Oct 10 '14 13:10 debreczeni

related: https://github.com/rails/rails/issues/12178

alepore avatar Jan 22 '16 11:01 alepore