Project

General

Profile

Plugin Tutorial » History » Version 14

Jean-Philippe Lang, 2008-08-10 20:04

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