Plugin Tutorial » History » Version 20

Jean-Philippe Lang, 2008-09-21 14:47
requirements

1 1 Jean-Philippe Lang
h1. Plugin Tutorial
2 12 Jean-Philippe Lang
3 20 Jean-Philippe Lang
Note: To follow this tutorial, you need to run Redmine devel r1786 or higher.
4 20 Jean-Philippe Lang
5 12 Jean-Philippe Lang
{{toc}}
6 1 Jean-Philippe Lang
7 1 Jean-Philippe Lang
h2. Creating a new Plugin
8 1 Jean-Philippe Lang
9 9 Jean-Philippe Lang
Creating a new plugin can be done using the Redmine plugin generator.
10 9 Jean-Philippe Lang
Syntax for this generator is:
11 1 Jean-Philippe Lang
12 9 Jean-Philippe Lang
  ruby script/generate redmine_plugin <plugin_name>
13 9 Jean-Philippe Lang
14 9 Jean-Philippe Lang
So open up a command prompt and "cd" to your redmine directory, then execute the following command:
15 9 Jean-Philippe Lang
16 18 Jean-Philippe Lang
  % ruby script/generate redmine_plugin Polls
17 1 Jean-Philippe Lang
18 18 Jean-Philippe Lang
The plugin structure is created in @vendor/plugins/redmine_polls@:
19 1 Jean-Philippe Lang
20 1 Jean-Philippe Lang
<pre>
21 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/app/controllers
22 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/app/helpers
23 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/app/models
24 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/app/views
25 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/db/migrate
26 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/lib/tasks
27 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/assets/images
28 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/assets/javascripts
29 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/assets/stylesheets
30 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/lang
31 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/README
32 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/init.rb
33 18 Jean-Philippe Lang
      create  vendor/plugins/redmine_polls/lang/en.yml
34 1 Jean-Philippe Lang
</pre>
35 1 Jean-Philippe Lang
36 18 Jean-Philippe Lang
Edit @vendor/plugins/redmine_polls/init.rb@ to adjust plugin information (name, author, description and version):
37 1 Jean-Philippe Lang
38 1 Jean-Philippe Lang
<pre><code class="ruby">
39 1 Jean-Philippe Lang
require 'redmine'
40 1 Jean-Philippe Lang
41 18 Jean-Philippe Lang
Redmine::Plugin.register :redmine_polls do
42 18 Jean-Philippe Lang
  name 'Polls plugin'
43 1 Jean-Philippe Lang
  author 'John Smith'
44 18 Jean-Philippe Lang
  description 'A plugin for managing polls'
45 1 Jean-Philippe Lang
  version '0.0.1'
46 1 Jean-Philippe Lang
end
47 1 Jean-Philippe Lang
</code></pre>
48 1 Jean-Philippe Lang
49 2 Jean-Philippe Lang
Then restart the application and point your browser to http://localhost:3000/admin/info.
50 1 Jean-Philippe Lang
After logging in, you should see your new plugin in the plugins list:
51 4 Jean-Philippe Lang
52 2 Jean-Philippe Lang
p=. !plugins_list1.png!
53 1 Jean-Philippe Lang
54 13 Jean-Philippe Lang
h2. Generating a model
55 13 Jean-Philippe Lang
56 19 Jean-Philippe Lang
Let's create a simple Poll model for our plugin:
57 1 Jean-Philippe Lang
58 19 Jean-Philippe Lang
  ruby script/generate redmine_plugin_model polls poll question:string yes:integer no:integer
59 14 Jean-Philippe Lang
60 19 Jean-Philippe Lang
This creates the Poll model and the corresponding migration file.
61 14 Jean-Philippe Lang
Migrate the database using the following command:
62 14 Jean-Philippe Lang
63 14 Jean-Philippe Lang
  rake db:migrate_plugins
64 14 Jean-Philippe Lang
65 14 Jean-Philippe Lang
Note that each plugin has its own set of migrations.
66 14 Jean-Philippe Lang
67 19 Jean-Philippe Lang
Edit @app/models/poll.rb@ in your plugin directory to add a #vote method that will be invoked from our controller:
68 15 Jean-Philippe Lang
69 15 Jean-Philippe Lang
<pre><code class="ruby">
70 19 Jean-Philippe Lang
class Poll < ActiveRecord::Base
71 15 Jean-Philippe Lang
  def vote(answer)
72 15 Jean-Philippe Lang
    increment(answer == 'yes' ? :yes : :no)
