Plugin Tutorial » History » Version 70

Jean-Philippe Lang, 2012-05-28 10:46

1 1 Jean-Philippe Lang
h1. Plugin Tutorial
2 12 Jean-Philippe Lang
3 67 Jean-Philippe Lang
This tutorial is based on Redmine 2.x. You can view a previous version of this tutorial for Redmine 1.x "here":/projects/redmine/wiki/Plugin_Tutorial?version=66.
4 20 Jean-Philippe Lang
5 30 Vinod Singh
{{>toc}}
6 1 Jean-Philippe Lang
7 1 Jean-Philippe Lang
h2. Creating a new Plugin
8 40 Nick Peelman
 
9 40 Nick Peelman
You may need to set the RAILS_ENV variable in order to use the command below:
10 32 Jiří Křivánek
11 32 Jiří Křivánek
<pre>
12 32 Jiří Křivánek
$ export RAILS_ENV="production"
13 32 Jiří Křivánek
</pre>
14 32 Jiří Křivánek
15 59 Harry Garrood
On windows:
16 59 Harry Garrood
17 59 Harry Garrood
<pre>
18 68 Jean-Philippe Lang
$ set RAILS_ENV=production
19 59 Harry Garrood
</pre>
20 59 Harry Garrood
21 9 Jean-Philippe Lang
Creating a new plugin can be done using the Redmine plugin generator.
22 9 Jean-Philippe Lang
Syntax for this generator is:
23 1 Jean-Philippe Lang
24 67 Jean-Philippe Lang
<pre>ruby script/rails generate redmine_plugin <plugin_name></pre>
25 9 Jean-Philippe Lang
26 1 Jean-Philippe Lang
So open up a command prompt and "cd" to your redmine directory, then execute the following command:
27 1 Jean-Philippe Lang
28 1 Jean-Philippe Lang
<pre>
29 68 Jean-Philippe Lang
$ ruby script/rails generate redmine_plugin Polls
30 67 Jean-Philippe Lang
      create  plugins/polls/app
31 67 Jean-Philippe Lang
      create  plugins/polls/app/controllers
32 67 Jean-Philippe Lang
      create  plugins/polls/app/helpers
33 67 Jean-Philippe Lang
      create  plugins/polls/app/models
34 67 Jean-Philippe Lang
      create  plugins/polls/app/views
35 67 Jean-Philippe Lang
      create  plugins/polls/db/migrate
36 67 Jean-Philippe Lang
      create  plugins/polls/lib/tasks
37 67 Jean-Philippe Lang
      create  plugins/polls/assets/images
38 67 Jean-Philippe Lang
      create  plugins/polls/assets/javascripts
39 67 Jean-Philippe Lang
      create  plugins/polls/assets/stylesheets
40 67 Jean-Philippe Lang
      create  plugins/polls/config/locales
41 67 Jean-Philippe Lang
      create  plugins/polls/test
42 67 Jean-Philippe Lang
      create  plugins/polls/README.rdoc
43 67 Jean-Philippe Lang
      create  plugins/polls/init.rb
44 67 Jean-Philippe Lang
      create  plugins/polls/config/routes.rb
45 67 Jean-Philippe Lang
      create  plugins/polls/config/locales/en.yml
46 67 Jean-Philippe Lang
      create  plugins/polls/test/test_helper.rb
47 1 Jean-Philippe Lang
</pre>
48 1 Jean-Philippe Lang
49 67 Jean-Philippe Lang
The plugin structure is created in @plugins/polls@. Edit @plugins/polls/init.rb@ to adjust plugin information (name, author, description and version):
50 1 Jean-Philippe Lang
51 1 Jean-Philippe Lang
<pre><code class="ruby">
52 67 Jean-Philippe Lang
Redmine::Plugin.register :polls do
53 1 Jean-Philippe Lang
  name 'Polls plugin'
54 1 Jean-Philippe Lang
  author 'John Smith'
