Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to extend an 'unloadable' Rails plugin?
    text
    copied!<p>I'm trying to write a plugin that will extend <a href="http://github.com/josevalim/inherited_resources" rel="nofollow noreferrer">InheritedResources</a>.</p> <p>Specifically I want to rewrite some default helpers.</p> <p>And I'd like it to "just work" once installed, w/o any changes to application code.</p> <p>The functionality is provided in a module which needs to be included in a <strong>right place</strong>. The question is where? :)</p> <p>The first attempt was to do it in my plugin's init.rb:</p> <pre><code>InheritedResources::Base.send :include, MyModule </code></pre> <p>It works in production, but fails miserably in development since InheritedResource::Base declared as <code>unloadable</code> and so its code is reloaded on each request. So my module is there for the first request, and then its gone.</p> <p>InheritedResource::Base is 'pulled' in again by any controller that uses it:</p> <pre><code>Class SomeController &lt; InheritedResource::Base </code></pre> <p>But no code is 'pulling in' my extension module since it is not referenced anywhere except init.rb which is <strong>not</strong> re-loaded on each request</p> <p>So right now I'm just including the module manually in every controller that needs it which sucks. I can't even include it once in ApplicationController because InheritedResources inherites from it and so it will override any changes back.</p> <h3>update</h3> <p>I'm not looking for advice on how to 'monkey patch'. The extension is working in production just great. my problem is how to catch moment exactly after InheritedResources loaded to stick my extension into it :)</p> <h3>update2</h3> <p>another attempt at clarification:</p> <p>the sequence of events is</p> <ul> <li>a) rails loads plugins. my plugin loads after inherited_resources and patches it.</li> <li>b) a development mode request is served and works</li> <li>c) rails unloads all the 'unloadable' code which includes all application code and also inherited_resources</li> <li>d) another request comes in</li> <li>e) rails loads controller, which inherites from inherited resources</li> <li>f) rails loads inherited resources which inherit from application_controller</li> <li>g) rails loads application_contrller (or may be its already loaded at this stage, not sure)</li> <li>g) request fails as no-one loaded my plugin to patch inherited_resources. plugin init.rb files are not reloaded</li> </ul> <p>I need to catch the point in time between g and h</p>
 

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