Search code examples
ruby-on-rails-3ruby-on-rails-pluginsrails-engines

How to change search order for render a partial in plugin/engine in rails3?


I'm writing a modular project in Rails3, and I've this issue.

I've my main project, and I want to implement part of it with differentes customizations, so I'm using Engine, so I've:

- app
  - views
    - shared
      - _header.html.erb     <-- This one is called
  - ...
- config
- ...
- vendors
  - plugins
    - myplugin
      - app
        - views
          - controller1
            - action1.html.erb
          - shared
            - _header.html.erb       <--- I want to render this!

But if from action1.html.erb I call

<%= render 'shared/header' %>

the first _header.html.erb is called, I want to call "before" the one in myplugin. Can I do it only for views in myplugin?

This allows me to prevent a lot of usless "namespacing".


Solution

  • We had exactly the same problem and came up with something like this:

    def self.prioritize_engine_view_paths!(engine)
      self.view_paths = engine.paths["app/views"].existent + MyApp::Application.paths["app/views"].existent
    end
    

    It's defined in ApplicationController of the main app and then it's called in ApplicationController of every engine:

    module MyEngine
      class ApplicationController < ::ApplicationController
        prioritize_engine_view_paths!(MyEngine::Engine)
      end
    end
    

    [EDIT] In Rails 3.2.0 it will be much easier:

    config.railties_order = [Blog::Engine, :main_app, :all]
    

    See https://github.com/rails/rails/commit/40b19e063592fc30705f17aafe6a458e7b622ff2 for details.