Defect #28340 » plugin_tutorial.textile
Plugin Tutorial
This tutorial is based on Redmine 4.x and 3.x.
You can view a previous version of this tutorial.
Redmine 1.x
Redmine 2.x
It assumes that you're familiar with the Ruby on Rails framework.
{{>toc}}
Creating a new Plugin
You may need to set the RAILS_ENV variable in order to use the command below:
$ export RAILS_ENV="production"
On windows:
$ set RAILS_ENV=production
Creating a new plugin can be done using the Redmine plugin generator.
Syntax for this generator is:
bundle exec rails generate redmine_plugin <plugin_name>
So open up a command prompt and "cd" to your redmine directory, then execute the following command:
$ bundle exec rails generate redmine_plugin Polls create plugins/polls/app create plugins/polls/app/controllers create plugins/polls/app/helpers create plugins/polls/app/models create plugins/polls/app/views create plugins/polls/db/migrate create plugins/polls/lib/tasks create plugins/polls/assets/images create plugins/polls/assets/javascripts create plugins/polls/assets/stylesheets create plugins/polls/config/locales create plugins/polls/test create plugins/polls/test/fixtures create plugins/polls/test/unit create plugins/polls/test/functional create plugins/polls/test/integration create plugins/polls/test/system create plugins/polls/README.rdoc create plugins/polls/init.rb create plugins/polls/config/routes.rb create plugins/polls/config/locales/en.yml create plugins/polls/test/test_helper.rb
The plugin structure is created in plugins/polls
. Edit plugins/polls/init.rb
to adjust plugin information (name, author, description and version):
Redmine::Plugin.register :polls do
name 'Polls plugin'
author 'Author name'
description 'This is a plugin for Redmine'
version '0.0.1'
url 'http://example.com/path/to/plugin'
author_url 'http://example.com/about'
end
Then restart the application and point your browser to http://localhost:3000/admin/plugins.
After logging in, you should see your new plugin in the plugins list:
Note: any change to the init.rb
file of your plugin requires to restart the application as it is not reloaded on each request.
Generating a model
For now plugin doesn't store anything. Let's create a simple Poll model for our plugin. Syntax is:
bundle exec rails generate redmine_plugin_model <plugin_name> <model_name> [field[:type][:index] field[:type][:index] ...]
So, go to the command prompt and run:
$ bundle exec rails generate redmine_plugin_model polls poll question:string yes:integer no:integer create plugins/polls/app/models/poll.rb create plugins/polls/test/unit/poll_test.rb create plugins/polls/db/migrate/xxxxxxxxxxxx_create_polls.rb
This creates the Poll model and the corresponding migration file xxxxxxxxxxxx_create_polls.rb
in plugins/polls/db/migrate
:
class CreatePolls < ActiveRecord::Migration[5.2]
def change
create_table :polls do |t|
t.string :question
t.integer :yes, default: 0
t.integer :no, default: 0
end
end
end
NOTE: For Redmine 3.x class CreatePolls < ActiveRecord::Migration[5.2]
is class CreatePolls < ActiveRecord::Migration
.
You can adjust your migration file (eg. default values...) then migrate the database using the following command:
$ bundle exec rake redmine:plugins:migrate Migrating polls (Polls plugin)... == CreatePolls: migrating ==================================================== -- create_table(:polls) -> 0.0410s == CreatePolls: migrated (0.0420s) ===========================================
Note that each plugin has its own set of migrations.
Let's add some Polls in the console so we have something to work with. The console is where you can interactively work and examine the Redmine environment and is very informative to play around in. But for now we just need create two Poll objects
bundle exec rails console >> Poll.create(question: "Can you see this poll") >> Poll.create(question: "And can you see this other poll") >> exit
Edit plugins/polls/app/models/poll.rb
in your plugin directory to add a #vote method that will be invoked from our controller:
class Poll < ActiveRecord::Base
def vote(answer)
increment(answer == 'yes' ? :yes : :no)
end
end
Generating a controller
For now, the plugin doesn't do anything. So let's create a controller for our plugin.
We can use the plugin controller generator for that. Syntax is:
bundle exec rails generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>]
So go back to the command prompt and run:
$ bundle exec rails generate redmine_plugin_controller Polls polls index vote create plugins/polls/app/controllers/polls_controller.rb create plugins/polls/app/helpers/polls_helper.rb create plugins/polls/test/functional/polls_controller_test.rb create plugins/polls/app/views/polls/index.html.erb create plugins/polls/app/views/polls/vote.html.erb
A controller PollsController
with 2 actions (#index
and #vote
) is created.
Edit plugins/polls/app/controllers/polls_controller.rb
to implement these 2 actions.
class PollsController < ApplicationController
def index
@polls = Poll.all
end
def vote
poll = Poll.find(params[:id])
poll.vote(params[:answer])
if poll.save
flash[:notice] = 'Vote saved.'
end
redirect_to polls_path(project_id: params[:project_id])
end
end
Then edit plugins/polls/app/views/polls/index.html.erb
that will display existing polls:
<h2>Polls</h2>
<% @polls.each do |poll| %>
<p>
<%= poll.question %>?
<%= link_to 'Yes', { action: 'vote', id: poll[:id], answer: 'yes', project_id: @project }, method: :post %> <%= poll.yes %> /
<%= link_to 'No', { action: 'vote', id: poll[:id], answer: 'no', project_id: @project }, method: :post %> <%= poll.no %>
</p>
<% end %>
You can remove plugins/polls/app/views/polls/vote.html.erb
since no rendering is done by the #vote
action.
Adding routes
Redmine does not provide the default wildcard route (':controller/:action/:id'
). Plugins have to declare the routes they need in their proper config/routes.rb
file. So edit plugins/polls/config/routes.rb
to add the 2 routes for the 2 actions:
get 'polls', to: 'polls#index'
post 'post/:id/vote', to: 'polls#vote'
You can find more information about Rails routes here: http://guides.rubyonrails.org/routing.html.
Now, restart the application and point your browser to http://localhost:3000/polls.
You should see the 2 polls and you should be able to vote for them:
Internationalization
The translation files must be stored in config/locales, eg. plugins/polls/config/locales/
.
Extending menus
Our controller works fine but users have to know the url to see the polls. Using the Redmine plugin API, you can extend standard menus.
So let's add a new item to the application menu.
Extending the application menu
Edit plugins/polls/init.rb
at the root of your plugin directory to add the following line at the end of the plugin registration block:
Redmine::Plugin.register :redmine_polls do
[...]
menu :application_menu, :polls, { controller: 'polls', action: 'index' }, caption: 'Polls'
end
Syntax is:
menu(menu_name, item_name, url, options={})
There are five menus that you can extend:
:top_menu
- the top left menu:account_menu
- the top right menu with sign in/sign out links:application_menu
- the main menu displayed when the user is not inside a project:project_menu
- the main menu displayed when the user is inside a project:admin_menu
- the menu displayed on the Administration page (can only insert after Settings, before Plugins)
Available options are:
:param
- the parameter key that is used for the project id (default is:id
):if
- a Proc that is called before rendering the item, the item is displayed only if it returns true:caption
- the menu caption that can be:- a localized string Symbol
- a String
- a Proc that can take the project as argument
:before
,:after
- specify where the menu item should be inserted (eg.after: :activity
):first
,:last
- if set to true, the item will stay at the beginning/end of the menu (eg.last: true
):html
- a hash of html options that are passed tolink_to
when rendering the menu item
In our example, we've added an item to the application menu which is empty by default.
Restart the application and go to http://localhost:3000/projects:
Now you can access the polls by clicking the Polls tab that appears when the user is not inside a project.
Extending the project menu
Now, let's consider that the polls are defined at project level (even if it's not the case in our example poll model). So we would like to add the Polls tab to the project menu instead.
Open init.rb
and replace the line that was added just before with these 2 lines:
Redmine::Plugin.register :redmine_polls do
[...]
permission :polls, { polls: [:index, :vote] }, public: true
menu :project_menu, :polls, { controller: 'polls', action: 'index' }, caption: 'Polls', after: :activity, param: :project_id
end
The second line adds our Polls tab to the project menu, just after the activity tab. The first line is required and declares that our 2 actions from PollsController
are public. We'll come back later to explain this with more details. Restart the application again and go to one of your projects:
If you click the Polls tab (in 3rd position), you should notice that the project menu is no longer displayed.
To make the project menu visible, you have to initialize the controller's instance variable @project
.
Edit your PollsController to do so:
def index
@project = Project.find(params[:project_id])
@polls = Poll.all # @project.polls
end
The project id is available in the :project_id
param because of the param: :project_id
option in the menu item declaration above.
Now, you should see the project menu when viewing the polls:
Removing item in menu
To remove an item in a menu, you can use delete_menu_item
like in this example:
Redmine::Plugin.register :redmine_polls do
[...]
delete_menu_item :top_menu, :my_page
delete_menu_item :top_menu, :help
delete_menu_item :project_menu, :overview
delete_menu_item :project_menu, :activity
delete_menu_item :project_menu, :news
end
Adding new permissions
For now, anyone can vote for polls. Let's make it more configurable by changing the permission declaration.
We're going to declare 2 project based permissions, one for viewing the polls and an other one for voting. These permissions are no longer public (public: true
option is removed).
Edit plugins/polls/init.rb
to replace the previous permission declaration with these 2 lines:
permission :view_polls, polls: :index
permission :vote_polls, polls: :vote
Restart the application and go to http://localhost:3000/roles/permissions:
You're now able to give these permissions to your existing roles.
Of course, some code needs to be added to the PollsController so that actions are actually protected according to the permissions of the current user. For this, we just need to append the :authorize
filter and make sure that the @project instance variable is properly set before calling this filter.
Here is how it would look like for the #index
action:
class PollsController < ApplicationController
before_action :find_project, :authorize, only: :index
[...]
def index
@polls = Poll.all # @project.polls
end
[...]
private
def find_project
# @project variable must be set before calling the authorize filter
@project = Project.find(params[:project_id])
end
end
Retrieving the current project before the #vote
action could be done using a similar way.
After this, viewing and voting polls will be only available to admin users or users that have the appropriate role on the project.
If you want to display the symbols of your permissions in a multilingual way, you need to add the necessary text labels in a language file.
Simply create an *.yml (eg. en.yml
) file in plugins/polls/config/locales
and fill it with labels like this:
"en":
permission_view_polls: View Polls
permission_vote_polls: Vote Polls
In this example the created file is known as en.yml
, but all other supported language file