73 15 Jean-Philippe Lang
  end
74 15 Jean-Philippe Lang
end
75 15 Jean-Philippe Lang
</code></pre>
76 15 Jean-Philippe Lang
77 1 Jean-Philippe Lang
h2. Generating a controller
78 1 Jean-Philippe Lang
79 1 Jean-Philippe Lang
For now, the plugin doesn't do anything. So let's create a controller for our plugin.
80 9 Jean-Philippe Lang
We can use the plugin controller generator for that. Syntax is:
81 9 Jean-Philippe Lang
82 9 Jean-Philippe Lang
  ruby script/generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>]
83 9 Jean-Philippe Lang
84 9 Jean-Philippe Lang
So go back to the command prompt and run:
85 3 Jean-Philippe Lang
86 3 Jean-Philippe Lang
<pre>
87 18 Jean-Philippe Lang
% ruby script/generate redmine_plugin_controller Polls polls index vote
88 3 Jean-Philippe Lang
      exists  app/controllers/
89 3 Jean-Philippe Lang
      exists  app/helpers/
90 18 Jean-Philippe Lang
      create  app/views/polls
91 3 Jean-Philippe Lang
      create  test/functional/
92 18 Jean-Philippe Lang
      create  app/controllers/polls_controller.rb
93 18 Jean-Philippe Lang
      create  test/functional/polls_controller_test.rb
94 18 Jean-Philippe Lang
      create  app/helpers/polls_helper.rb
95 18 Jean-Philippe Lang
      create  app/views/polls/index.html.erb
96 18 Jean-Philippe Lang
      create  app/views/polls/vote.html.erb