55 1 Jean-Philippe Lang
  description 'A plugin for managing polls'
56 1 Jean-Philippe Lang
  version '0.0.1'
57 1 Jean-Philippe Lang
end
58 1 Jean-Philippe Lang
</code></pre>
59 1 Jean-Philippe Lang
60 1 Jean-Philippe Lang
Then restart the application and point your browser to http://localhost:3000/admin/plugins.
61 1 Jean-Philippe Lang
After logging in, you should see your new plugin in the plugins list:
62 1 Jean-Philippe Lang
63 1 Jean-Philippe Lang
!plugins_list1.png!
64 1 Jean-Philippe Lang
65 1 Jean-Philippe Lang
h2. Generating a model
66 1 Jean-Philippe Lang
67 1 Jean-Philippe Lang
For now plugin doesn't store anything. Let's create a simple Poll model for our plugin. Syntax is:
68 1 Jean-Philippe Lang
69 1 Jean-Philippe Lang
<pre>
70 67 Jean-Philippe Lang
   ruby script/rails generate redmine_plugin_model <plugin_name> <model_name> [field[:type][:index] field[:type][:index] ...]
71 1 Jean-Philippe Lang
</pre>
72 1 Jean-Philippe Lang
73 1 Jean-Philippe Lang
So, go to the command prompt and run:
74 1 Jean-Philippe Lang
75 1 Jean-Philippe Lang
<pre>
76 68 Jean-Philippe Lang
$ ruby script/rails generate redmine_plugin_model polls poll question:string yes:integer no:integer
77 67 Jean-Philippe Lang
      create  plugins/polls/app/models/poll.rb
78 67 Jean-Philippe Lang
      create  plugins/polls/test/unit/poll_test.rb
79 67 Jean-Philippe Lang
      create  plugins/polls/db/migrate/001_create_polls.rb
80 13 Jean-Philippe Lang
</pre>
81 1 Jean-Philippe Lang
82 67 Jean-Philippe Lang
This creates the Poll model and the corresponding migration file @001_create_polls.rb@ in @plugins/polls/db/migrate@:
83 1 Jean-Philippe Lang
84 67 Jean-Philippe Lang
<pre><code class="ruby">
85 67 Jean-Philippe Lang
class CreatePolls < ActiveRecord::Migration
86 67 Jean-Philippe Lang
  def change
87 67 Jean-Philippe Lang
    create_table :polls do |t|
88 67 Jean-Philippe Lang
      t.string :question
89 67 Jean-Philippe Lang
      t.integer :yes, :default => 0
90 67 Jean-Philippe Lang
      t.integer :no, :default => 0
91 67 Jean-Philippe Lang
    end
92 67 Jean-Philippe Lang
  end
93 67 Jean-Philippe Lang
end
94 67 Jean-Philippe Lang
</code></pre>
95 1 Jean-Philippe Lang
96 67 Jean-Philippe Lang
You can adjust your migration file (eg. default values...) then migrate the database using the following command:
97 14 Jean-Philippe Lang
98 67 Jean-Philippe Lang
<pre>
99 68 Jean-Philippe Lang
$ rake redmine:plugins:migrate
100 1 Jean-Philippe Lang
101 67 Jean-Philippe Lang
Migrating polls (Polls plugin)...
102 67 Jean-Philippe Lang
==  CreatePolls: migrating ====================================================
103 67 Jean-Philippe Lang
-- create_table(:polls)
104 67 Jean-Philippe Lang
   -> 0.0410s
