title: How can I dynamically require assets in the Rails 3.1 asset pipeline?
body: <p>I have a plugin-based system that I use for application development in Rails.  Each plugin implements an engine with MVC components, etc.  The main application is simply an empty harness that delegates all the work to the plugins that are installed.</p>
  
  <p>I'm currently upgrading to Rails 3.1 from Rails 2.3.5, and am trying to get the asset pipeline working with my framework.</p>
  
  <p>The problem I'm having is trying to programmatically require my plugin's assets into, for example, the application.js manifest.</p>
  
  <p>I can manually add them like so:</p>
  
  <pre><code>//= require &lt;plugin_manifest_path&gt;
  </code></pre>
  
  <p>And everything works as expected.  However, as there are dozens of plugins in my framework, and each installation has a different mix, I want to have this manifest change based on which plugins are installed.  I tried this as a solution:</p>
  
  <pre><code>&lt;%=
  Rails.plugins.collect do |plugin|
    "//= require #{plugin}"
  end.join("\n")
  %&gt;
  </code></pre>
  
  <p>But what I discovered is that the require/directive phase of the asset pipeline compilation happens before ERB expansion, so the generated comments were simply ending up as comments.</p>
  
  <p>Is there another mechanism for including paths for compilation that might work?  Any way to pre-process a manifest file <em>before</em> the directive processing kicks in?</p>
  
  <p>If I can't think of anything better, I may have to write a rake/deployment task that generates a plugin.js manifest file on deploy, but I'd love something more clear and elegant if possible.  Thanks!</p>
  
  <p><strong>EDIT</strong>: Solution found, will post full solution as soon as stackoverflow lets me.  See comments below in the mean time...</p>