97 3 Jean-Philippe Lang
</pre>
98 3 Jean-Philippe Lang
99 18 Jean-Philippe Lang
A controller @PollsController@ with 2 actions (@#index@ and @#vote@) is created.
100 3 Jean-Philippe Lang
101 18 Jean-Philippe Lang
Edit @app/controllers/polls_controller.rb@ in @redmine_polls@ directory to implement these 2 actions.
102 3 Jean-Philippe Lang
103 3 Jean-Philippe Lang
<pre><code class="ruby">
104 18 Jean-Philippe Lang
class PollsController < ApplicationController
105 1 Jean-Philippe Lang
  unloadable
106 1 Jean-Philippe Lang
107 7 Jean-Philippe Lang
  def index
108 19 Jean-Philippe Lang
    @polls = Poll.find(:all)
109 3 Jean-Philippe Lang
  end
110 3 Jean-Philippe Lang
111 7 Jean-Philippe Lang
  def vote
112 19 Jean-Philippe Lang
    poll = Poll.find(params[:id])
113 19 Jean-Philippe Lang
    poll.vote(params[:anwser])
114 3 Jean-Philippe Lang
    flash[:notice] = 'Vote saved.'
115 3 Jean-Philippe Lang
    redirect_to :action => 'index'
116 3 Jean-Philippe Lang
  end
117 3 Jean-Philippe Lang
end
118 1 Jean-Philippe Lang
</code></pre>
119 5 Jean-Philippe Lang
120 18 Jean-Philippe Lang
Then edit @app/views/polls/index.html.erb@ that will display existing polls:
121 3 Jean-Philippe Lang
122 3 Jean-Philippe Lang
123 3 Jean-Philippe Lang
<pre>
124 18 Jean-Philippe Lang
<h2>Polls</h2>
125 3 Jean-Philippe Lang
126 19 Jean-Philippe Lang
<% @polls.each do |poll| %>
127 3 Jean-Philippe Lang
  <p>
128 19 Jean-Philippe Lang
  <%= poll[:question] %>?
129 19 Jean-Philippe Lang
  <%= link_to 'Yes', {:action => 'vote', :id => poll[:id], :answer => 'yes'}, :method => :post %> (<%= poll[:yes] %>) /
130 19 Jean-Philippe Lang
  <%= link_to 'No', {:action => 'vote', :id => poll[:id], :answer => 'no'}, :method => :post %> (<%= poll[:no] %>)
131 3 Jean-Philippe Lang
  </p>
132 3 Jean-Philippe Lang
<% end %>
133 3 Jean-Philippe Lang
</pre>
134 3 Jean-Philippe Lang
135 3 Jean-Philippe Lang
You can remove @vote.html.erb@ since no rendering is done by the corresponding action.
136 3 Jean-Philippe Lang
137 18 Jean-Philippe Lang
Now, restart the application and point your browser to http://localhost:3000/polls.
138 18 Jean-Philippe Lang
You should see the 2 polls and you should be able to vote for them:
139 4 Jean-Philippe Lang
140 18 Jean-Philippe Lang
p=. !polls1.png!
141 4 Jean-Philippe Lang
142 19 Jean-Philippe Lang
Note that poll results are reset on each request if you don't run the application in production mode, since our poll "model" is stored in a class variable in this example.
143 4 Jean-Philippe Lang
144 4 Jean-Philippe Lang
h2. Extending menus
145 4 Jean-Philippe Lang
146 18 Jean-Philippe Lang
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.
147 4 Jean-Philippe Lang
So let's add a new item to the application menu.
148 4 Jean-Philippe Lang
149 4 Jean-Philippe Lang
h3. Extending the application menu
150 4 Jean-Philippe Lang
151 4 Jean-Philippe Lang
Edit @init.rb@ at the root of your plugin directory to add the following line at the end of the plugin registration block:
152 4 Jean-Philippe Lang
153 4 Jean-Philippe Lang
<pre><code class="ruby">
154 18 Jean-Philippe Lang
Redmine::Plugin.register :redmine_polls do
155 4 Jean-Philippe Lang
  [...]
156 4 Jean-Philippe Lang
  
157 18 Jean-Philippe Lang
  menu :application_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls'
158 4 Jean-Philippe Lang
end
159 4 Jean-Philippe Lang
</code></pre>
160 4 Jean-Philippe Lang
161 4 Jean-Philippe Lang
Syntax is:
162 4 Jean-Philippe Lang
163 4 Jean-Philippe Lang
  menu(menu_name, item_name, url, options={})
164 4 Jean-Philippe Lang
165 4 Jean-Philippe Lang
There are 4 menus that you can extend:
166 4 Jean-Philippe Lang
167 4 Jean-Philippe Lang
* @:top_menu@ - the top left menu
168 4 Jean-Philippe Lang
* @:account_menu@ - the top right menu with sign in/sign out links
169 4 Jean-Philippe Lang
* @:application_menu@ - the main menu displayed when the user is not inside a project
170 4 Jean-Philippe Lang
* @:project_menu@ - the main menu displayed when the user is inside a project
171 4 Jean-Philippe Lang
172 4 Jean-Philippe Lang
Available options are:
173 4 Jean-Philippe Lang
174 4 Jean-Philippe Lang
* @:param@ - the parameter key that is used for the project id (default is @:id@)
175 4 Jean-Philippe Lang
* @:if@ - a Proc that is called before rendering the item, the item is displayed only if it returns true
176 4 Jean-Philippe Lang
* @:caption@ - the menu caption that can be:
177 4 Jean-Philippe Lang
178 4 Jean-Philippe Lang
  * a localized string Symbol
179 4 Jean-Philippe Lang
  * a String
180 4 Jean-Philippe Lang
  * a Proc that can take the project as argument
181 4 Jean-Philippe Lang
182 4 Jean-Philippe Lang
* @:before@, @:after@ - specify where the menu item should be inserted (eg. @:after => :activity@)
183 4 Jean-Philippe Lang
* @:last@ - if set to true, the item will stay at the end of the menu (eg. @:last => true@)
184 4 Jean-Philippe Lang
* @:html_options@ - a hash of html options that are passed to @link_to@ when rendering the menu item
185 4 Jean-Philippe Lang
186 4 Jean-Philippe Lang
In our example, we've added an item to the application menu which is emtpy by default.
187 4 Jean-Philippe Lang
Restart the application and go to http://localhost:3000:
188 4 Jean-Philippe Lang
189 4 Jean-Philippe Lang
p=. !application_menu.png!
190 4 Jean-Philippe Lang
191 18 Jean-Philippe Lang
Now you can access the polls by clicking the Polls tab from the welcome screen.
192 4 Jean-Philippe Lang
193 4 Jean-Philippe Lang
h3. Extending the project menu
194 4 Jean-Philippe Lang
195 19 Jean-Philippe Lang
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.
196 6 Jean-Philippe Lang
Open @init.rb@ and replace the line that was added just before with these 2 lines:
197 6 Jean-Philippe Lang
198 6 Jean-Philippe Lang
<pre><code class="ruby">
199 18 Jean-Philippe Lang
Redmine::Plugin.register :redmine_polls do
200 6 Jean-Philippe Lang
  [...]
201 6 Jean-Philippe Lang
202 18 Jean-Philippe Lang
  permission :polls, {:polls => [:index, :vote]}, :public => true
203 18 Jean-Philippe Lang
  menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id
204 6 Jean-Philippe Lang
end
205 6 Jean-Philippe Lang
</code></pre>
206 6 Jean-Philippe Lang
207 18 Jean-Philippe Lang
The second line adds our Polls tab to the project menu, just after the activity tab.
208 18 Jean-Philippe Lang
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.
209 6 Jean-Philippe Lang
210 6 Jean-Philippe Lang
Restart the application again and go to one of your projects:
211 6 Jean-Philippe Lang
212 6 Jean-Philippe Lang
p=. !project_menu.png!
213 6 Jean-Philippe Lang
214 18 Jean-Philippe Lang
If you click the Polls tab, you should notice that the project menu is no longer displayed.
215 6 Jean-Philippe Lang
To make the project menu visible, you have to initialize the controller's instance variable @@project@.
216 6 Jean-Philippe Lang
217 18 Jean-Philippe Lang
Edit your PollsController to do so:
218 6 Jean-Philippe Lang
219 6 Jean-Philippe Lang
<pre><code class="ruby">
220 6 Jean-Philippe Lang
def index
221 6 Jean-Philippe Lang
  @project = Project.find(params[:project_id])
222 19 Jean-Philippe Lang
  @polls = Poll.find(:all) # @project.polls
223 6 Jean-Philippe Lang
end
224 6 Jean-Philippe Lang
</code></pre>
225 6 Jean-Philippe Lang
226 6 Jean-Philippe Lang
The project id is available in the @:project_id@ param because of the @:param => :project_id@ option in the menu item declaration above.
227 6 Jean-Philippe Lang
228 18 Jean-Philippe Lang
Now, you should see the project menu when viewing the polls:
229 6 Jean-Philippe Lang
230 18 Jean-Philippe Lang
p=. !project_menu_polls.png!
231 4 Jean-Philippe Lang
232 4 Jean-Philippe Lang
h2. Adding new permissions
233 4 Jean-Philippe Lang
234 18 Jean-Philippe Lang
For now, anyone can vote for polls. Let's make it more configurable by changing the permission declaration.
235 18 Jean-Philippe Lang
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).
236 10 Jean-Philippe Lang
237 10 Jean-Philippe Lang
Edit @init.rb@ to replace the previous permission declaration with these 2 lines:
238 10 Jean-Philippe Lang
239 10 Jean-Philippe Lang
<pre><code class="ruby">
240 20 Jean-Philippe Lang
241 18 Jean-Philippe Lang
  permission :view_polls, :polls => :index