105 67 Jean-Philippe Lang
==  CreatePolls: migrated (0.0420s) ===========================================
106 67 Jean-Philippe Lang
</pre>
107 24 Eric Davis
108 64 Denny Schäfer
Note that each plugin has its own set of migrations.
109 64 Denny Schäfer
110 24 Eric Davis
Lets 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
111 24 Eric Davis
112 24 Eric Davis
<pre>
113 67 Jean-Philippe Lang
ruby script/rails console
114 15 Jean-Philippe Lang
>> Poll.create(:question => "Can you see this poll")
115 19 Jean-Philippe Lang
>> Poll.create(:question => "And can you see this other poll")
116 15 Jean-Philippe Lang
>> exit
117 15 Jean-Philippe Lang
</pre>
118 15 Jean-Philippe Lang
119 67 Jean-Philippe Lang
Edit @plugins/polls/app/models/poll.rb@ in your plugin directory to add a #vote method that will be invoked from our controller:
120 1 Jean-Philippe Lang
121 15 Jean-Philippe Lang
<pre><code class="ruby">
122 15 Jean-Philippe Lang
class Poll < ActiveRecord::Base
123 1 Jean-Philippe Lang
  def vote(answer)
124 1 Jean-Philippe Lang
    increment(answer == 'yes' ? :yes : :no)
125 60 Mischa The Evil
  end
126 57 Etienne Massip
end
127 9 Jean-Philippe Lang
</code></pre>
128 9 Jean-Philippe Lang
129 23 Jean-Baptiste Barth
h2. Generating a controller
130 9 Jean-Philippe Lang
131 67 Jean-Philippe Lang
*Warning*: 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.
132 3 Jean-Philippe Lang
133 3 Jean-Philippe Lang
For now, the plugin doesn't do anything. So let's create a controller for our plugin.
134 1 Jean-Philippe Lang
We can use the plugin controller generator for that. Syntax is:
135 18 Jean-Philippe Lang
136 67 Jean-Philippe Lang
<pre>ruby script/rails generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>]</pre>
137 1 Jean-Philippe Lang
138 1 Jean-Philippe Lang
So go back to the command prompt and run:
139 1 Jean-Philippe Lang
140 1 Jean-Philippe Lang
<pre>
141 68 Jean-Philippe Lang
$ ruby script/rails generate redmine_plugin_controller Polls polls index vote
142 67 Jean-Philippe Lang
      create  plugins/polls/app/controllers/polls_controller.rb
143 67 Jean-Philippe Lang
      create  plugins/polls/app/helpers/polls_helper.rb
144 67 Jean-Philippe Lang
      create  plugins/polls/test/functional/polls_controller_test.rb
145 67 Jean-Philippe Lang
      create  plugins/polls/app/views/polls/index.html.erb
146 67 Jean-Philippe Lang
      create  plugins/polls/app/views/polls/vote.html.erb
