Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails FilterChain internals - how to make it reloadable?
    primarykey
    data
    text
    <p>I'd like to have a more dynamic way of working with filter chains in Ruby on Rails 2.3.2.</p> <p>At the moment, the filter chain is built as the classes are loaded. I have a very particular need for the filter chain to be rebuilt at some point in time after the classes have loaded.</p> <p>This is to do with a Rails project (Spree, Radiant may also be affected I'm guessing) that uses extensions which can do things like this <strong>after</strong> the filter chain has been built for MyController:</p> <pre><code>MyController.class_eval do before_filter :my_filter_method end ApplicationController.class_eval do before_filter :app_filter_method end </code></pre> <p>The problem here is that the <code>app_filter_method</code> filter will <strong>not</strong> be added to the MyController filter chain. This is because the filter chain of MyController is built from an earlier copy of the filter chain of ApplicationController. This copy of the filter chain of ApplicationController does <strong>not</strong> yet have the <code>app_filter_method</code> filter applied to it.</p> <p>I can think of 2 places so far that the rebuilding of the FilterChain could happen in:</p> <p>1) Each time it is called on MyController.filter_chain</p> <p>2) Reloadable on demand. So MyController.reload_filter_chain will rebuild the filter chain, using the filters from MyController's subclasses in the chain.</p> <p>Perhaps specifying a subclass of FilterChain like ReloadableFilterChain that builds the filter chain for each request might do it - thoughts?</p> <p>Here are links to the source of filters.rb on GitHub, for Ruby on Rails 2.3.2:</p> <p><a href="http://github.com/rails/rails/blob/73fc42cc0b5e94541480032c2941a50edd4080c2/actionpack/lib/action_controller/filters.rb#L573" rel="nofollow noreferrer">The filter_chain method (line 573)</a></p> <p><a href="http://github.com/rails/rails/blob/73fc42cc0b5e94541480032c2941a50edd4080c2/actionpack/lib/action_controller/filters.rb#L10" rel="nofollow noreferrer">The FilterChain class (line 10)</a></p> <p>I was hoping some of you here might have some insight or advice on how to do this.</p> <p>Any help much appreciated.</p> <p>Eliot</p> <p>More detail as requested:</p> <p>Spree is a rails app that uses extensions to modify behaviour.</p> <p>Sometimes an extension is used to add a filter to a controller (such as in the example code in the question). One example use is the static content extension.</p> <p>The static content extension allows you to show HTML pages stored in the database to be shown for any request path you specify. For example, you could show one of these HTML pages instead of the default content Spree shows for the /products request path.</p> <p>The static content extension filters all requests and checks the path. If the path matches one of the pages in the db, then the filter renders the page.</p> <p>The static content extension declares its filter like so:</p> <pre><code>Spree::BaseController.class_eval do before_filter :render_page_if_exists def render_page_if_exists ... end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload