Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including methods to a controller from a plugin

Using Rails 2.3.11, I'm creating a plugin for Redmine that add methods to ApplicationController.

I've created the following module, in the plugin :

module ApplicationControllerPatch
  def self.included(base) # :nodoc:
    base.class_eval do
      rescue_from AnException, :with => :rescue_method

      def rescue_method(exception)
        ...
      end
    end
  end
end

Now, if I include this module directly into the application_controller.rb file, like this:

class ApplicationController < ActionController::Base
  include ApplicationControllerPatch

  ...
end

Everything works just fine, however I would like to avoid editing the core source by including this module from the plugin itself. So far, if I do:

ApplicationController.send(:include, ApplicationControllerPatch)

directly from this module file (located in the plugin folder). This will load properly for the request and then it gets overwritten by the controller (I guess).

What's is the way to accomplish this ?

like image 899
Sébastien Grosjean - ZenCocoon Avatar asked Oct 10 '22 15:10

Sébastien Grosjean - ZenCocoon


1 Answers

A common pattern is to use Dispatcher.to_prepare inside your plugin's init.rb. This is required because in development mode (or generally if config.cache_classes = false) Rails reloads all classes right before each request to pick up changes without the need to completely restart the application server each time.

This however means the you have to apply your patch again after the class got reloaded as Rails can't know which modules got injected later on. Using Dispatcher.to_prepare you can achieve exactly that. The code defined in the block is executed once in production mode and before each request in development mode which makes it the premier place to monkey patch core classes.

The upside of this approach is that you can have your plugins self-contained and do not need to change anything in the surrounding application.

Put this inside your init.rb, e.g. vendor/plugins/my_plugin/init.rb

require 'redmine'

# Patches to the Redmine core.
require 'dispatcher'

Dispatcher.to_prepare do
  ApplicationController.send(:include, MyPlugin::ApplicationControllerPatch) unless ApplicationController.include?(RedmineSpentTimeColumn::Patches::IssuePatch)
end

Redmine::Plugin.register :my_plugin do
  name 'My Plugin'
  [...]
end

Your patch should always be namespaced inside a module named after your plugin to not run into issues with multiple plugins defining the same module names. Then put the patch into lib/my_plugin/application_controller_patch.rb. That way, it will be picked up automatically by the Rails Autoloader.

Put this into vendor/plugins/my_plugin/lib/my_plugin/application_controller_patch.rb

module MyPlugin
  module ApplicationControllerPatch
    def self.included(base) # :nodoc:
      base.class_eval do
        rescue_from AnException, :with => :rescue_method

        def rescue_method(exception)
          [...]
        end
      end
    end
  end
end
like image 197
Holger Just Avatar answered Oct 13 '22 10:10

Holger Just