147 1 Jean-Philippe Lang
</pre>
148 3 Jean-Philippe Lang
149 1 Jean-Philippe Lang
A controller @PollsController@ with 2 actions (@#index@ and @#vote@) is created.
150 3 Jean-Philippe Lang
151 67 Jean-Philippe Lang
Edit @plugins/polls/app/controllers/polls_controller.rb@ to implement these 2 actions.
152 1 Jean-Philippe Lang
153 1 Jean-Philippe Lang
<pre><code class="ruby">
154 7 Jean-Philippe Lang
class PollsController < ApplicationController
155 19 Jean-Philippe Lang
  unloadable
156 3 Jean-Philippe Lang
157 7 Jean-Philippe Lang
  def index
158 19 Jean-Philippe Lang
    @polls = Poll.find(:all)
159 1 Jean-Philippe Lang
  end
160 21 Jean-Baptiste Barth
161 25 Eric Davis
  def vote
162 25 Eric Davis
    poll = Poll.find(params[:id])
163 25 Eric Davis
    poll.vote(params[:answer])
164 25 Eric Davis
    if poll.save
165 3 Jean-Philippe Lang
      flash[:notice] = 'Vote saved.'
166 3 Jean-Philippe Lang
      redirect_to :action => 'index'
167 1 Jean-Philippe Lang
    end
168 5 Jean-Philippe Lang
  end
169 26 Eric Davis
end
170 3 Jean-Philippe Lang
</code></pre>
171 3 Jean-Philippe Lang
172 67 Jean-Philippe Lang
Then edit @plugins/polls/app/views/polls/index.html.erb@ that will display existing polls:
173 18 Jean-Philippe Lang
174 3 Jean-Philippe Lang
175 19 Jean-Philippe Lang
<pre>
176 3 Jean-Philippe Lang
<h2>Polls</h2>
177 19 Jean-Philippe Lang
178 50 Igor Zubkov
<% @polls.each do |poll| %>
179 50 Igor Zubkov
  <p>
180 3 Jean-Philippe Lang
  <%= poll[:question] %>?
181 3 Jean-Philippe Lang
  <%= link_to 'Yes', { :action => 'vote', :id => poll[:id], :answer => 'yes' }, :method => :post %> (<%= poll[:yes] %>) /
182 3 Jean-Philippe Lang
  <%= link_to 'No', { :action => 'vote', :id => poll[:id], :answer => 'no' }, :method => :post %> (<%= poll[:no] %>)
183 3 Jean-Philippe Lang
  </p>
184 26 Eric Davis
<% end %>
185 3 Jean-Philippe Lang
</pre>
186 18 Jean-Philippe Lang
187 67 Jean-Philippe Lang
You can remove @plugins/polls/app/views/polls/vote.html.erb@ since no rendering is done by the corresponding action.
188 4 Jean-Philippe Lang
189 67 Jean-Philippe Lang
Edit @plugins/polls/config/routes.rb@ to add the 2 routes for the 2 actions:
190 67 Jean-Philippe Lang
191 67 Jean-Philippe Lang
<pre><code class="ruby">
192 67 Jean-Philippe Lang
get 'polls', :to => 'polls#index'
193 67 Jean-Philippe Lang
post 'post/:id/vote', :to => 'polls#vote'
194 67 Jean-Philippe Lang
</code></pre>
195 67 Jean-Philippe Lang
196 38 Randy Syring
Now, restart the application and point your browser to http://localhost:3000/polls.
197 38 Randy Syring
You should see the 2 polls and you should be able to vote for them:
198 38 Randy Syring
199 38 Randy Syring
!pools1.png!
200 4 Jean-Philippe Lang
201 18 Jean-Philippe Lang
h2. Translations
202 4 Jean-Philippe Lang
203 67 Jean-Philippe Lang
The translation files must be stored in config/locales, eg. @plugins/polls/config/locales/@.
204 1 Jean-Philippe Lang
205 1 Jean-Philippe Lang
h2. Extending menus
206 4 Jean-Philippe Lang
207 26 Eric Davis
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.
208 4 Jean-Philippe Lang
So let's add a new item to the application menu.
209 4 Jean-Philippe Lang
210 18 Jean-Philippe Lang
h3. Extending the application menu
211 4 Jean-Philippe Lang
212 67 Jean-Philippe Lang
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:
213 18 Jean-Philippe Lang
214 4 Jean-Philippe Lang
<pre><code class="ruby">
215 4 Jean-Philippe Lang
Redmine::Plugin.register :redmine_polls do
216 4 Jean-Philippe Lang
  [...]
217 4 Jean-Philippe Lang
  
218 4 Jean-Philippe Lang
  menu :application_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls'
219 4 Jean-Philippe Lang
end
220 4 Jean-Philippe Lang
</code></pre>
221 42 Mischa The Evil
222 4 Jean-Philippe Lang
Syntax is:
223 4 Jean-Philippe Lang
224 4 Jean-Philippe Lang
  menu(menu_name, item_name, url, options={})
225 4 Jean-Philippe Lang
226 4 Jean-Philippe Lang
There are five menus that you can extend:
227 1 Jean-Philippe Lang
228 4 Jean-Philippe Lang
* @:top_menu@ - the top left menu
229 4 Jean-Philippe Lang
* @:account_menu@ - the top right menu with sign in/sign out links
230 4 Jean-Philippe Lang
* @:application_menu@ - the main menu displayed when the user is not inside a project
231 4 Jean-Philippe Lang
* @:project_menu@ - the main menu displayed when the user is inside a project
232 4 Jean-Philippe Lang
* @:admin_menu@ - the menu displayed on the Administration page (can only insert after Settings, before Plugins)
233 4 Jean-Philippe Lang
234 4 Jean-Philippe Lang
Available options are:
235 4 Jean-Philippe Lang
236 1 Jean-Philippe Lang
* @:param@ - the parameter key that is used for the project id (default is @:id@)
237 36 Jérémie Delaitre
* @:if@ - a Proc that is called before rendering the item, the item is displayed only if it returns true
238 36 Jérémie Delaitre
* @:caption@ - the menu caption that can be:
239 4 Jean-Philippe Lang
240 4 Jean-Philippe Lang
  * a localized string Symbol
241 4 Jean-Philippe Lang
  * a String
242 4 Jean-Philippe Lang
  * a Proc that can take the project as argument
243 29 Vinod Singh
244 4 Jean-Philippe Lang
* @:before@, @:after@ - specify where the menu item should be inserted (eg. @:after => :activity@)
245 18 Jean-Philippe Lang
* @:first@, @:last@ - if set to true, the item will stay at the beginning/end of the menu (eg. @:last => true@)
246 4 Jean-Philippe Lang
* @:html@ - a hash of html options that are passed to @link_to@ when rendering the menu item
247 4 Jean-Philippe Lang
248 4 Jean-Philippe Lang
In our example, we've added an item to the application menu which is emtpy by default.
249 19 Jean-Philippe Lang
Restart the application and go to http://localhost:3000:
250 6 Jean-Philippe Lang
251 6 Jean-Philippe Lang
!application_menu.png!
252 6 Jean-Philippe Lang
253 18 Jean-Philippe Lang
Now you can access the polls by clicking the Polls tab from the welcome screen.
254 6 Jean-Philippe Lang
255 6 Jean-Philippe Lang
h3. Extending the project menu
256 51 Igor Zubkov
257 18 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.
258 6 Jean-Philippe Lang
Open @init.rb@ and replace the line that was added just before with these 2 lines:
259 6 Jean-Philippe Lang
260 6 Jean-Philippe Lang
<pre><code class="ruby">
261 18 Jean-Philippe Lang
Redmine::Plugin.register :redmine_polls do
262 18 Jean-Philippe Lang
  [...]
263 6 Jean-Philippe Lang
264 6 Jean-Philippe Lang
  permission :polls, { :polls => [:index, :vote] }, :public => true
265 6 Jean-Philippe Lang
  menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id
266 1 Jean-Philippe Lang
end
267 6 Jean-Philippe Lang
</code></pre>
268 6 Jean-Philippe Lang
269 67 Jean-Philippe Lang
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:
270 6 Jean-Philippe Lang
271 1 Jean-Philippe Lang
!http://www.redmine.org/attachments/3773/project_menu.png!
272 6 Jean-Philippe Lang
273 67 Jean-Philippe Lang
If you click the Polls tab (in 3rd position), you should notice that the project menu is no longer displayed.
274 19 Jean-Philippe Lang
To make the project menu visible, you have to initialize the controller's instance variable @@project@.
275 6 Jean-Philippe Lang
276 61 Harry Garrood
Edit your PollsController to do so:
277 61 Harry Garrood
278 6 Jean-Philippe Lang
<pre><code class="ruby">
279 6 Jean-Philippe Lang
def index
280 18 Jean-Philippe Lang
  @project = Project.find(params[:project_id])
281 6 Jean-Philippe Lang
  @polls = Poll.find(:all) # @project.polls
282 39 Ric Turley
end
283 4 Jean-Philippe Lang
</code></pre>
284 4 Jean-Philippe Lang
285 4 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.
286 18 Jean-Philippe Lang
287 18 Jean-Philippe Lang
Now, you should see the project menu when viewing the polls:
288 10 Jean-Philippe Lang
289 26 Eric Davis
!http://www.redmine.org/attachments/3774/project_menu_pools.png!
290 10 Jean-Philippe Lang
291 10 Jean-Philippe Lang
h2. Adding new permissions
292 20 Jean-Philippe Lang
293 18 Jean-Philippe Lang
For now, anyone can vote for polls. Let's make it more configurable by changing the permission declaration.
294 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).
295 1 Jean-Philippe Lang
296 67 Jean-Philippe Lang
Edit @plugins/polls/init.rb@ to replace the previous permission declaration with these 2 lines:
297 10 Jean-Philippe Lang
298 10 Jean-Philippe Lang
<pre><code class="ruby">
299 10 Jean-Philippe Lang
  permission :view_polls, :polls => :index
300 29 Vinod Singh
  permission :vote_polls, :polls => :vote
301 1 Jean-Philippe Lang
</code></pre>
302 10 Jean-Philippe Lang
303 10 Jean-Philippe Lang
Restart the application and go to http://localhost:3000/roles/report:
304 18 Jean-Philippe Lang
305 10 Jean-Philippe Lang
!permissions1.png!
306 10 Jean-Philippe Lang
307 10 Jean-Philippe Lang
You're now able to give these permissions to your existing roles.
308 1 Jean-Philippe Lang
309 67 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. 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.
310 18 Jean-Philippe Lang
311 10 Jean-Philippe Lang
Here is how it would look like for the @#index@ action:
312 10 Jean-Philippe Lang
313 10 Jean-Philippe Lang
<pre><code class="ruby">
314 10 Jean-Philippe Lang
class PollsController < ApplicationController
315 10 Jean-Philippe Lang
  unloadable
316 10 Jean-Philippe Lang
  
317 10 Jean-Philippe Lang
  before_filter :find_project, :authorize, :only => :index
318 19 Jean-Philippe Lang
319 10 Jean-Philippe Lang
  [...]
320 10 Jean-Philippe Lang
  
321 10 Jean-Philippe Lang
  def index
322 10 Jean-Philippe Lang
    @polls = Poll.find(:all) # @project.polls
