Project

General

Profile

How to override controller ?

Added by shamith c almost 12 years ago

Hi,

I am new to Redmine Development. I am trying to create a plugin. I need to override a issue controller . ie need a before filter for create action and index action of issue controller. Please give correct method to do this.If possible, give sample code snippet.

Thanks,
Shamith c


Replies (2)

RE: How to override controller ? - Added by William Roush almost 12 years ago

Someone hop in if I make any mistakes here.

Create a class that'll override your controller....

In my case:

lib/version_controller_patch.rb:

module VersionDetails
  module VersionControllerPatch
    def self.included(base)
      base.class_eval do
        # Insert overrides here, for example:
        def show_with_plugin
          show_without_plugin
        end
        alias_method_chain :show, :plugin # This tells Redmine to allow me to extend show by letting me call it via "show_without_plugin" above.
        # I can outright override it by just calling it "def show", at which case the original controller's method will be overridden instead of extended.
      end
    end
  end
end

Now you need to initialize it, so in init.rb:

require 'versions_controller_patch' # I need to include the file I created above.

Redmine::Plugin.register :version_details do
  name 'Version Details plugin'
  author 'William Roush'
  description 'This is a plugin for Redmine'
  version '0.0.3'
  url 'http://example.com/path/to/plugin'
  author_url 'http://example.com/about'

  # Here I have support for Redmine 1.x by falling back on Rails 2.x implementation.
  if Gem::Version.new("3.0") > Gem::Version.new(Rails.version) then
    Dispatcher.to_prepare do
      # This tells the Redmine version's controller to include the module from the file above.
      VersionsController.send(:include, VersionDetails::VersionControllerPatch)
    end
  else
    # Rails 3.0 implementation.
    Rails.configuration.to_prepare do 
      # This tells the Redmine version's controller to include the module from the file above.
      VersionsController.send(:include, VersionDetails::VersionControllerPatch)
    end
  end
end

Does that explain it?

The documentation on plugin development is pretty lacking for more advanced things, I'm tempted to get some solid docs together and see about contributing them when I get more of my project in order.

RE: How to override controller ? - Added by poornima dhanasekaran over 11 years ago

Hi,

Is it possible to override an existing controller actions?.

example:
--------
I want to modify some actions in an exisiting redmine plugins.

Class ControllerName < ApplicationController

def index
p "from default"
end

end

Created plugin called 'sample' by ruby script/generare redmine_plugin sample.
Here i want to modify the index actions with the help of my plugins. Is it possible, Can anyone help me out?.

Thanks & Regards,
Poornima Dhanasekaran.

    (1-2/2)