242 18 Jean-Philippe Lang
  permission :vote_polls, :polls => :vote
243 1 Jean-Philippe Lang
</code></pre>
244 14 Jean-Philippe Lang
245 10 Jean-Philippe Lang
246 10 Jean-Philippe Lang
Restart the application and go to http://localhost:3000/roles/report:
247 10 Jean-Philippe Lang
248 10 Jean-Philippe Lang
p=. !permissions1.png!
249 10 Jean-Philippe Lang
250 10 Jean-Philippe Lang
You're now able to give these permissions to your existing roles.
251 10 Jean-Philippe Lang
252 18 Jean-Philippe Lang
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.
253 10 Jean-Philippe Lang
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.
254 10 Jean-Philippe Lang
255 10 Jean-Philippe Lang
Here is how it would look like for the @#index@ action:
256 10 Jean-Philippe Lang
257 1 Jean-Philippe Lang
<pre><code class="ruby">
258 18 Jean-Philippe Lang
class PollsController < ApplicationController
259 10 Jean-Philippe Lang
  unloadable
260 10 Jean-Philippe Lang
  
261 10 Jean-Philippe Lang
  before_filter :find_project, :authorize, :only => :index
262 10 Jean-Philippe Lang
263 10 Jean-Philippe Lang
  [...]
264 10 Jean-Philippe Lang
  
265 10 Jean-Philippe Lang
  def index
266 19 Jean-Philippe Lang
    @polls = Poll.find(:all) # @project.polls
267 10 Jean-Philippe Lang
  end
268 10 Jean-Philippe Lang
269 10 Jean-Philippe Lang
  [...]
270 10 Jean-Philippe Lang
  
271 10 Jean-Philippe Lang
  private
272 10 Jean-Philippe Lang
  
273 10 Jean-Philippe Lang
  def find_project
274 10 Jean-Philippe Lang
    # @project variable must be set before calling the authorize filter