323 10 Jean-Philippe Lang
  end
324 10 Jean-Philippe Lang
325 10 Jean-Philippe Lang
  [...]
326 10 Jean-Philippe Lang
  
327 10 Jean-Philippe Lang
  private
328 10 Jean-Philippe Lang
  
329 10 Jean-Philippe Lang
  def find_project
330 10 Jean-Philippe Lang
    # @project variable must be set before calling the authorize filter
331 10 Jean-Philippe Lang
    @project = Project.find(params[:project_id])
332 18 Jean-Philippe Lang
  end
333 4 Jean-Philippe Lang
end
334 31 Markus Bockman
</code></pre>
335 1 Jean-Philippe Lang
336 37 Randy Syring
Retrieving the current project before the @#vote@ action could be done using a similar way.
337 31 Markus Bockman
After this, viewing and voting polls will be only available to admin users or users that have the appropriate role on the project.
338 31 Markus Bockman
339 31 Markus Bockman
If you want to display the symbols of your permissions in a multilangual way, you need to add the necessary text labels in a language file.
340 67 Jean-Philippe Lang
Simply create an *.yml (eg. @en.yml@) file in @plugins/polls/config/locales@ and fill it with labels like this:
341 31 Markus Bockman
342 31 Markus Bockman
<pre><code class="ruby">
343 31 Markus Bockman
  permission_view_polls: View Polls
344 31 Markus Bockman
  permission_vote_polls: Vote Polls
