Search code examples
redmineredmine-plugins

Redmine plugin needs to patch ApplicationController first?


This is in Redmine 1.3.

I am patching ApplicationController to add behavior (really, just to include a helper) in all controllers. The problem is that any controllers that are patched before my ApplicationController patch don't get the new behavior.

This works fine:

Dispatcher.to_prepare :my_plugin do
  require_dependency 'my_plugin/application_controller_patch'
  require_dependency 'my_plugin/welcome_controller_patch'
end

But with this, the WelcomeController throws an error when I call the helper I added.

Dispatcher.to_prepare :my_plugin do
  require_dependency 'my_plugin/welcome_controller_patch'
  require_dependency 'my_plugin/application_controller_patch'
end

This is easy to fix within a plugin, but the problem I'm having is that another plugin is patching a controller and it's subsequently losing my fix. Worse, this only happens in production -- in development, I think the plugin order is different because it works fine. I don't see a way to alter the plugin order.

I'm pretty sure my patch itself is fine, but just in case it looks like this:

require_dependency 'application_controller'  

module MyPlugin::ApplicationControllerPatch

  def self.included(base) # :nodoc:
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
    base.class_eval do
      unloadable
      helper :search
      include SearchHelper
    end
  end

  module ClassMethods    
  end

  module InstanceMethods
  end # of InstanceMethods
end # of module

ApplicationController.send(:include, MyPlugin::ApplicationControllerPatch)

Solution

  • Of course, I figure it out once I've gone and asked the question.

    In config/additional_environment.rb, the line

    config.plugins = [ :my_plugin, :all ]
    

    ensures that my plugin is loaded first. This seems to have fixed the behavior.