At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

A Developer's Guide – How to Develop A Redmine Plugin

  • By Saravana
  • February 6, 2017
  • 1329 Views

I have written this article with the purpose of explaining how to develop a Redmine Plugin. So let us go about this basic introduction into Redmine Plugin, and how to go about developing it.

What is Redmine?

The basic definition of Redmine is this – Redmine is a project management tool that is a flexible open source software. It is cross-platform and cross-database tool, developed using the Ruby on Rails framework.

Why Redmine Plugins?

As is the case with any technology, plugins will extend the functionalities of Redmine. They will do so without modifying the Redmine core. They can also be easily added/removed into the Redmine application.

Creating a new Redmine Plugin

In order to create a new plugin, you need to use the generator mentioned below. This will generate a new plugin under the application’s plugins directory.

bundle exec ruby bin/rails generate redmine_plugin <plugin_name>

To modify the plugin information, edit the init.rb file located in plugins/plugin_name/init.rb. Next, you have to restart the application so as to see the new plugin appear in the plugins list. Now that a new plugin has been created, we need to generate a model for the same.

Generating a model

In order to create a new model, run the below generator so that it creates the model and the corresponding migration. And then, migrate the database using the following command:

bundle exec ruby script/rails generate redmine_plugin_model <pluginName> <modelName> [field[:type][:index]]

In order to migrate the plugins migration files:

bundle exec rake redmine:plugins:migrate

Obviously, next is to generate a controller.

Generating a controller

In order to create a new controller, run this generator:

bundle exec ruby script/rails generate redmine_plugin_controller <pluginName> <controllerName>

Adding routes

Edit the file in: plugins/pluginName/config/routes.rb in order to add the routes.

Extending menus

In order to edit the menus, you need to edit the file: plugins/plugin/init.rb and add the following line at the end of the plugin registration.

Redmine::Plugin.register :redmine_polls do
[...]
menu :application_menu, :polls, { :controller => controllerName, :action => actionName }, :caption => 'Plugin'
end

Syntax is:

menu(menuName, itemName, url, options={})

When do we need to extend?

Sometimes plugins require a specific feature implemented in the Redmine core or the plugin overrides a specific view.

  • Add new methods to a model/controller
  • Wrap an existing method to a model/controller
  • Changing the existing view

Extending Redmine model/controller

We can not override a controller and model in Redmine from Plugins. But it is easy to extend the models and controllers.

Adding a new method

Create a new module under plugins/plugin/lib directory.

require_dependency 'model/controller'
module Module
def self.included(parent)
parent.send(:include, InstanceMethods)
end
module InstanceMethods
#Insert overrides here
def method_name
code
end
end
end
# This tells the Model/Controller to include the module
Model/Controller.send(:include, ModuleName)

Wrapping an existing method

Wrapping can only be done once per method. If multiple plugins are wrapping the same method, then only the last wrapping would be valid. And so, all the previous ones would be ignored.
Create a new module under plugins/plugin/lib directory.

require_dependency 'model/controller'
module ModuleName
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable # This is required only in the development mode
alias_method_chain :new_method, :old_method
End
end
module InstanceMethods
#Insert overrides here
def new_method_with_old_method
code
end
end
end
  # This tells the Model/Controller to include the module
 Model/Controller.send(:include, ModuleName)

Changing the existing view

  • Unlike Model and Controller, we can override Redmine views by just adding the files under plugins view directory.
  • For example, in order to override the view page, the below file can be added:

/plugins/my_plugin/app/views/projects/view.html.erb.
Thus, a basic Redmine plugin has been developed. Try it out, and see for yourself how this can help with your development. For more on Ruby on Rails development follow Agira Technologies.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions

Saravana

An enthusiastic Tech Lead with 7 plus years of experience in Web development arena. Owns legitimate experience in Ruby, Ruby On Rails, AngularJs, DevOps. Golang, Another add on, This young tech freak never miss a chance to get his hands on planting and Gardening even in his busy weekends.