345 31 Markus Bockman
</code></pre>
346 31 Markus Bockman
347 67 Jean-Philippe Lang
In this example the created file is known as @en.yml@, but all other supported language files are also possible too.
348 31 Markus Bockman
As you can see on the example above, the labels consists of the permission symbols @:view_polls@ and @:vote_polls@ with an additional @permission_@ added at the front. 
349 31 Markus Bockman
350 4 Jean-Philippe Lang
Restart your application and point the permission section.
351 4 Jean-Philippe Lang
352 56 Thomas Winkel
h2. Creating a project module
353 26 Eric Davis
354 11 Jean-Philippe Lang
For now, the poll functionality is added to all your projects. But you may want to enable polls for some projects only.
355 11 Jean-Philippe Lang
So, let's create a 'Polls' project module. This is done by wrapping the permissions declaration inside a call to @#project_module@.
356 11 Jean-Philippe Lang
357 11 Jean-Philippe Lang
Edit @init.rb@ and change the permissions declaration:
358 18 Jean-Philippe Lang
359 18 Jean-Philippe Lang
<pre><code class="ruby">
360 18 Jean-Philippe Lang
  project_module :polls do
361 11 Jean-Philippe Lang
    permission :view_polls, :polls => :index
362 11 Jean-Philippe Lang
    permission :vote_polls, :polls => :vote
363 11 Jean-Philippe Lang
  end
364 11 Jean-Philippe Lang
</code></pre>
365 18 Jean-Philippe Lang
366 11 Jean-Philippe Lang
Restart the application and go to one of your project settings.
367 29 Vinod Singh
Click on the Modules tab. You should see the Polls module at the end of the modules list (disabled by default):
368 11 Jean-Philippe Lang
369 18 Jean-Philippe Lang
!modules.png!
370 11 Jean-Philippe Lang
371 11 Jean-Philippe Lang
You can now enable/disable polls at project level.
372 11 Jean-Philippe Lang
373 16 Jean-Philippe Lang
h2. Improving the plugin views
374 16 Jean-Philippe Lang
375 16 Jean-Philippe Lang
h3. Adding stylesheets
376 26 Eric Davis
377 16 Jean-Philippe Lang
Let's start by adding a stylesheet to our plugin views.
378 67 Jean-Philippe Lang
Create a file named @voting.css@ in the @plugins/polls/assets/stylesheets@ directory:
379 16 Jean-Philippe Lang
380 16 Jean-Philippe Lang
<pre>
381 16 Jean-Philippe Lang
a.vote { font-size: 120%; }
382 16 Jean-Philippe Lang
a.vote.yes { color: green; }
383 16 Jean-Philippe Lang
a.vote.no  { color: red; }
384 18 Jean-Philippe Lang
</pre>
385 16 Jean-Philippe Lang
386 67 Jean-Philippe Lang
When starting the application, plugin assets are automatically copied to @public/plugin_assets/polls/@ to make them available through your web server. So any change to your plugin stylesheets or javascripts needs an application restart.
387 16 Jean-Philippe Lang
388 67 Jean-Philippe Lang
Then, append the following lines at the end of @plugins/polls/app/views/polls/index.html.erb@ so that your stylesheet get included in the page header by Redmine:
389 16 Jean-Philippe Lang
390 18 Jean-Philippe Lang
<pre>
391 16 Jean-Philippe Lang
<% content_for :header_tags do %>
392 16 Jean-Philippe Lang
    <%= stylesheet_link_tag 'voting', :plugin => 'redmine_polls' %>
393 16 Jean-Philippe Lang
<% end %>
394 18 Jean-Philippe Lang
</pre>
395 16 Jean-Philippe Lang
396 16 Jean-Philippe Lang
Note that the @:plugin => 'redmine_polls'@ option is required when calling the @stylesheet_link_tag@ helper.
397 16 Jean-Philippe Lang
398 16 Jean-Philippe Lang
Javascripts can be included in plugin views using the @javascript_include_tag@ helper in the same way.
399 16 Jean-Philippe Lang
400 16 Jean-Philippe Lang
h3. Setting page title
401 16 Jean-Philippe Lang
402 16 Jean-Philippe Lang
You can set the HTML title from inside your views by using the @html_title@ helper.
403 53 Igor Zubkov
Example:
404 34 Tom Bostelmann
405 34 Tom Bostelmann
  <% html_title "Polls" %>