275 10 Jean-Philippe Lang
    @project = Project.find(params[:project_id])
276 10 Jean-Philippe Lang
  end
277 10 Jean-Philippe Lang
end
278 10 Jean-Philippe Lang
</code></pre>
279 10 Jean-Philippe Lang
280 10 Jean-Philippe Lang
Retrieving the current project before the @#vote@ action could be done using a similiar way.
281 18 Jean-Philippe Lang
After this, viewing and voting polls will be only available to admin users or users that have the appropriate role on the project.
282 4 Jean-Philippe Lang
283 4 Jean-Philippe Lang
h2. Creating a project module
284 4 Jean-Philippe Lang
285 19 Jean-Philippe Lang
For now, the poll functionality is added to all your projects. But you way want to enable polls for some projects only.
286 18 Jean-Philippe Lang
So, let's create a 'Polls' project module. This is done by wraping the permissions declaration inside a call to @#project_module@.
287 11 Jean-Philippe Lang
288 11 Jean-Philippe Lang
Edit @init.rb@ and change the permissions declaration:
289 11 Jean-Philippe Lang
290 11 Jean-Philippe Lang
<pre><code class="ruby">
291 18 Jean-Philippe Lang
  project_module :polls do
292 18 Jean-Philippe Lang
    permission :view_polls, :polls => :index
293 18 Jean-Philippe Lang
    permission :vote_polls, :polls => :vote
294 11 Jean-Philippe Lang
  end
295 11 Jean-Philippe Lang
</code></pre>
296 11 Jean-Philippe Lang
297 11 Jean-Philippe Lang
Restart the application and go to one of your project settings.
298 18 Jean-Philippe Lang
Click on the Modules tab. You should see the Polls module at the end of the modules list (disabled by default):
299 11 Jean-Philippe Lang
300 11 Jean-Philippe Lang
p=. !modules.png!
301 11 Jean-Philippe Lang
302 18 Jean-Philippe Lang
You can now enable/disable polls at project level.
303 11 Jean-Philippe Lang
304 11 Jean-Philippe Lang
h2. Improving the plugin views
305 11 Jean-Philippe Lang
306 16 Jean-Philippe Lang
h3. Adding stylesheets
307 16 Jean-Philippe Lang
308 16 Jean-Philippe Lang
Let's start by adding a stylesheet to our plugin views.
309 16 Jean-Philippe Lang
Create a file named @voting.css@ in the @assets/stylesheets@ directory of your plugin:
310 16 Jean-Philippe Lang
311 16 Jean-Philippe Lang
<pre>
312 16 Jean-Philippe Lang
a.vote { font-size: 120%; }
313 16 Jean-Philippe Lang
a.vote.yes { color: green; }
314 16 Jean-Philippe Lang
a.vote.no  { color: red; }
315 16 Jean-Philippe Lang
</pre>
316 16 Jean-Philippe Lang
317 18 Jean-Philippe Lang
When starting the application, plugin assets are automatically copied to @public/plugin_assets/redmine_polls/@ by Rails Engines to make them available through your web server. So any change to your plugin stylesheets or javascripts needs an application restart.
318 16 Jean-Philippe Lang
319 18 Jean-Philippe Lang
Then, append the following lines at the end of @app/views/polls/index.html.erb@ so that your stylesheet get included in the page header by Redmine:
320 16 Jean-Philippe Lang
321 16 Jean-Philippe Lang
<pre>
322 16 Jean-Philippe Lang
<% content_for :header_tags do %>
323 18 Jean-Philippe Lang
    <%= stylesheet_link_tag 'voting', :plugin => 'redmine_polls' %>
324 16 Jean-Philippe Lang
<% end %>
325 16 Jean-Philippe Lang
</pre>
326 16 Jean-Philippe Lang
327 18 Jean-Philippe Lang
Note that the @:plugin => 'redmine_polls'@ option is required when calling the @stylesheet_link_tag@ helper.
328 16 Jean-Philippe Lang
329 16 Jean-Philippe Lang
Javascripts can be included in plugin views using the @javascript_include_tag@ helper in the same way.
330 16 Jean-Philippe Lang
331 16 Jean-Philippe Lang
h3. Setting page title
332 16 Jean-Philippe Lang
333 16 Jean-Philippe Lang
You can set the HTML title from inside your views by using the @html_title@ helper.
334 16 Jean-Philippe Lang
Example:
335 16 Jean-Philippe Lang
336 18 Jean-Philippe Lang
  <% html_title "Polls" -%>