406 34 Tom Bostelmann
407 34 Tom Bostelmann
h2. Testing your plugin
408 34 Tom Bostelmann
409 34 Tom Bostelmann
h3. test/test_helper.rb:
410 34 Tom Bostelmann
411 34 Tom Bostelmann
Here are the contents of my test helper file:
412 34 Tom Bostelmann
413 1 Jean-Philippe Lang
<pre>
414 69 Jean-Philippe Lang
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
415 34 Tom Bostelmann
</pre>
416 34 Tom Bostelmann
417 70 Jean-Philippe Lang
h3. Sample test
418 34 Tom Bostelmann
419 70 Jean-Philippe Lang
Contents of @polls_controller_test.rb@:
420 1 Jean-Philippe Lang
421 1 Jean-Philippe Lang
<pre><code class="ruby">
422 69 Jean-Philippe Lang
require File.expand_path('../../test_helper', __FILE__)
423 34 Tom Bostelmann
424 69 Jean-Philippe Lang
class PollsControllerTest < ActionController::TestCase
425 69 Jean-Philippe Lang
  fixtures :projects
426 34 Tom Bostelmann
427 69 Jean-Philippe Lang
  def test_index
428 69 Jean-Philippe Lang
    get :index, :project_id => 1
429 34 Tom Bostelmann
430 69 Jean-Philippe Lang
    assert_response 'success'
431 69 Jean-Philippe Lang
    assert_template 'index'
432 34 Tom Bostelmann
  end
433 52 Igor Zubkov
end
434 54 Igor Zubkov
</code></pre>
435 34 Tom Bostelmann
436 70 Jean-Philippe Lang
h3. Running test
437 34 Tom Bostelmann
438 70 Jean-Philippe Lang
Initialize the test database if necessary:
439 68 Jean-Philippe Lang
440 34 Tom Bostelmann
<pre>
441 34 Tom Bostelmann
$ rake db:drop db:create db:migrate db:migrate_plugins redmine:load_default_data RAILS_ENV=test
442 48 Igor Zubkov
</pre>
443 34 Tom Bostelmann
444 69 Jean-Philippe Lang
To execute the polls_controller_test.rb:
445 34 Tom Bostelmann
446 34 Tom Bostelmann
<pre>
447 69 Jean-Philippe Lang
$ ruby plugins\polls\test\functionals\polls_controller_test.rb
448 47 Mo Morsi
</pre>
449 35 Tom Bostelmann
450 35 Tom Bostelmann
h3. Testing with permissions
451 35 Tom Bostelmann
452 1 Jean-Philippe Lang
If your plugin requires membership to a project, add the following to the beginning of your functional tests:
453 47 Mo Morsi
454 47 Mo Morsi
<pre>
455 47 Mo Morsi
def test_index
456 1 Jean-Philippe Lang
  @request.session[:user_id] = 2
457 47 Mo Morsi
  ...
458 1 Jean-Philippe Lang
end
459 47 Mo Morsi
</pre>
460 47 Mo Morsi
461 47 Mo Morsi
If your plugin requires a specific permission, you can add that to a user role like so (lookup which role is appropriate for the user in the fixtures):
462 1 Jean-Philippe Lang
463 47 Mo Morsi
<pre>
464 47 Mo Morsi
def test_index
465 1 Jean-Philippe Lang
  Role.find(1).add_permission! :my_permission
466 1 Jean-Philippe Lang
  ...
467 35 Tom Bostelmann
end
468 47 Mo Morsi
</pre>
469 47 Mo Morsi
470 47 Mo Morsi
471 47 Mo Morsi
You may enable/disable a specific module like so:
472 47 Mo Morsi
473 47 Mo Morsi
<pre>
474 47 Mo Morsi
def test_index
475 47 Mo Morsi
  Project.find(1).enabled_module_names = [:mymodule]
476 47 Mo Morsi
  ...
477 1 Jean-Philippe Lang
end
478 1 Jean-Philippe Lang
</pre>