Plugin Tutorial » History » Version 116
Tomofumi Murata, 2023-12-01 03:53
Fix vote redirect error
1 | 1 | Jean-Philippe Lang | h1. Plugin Tutorial |
---|---|---|---|
2 | 12 | Jean-Philippe Lang | |
3 | 105 | Mizuki ISHIKAWA | This tutorial is based on Redmine 4.x and 3.x. |
4 | You can view a previous version of this tutorial. |
||
5 | "Redmine 1.x":/projects/redmine/wiki/Plugin_Tutorial?version=66 |
||
6 | "Redmine 2.x":/projects/redmine/wiki/Plugin_Tutorial?version=104 |
||
7 | |||
8 | 99 | Toshi MARUYAMA | It assumes that you're familiar with the Ruby on Rails framework. |
9 | |||
10 | 30 | Vinod Singh | {{>toc}} |
11 | 1 | Jean-Philippe Lang | |
12 | h2. Creating a new Plugin |
||
13 | 40 | Nick Peelman | |
14 | You may need to set the RAILS_ENV variable in order to use the command below: |
||
15 | 32 | Jiří Křivánek | |
16 | <pre> |
||
17 | $ export RAILS_ENV="production" |
||
18 | </pre> |
||
19 | |||
20 | 59 | Harry Garrood | On windows: |
21 | |||
22 | <pre> |
||
23 | 1 | Jean-Philippe Lang | $ set RAILS_ENV=production |
24 | 59 | Harry Garrood | </pre> |
25 | |||
26 | 9 | Jean-Philippe Lang | Creating a new plugin can be done using the Redmine plugin generator. |
27 | 1 | Jean-Philippe Lang | Syntax for this generator is: |
28 | |||
29 | 105 | Mizuki ISHIKAWA | <pre>bundle exec rails generate redmine_plugin <plugin_name></pre> |
30 | 9 | Jean-Philippe Lang | |
31 | 1 | Jean-Philippe Lang | So open up a command prompt and "cd" to your redmine directory, then execute the following command: |
32 | |||
33 | <pre> |
||
34 | 105 | Mizuki ISHIKAWA | $ bundle exec rails generate redmine_plugin Polls |
35 | 67 | Jean-Philippe Lang | create plugins/polls/app |
36 | create plugins/polls/app/controllers |
||
37 | create plugins/polls/app/helpers |
||
38 | create plugins/polls/app/models |
||
39 | 1 | Jean-Philippe Lang | create plugins/polls/app/views |
40 | create plugins/polls/db/migrate |
||
41 | create plugins/polls/lib/tasks |
||
42 | create plugins/polls/assets/images |
||
43 | create plugins/polls/assets/javascripts |
||
44 | 67 | Jean-Philippe Lang | create plugins/polls/assets/stylesheets |
45 | create plugins/polls/config/locales |
||
46 | create plugins/polls/test |
||
47 | 105 | Mizuki ISHIKAWA | create plugins/polls/test/fixtures |
48 | create plugins/polls/test/unit |
||
49 | create plugins/polls/test/functional |
||
50 | create plugins/polls/test/integration |
||
51 | create plugins/polls/test/system |
||
52 | 67 | Jean-Philippe Lang | create plugins/polls/README.rdoc |
53 | create plugins/polls/init.rb |
||
54 | 1 | Jean-Philippe Lang | create plugins/polls/config/routes.rb |
55 | create plugins/polls/config/locales/en.yml |
||
56 | 67 | Jean-Philippe Lang | create plugins/polls/test/test_helper.rb |
57 | 1 | Jean-Philippe Lang | </pre> |
58 | |||
59 | The plugin structure is created in @plugins/polls@. Edit @plugins/polls/init.rb@ to adjust plugin information (name, author, description and version): |
||
60 | |||
61 | 67 | Jean-Philippe Lang | <pre><code class="ruby"> |
62 | 1 | Jean-Philippe Lang | Redmine::Plugin.register :polls do |
63 | name 'Polls plugin' |
||
64 | 105 | Mizuki ISHIKAWA | author 'Author name' |
65 | description 'This is a plugin for Redmine' |
||
66 | 1 | Jean-Philippe Lang | version '0.0.1' |
67 | 105 | Mizuki ISHIKAWA | url 'http://example.com/path/to/plugin' |
68 | author_url 'http://example.com/about' |
||
69 | 1 | Jean-Philippe Lang | end |
70 | </code></pre> |
||
71 | |||
72 | Then restart the application and point your browser to http://localhost:3000/admin/plugins. |
||
73 | After logging in, you should see your new plugin in the plugins list: |
||
74 | 71 | Jean-Philippe Lang | |
75 | 72 | Jean-Philippe Lang | p=. !plugins_list1.png! |
76 | |||
77 | 1 | Jean-Philippe Lang | Note: any change to the @init.rb@ file of your plugin requires to restart the application as it is not reloaded on each request. |
78 | |||
79 | h2. Generating a model |
||
80 | |||
81 | For now plugin doesn't store anything. Let's create a simple Poll model for our plugin. Syntax is: |
||
82 | |||
83 | <pre> |
||
84 | 105 | Mizuki ISHIKAWA | bundle exec rails generate redmine_plugin_model <plugin_name> <model_name> [field[:type][:index] field[:type][:index] ...] |
85 | 1 | Jean-Philippe Lang | </pre> |
86 | |||
87 | So, go to the command prompt and run: |
||
88 | |||
89 | <pre> |
||
90 | 105 | Mizuki ISHIKAWA | $ bundle exec rails generate redmine_plugin_model polls poll question:string yes:integer no:integer |
91 | 67 | Jean-Philippe Lang | create plugins/polls/app/models/poll.rb |
92 | create plugins/polls/test/unit/poll_test.rb |
||
93 | 105 | Mizuki ISHIKAWA | create plugins/polls/db/migrate/xxxxxxxxxxxx_create_polls.rb |
94 | 1 | Jean-Philippe Lang | </pre> |
95 | 67 | Jean-Philippe Lang | |
96 | 105 | Mizuki ISHIKAWA | This creates the Poll model and the corresponding migration file @xxxxxxxxxxxx_create_polls.rb@ in @plugins/polls/db/migrate@: |
97 | 1 | Jean-Philippe Lang | |
98 | 67 | Jean-Philippe Lang | <pre><code class="ruby"> |
99 | 105 | Mizuki ISHIKAWA | class CreatePolls < ActiveRecord::Migration[5.2] |
100 | 67 | Jean-Philippe Lang | def change |
101 | create_table :polls do |t| |
||
102 | t.string :question |
||
103 | 105 | Mizuki ISHIKAWA | t.integer :yes, default: 0 |
104 | t.integer :no, default: 0 |
||
105 | 67 | Jean-Philippe Lang | end |
106 | end |
||
107 | 1 | Jean-Philippe Lang | end |
108 | </code></pre> |
||
109 | |||
110 | 105 | Mizuki ISHIKAWA | NOTE: For Redmine 3.x @class CreatePolls < ActiveRecord::Migration[5.2]@ is @class CreatePolls < ActiveRecord::Migration@. |
111 | |||
112 | 14 | Jean-Philippe Lang | You can adjust your migration file (eg. default values...) then migrate the database using the following command: |
113 | 67 | Jean-Philippe Lang | |
114 | 100 | Toshi MARUYAMA | <pre> |
115 | 1 | Jean-Philippe Lang | $ bundle exec rake redmine:plugins:migrate |
116 | 67 | Jean-Philippe Lang | |
117 | Migrating polls (Polls plugin)... |
||
118 | == CreatePolls: migrating ==================================================== |
||
119 | -- create_table(:polls) |
||
120 | -> 0.0410s |
||
121 | == CreatePolls: migrated (0.0420s) =========================================== |
||
122 | 64 | Anonymous | </pre> |
123 | |||
124 | 104 | Go MAEDA | Note that each plugin has its own set of migrations. |
125 | 24 | Eric Davis | |
126 | 100 | Toshi MARUYAMA | 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 |
127 | 77 | mina Beshay | |
128 | 1 | Jean-Philippe Lang | <pre> |
129 | 105 | Mizuki ISHIKAWA | bundle exec rails console |
130 | >> Poll.create(question: "Can you see this poll") |
||
131 | >> Poll.create(question: "And can you see this other poll") |
||
132 | 1 | Jean-Philippe Lang | >> exit |
133 | 15 | Jean-Philippe Lang | </pre> |
134 | |||
135 | 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: |
136 | 1 | Jean-Philippe Lang | |
137 | 15 | Jean-Philippe Lang | <pre><code class="ruby"> |
138 | class Poll < ActiveRecord::Base |
||
139 | 1 | Jean-Philippe Lang | def vote(answer) |
140 | increment(answer == 'yes' ? :yes : :no) |
||
141 | 60 | Mischa The Evil | end |
142 | 57 | Etienne Massip | end |
143 | 9 | Jean-Philippe Lang | </code></pre> |
144 | |||
145 | 67 | Jean-Philippe Lang | h2. Generating a controller |
146 | 3 | Jean-Philippe Lang | |
147 | For now, the plugin doesn't do anything. So let's create a controller for our plugin. |
||
148 | 1 | Jean-Philippe Lang | We can use the plugin controller generator for that. Syntax is: |
149 | 100 | Toshi MARUYAMA | |
150 | 105 | Mizuki ISHIKAWA | <pre>bundle exec rails generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>]</pre> |
151 | 1 | Jean-Philippe Lang | |
152 | So go back to the command prompt and run: |
||
153 | |||
154 | 101 | Toshi MARUYAMA | <pre> |
155 | 105 | Mizuki ISHIKAWA | $ bundle exec rails generate redmine_plugin_controller Polls polls index vote |
156 | 67 | Jean-Philippe Lang | create plugins/polls/app/controllers/polls_controller.rb |
157 | create plugins/polls/app/helpers/polls_helper.rb |
||
158 | create plugins/polls/test/functional/polls_controller_test.rb |
||
159 | create plugins/polls/app/views/polls/index.html.erb |
||
160 | 1 | Jean-Philippe Lang | create plugins/polls/app/views/polls/vote.html.erb |
161 | 3 | Jean-Philippe Lang | </pre> |
162 | 67 | Jean-Philippe Lang | |
163 | 1 | Jean-Philippe Lang | A controller @PollsController@ with 2 actions (@#index@ and @#vote@) is created. |
164 | |||
165 | Edit @plugins/polls/app/controllers/polls_controller.rb@ to implement these 2 actions. |
||
166 | |||
167 | <pre><code class="ruby"> |
||
168 | 3 | Jean-Philippe Lang | class PollsController < ApplicationController |
169 | 7 | Jean-Philippe Lang | def index |
170 | 72 | Jean-Philippe Lang | @polls = Poll.all |
171 | 25 | Eric Davis | end |
172 | |||
173 | 1 | Jean-Philippe Lang | def vote |
174 | 25 | Eric Davis | poll = Poll.find(params[:id]) |
175 | 3 | Jean-Philippe Lang | poll.vote(params[:answer]) |
176 | 1 | Jean-Philippe Lang | if poll.save |
177 | flash[:notice] = 'Vote saved.' |
||
178 | 3 | Jean-Philippe Lang | end |
179 | 105 | Mizuki ISHIKAWA | redirect_to polls_path(project_id: params[:project_id]) |
180 | 5 | Jean-Philippe Lang | end |
181 | 26 | Eric Davis | end |
182 | 3 | Jean-Philippe Lang | </code></pre> |
183 | |||
184 | 67 | Jean-Philippe Lang | Then edit @plugins/polls/app/views/polls/index.html.erb@ that will display existing polls: |
185 | 3 | Jean-Philippe Lang | |
186 | 74 | Etienne Massip | <pre><code class="erb"> |
187 | 3 | Jean-Philippe Lang | <h2>Polls</h2> |
188 | 19 | Jean-Philippe Lang | |
189 | 50 | Igor Zubkov | <% @polls.each do |poll| %> |
190 | 1 | Jean-Philippe Lang | <p> |
191 | 105 | Mizuki ISHIKAWA | <%= poll.question %>? |
192 | <%= link_to 'Yes', { action: 'vote', id: poll[:id], answer: 'yes', project_id: @project }, method: :post %> <%= poll.yes %> / |
||
193 | <%= link_to 'No', { action: 'vote', id: poll[:id], answer: 'no', project_id: @project }, method: :post %> <%= poll.no %> |
||
194 | 1 | Jean-Philippe Lang | </p> |
195 | 3 | Jean-Philippe Lang | <% end %> |
196 | 74 | Etienne Massip | </code></pre> |
197 | 72 | Jean-Philippe Lang | |
198 | 18 | Jean-Philippe Lang | You can remove @plugins/polls/app/views/polls/vote.html.erb@ since no rendering is done by the @#vote@ action. |
199 | 72 | Jean-Philippe Lang | |
200 | 1 | Jean-Philippe Lang | h3. Adding routes |
201 | 72 | Jean-Philippe Lang | |
202 | 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: |
||
203 | 1 | Jean-Philippe Lang | |
204 | 67 | Jean-Philippe Lang | <pre><code class="ruby"> |
205 | 105 | Mizuki ISHIKAWA | get 'polls', to: 'polls#index' |
206 | post 'post/:id/vote', to: 'polls#vote' |
||
207 | 67 | Jean-Philippe Lang | </code></pre> |
208 | |||
209 | 72 | Jean-Philippe Lang | You can find more information about Rails routes here: http://guides.rubyonrails.org/routing.html. |
210 | |||
211 | 38 | Randy Syring | Now, restart the application and point your browser to http://localhost:3000/polls. |
212 | 1 | Jean-Philippe Lang | You should see the 2 polls and you should be able to vote for them: |
213 | 38 | Randy Syring | |
214 | 71 | Jean-Philippe Lang | p=. !pools1.png! |
215 | 4 | Jean-Philippe Lang | |
216 | 72 | Jean-Philippe Lang | h2. Internationalization |
217 | 4 | Jean-Philippe Lang | |
218 | 67 | Jean-Philippe Lang | The translation files must be stored in config/locales, eg. @plugins/polls/config/locales/@. |
219 | 1 | Jean-Philippe Lang | |
220 | h2. Extending menus |
||
221 | 4 | Jean-Philippe Lang | |
222 | 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. |
||
223 | 18 | Jean-Philippe Lang | So let's add a new item to the application menu. |
224 | 4 | Jean-Philippe Lang | |
225 | 18 | Jean-Philippe Lang | h3. Extending the application menu |
226 | 4 | Jean-Philippe Lang | |
227 | 1 | 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: |
228 | |||
229 | 4 | Jean-Philippe Lang | <pre><code class="ruby"> |
230 | Redmine::Plugin.register :redmine_polls do |
||
231 | 1 | Jean-Philippe Lang | [...] |
232 | 4 | Jean-Philippe Lang | |
233 | 105 | Mizuki ISHIKAWA | menu :application_menu, :polls, { controller: 'polls', action: 'index' }, caption: 'Polls' |
234 | 1 | Jean-Philippe Lang | end |
235 | 4 | Jean-Philippe Lang | </code></pre> |
236 | 42 | Mischa The Evil | |
237 | 4 | Jean-Philippe Lang | Syntax is: |
238 | |||
239 | menu(menu_name, item_name, url, options={}) |
||
240 | 1 | Jean-Philippe Lang | |
241 | 4 | Jean-Philippe Lang | There are five menus that you can extend: |
242 | |||
243 | 1 | Jean-Philippe Lang | * @:top_menu@ - the top left menu |
244 | * @:account_menu@ - the top right menu with sign in/sign out links |
||
245 | 4 | Jean-Philippe Lang | * @:application_menu@ - the main menu displayed when the user is not inside a project |
246 | * @:project_menu@ - the main menu displayed when the user is inside a project |
||
247 | * @:admin_menu@ - the menu displayed on the Administration page (can only insert after Settings, before Plugins) |
||
248 | |||
249 | Available options are: |
||
250 | |||
251 | 1 | Jean-Philippe Lang | * @:param@ - the parameter key that is used for the project id (default is @:id@) |
252 | 36 | Jérémie Delaitre | * @:if@ - a Proc that is called before rendering the item, the item is displayed only if it returns true |
253 | 4 | Jean-Philippe Lang | * @:caption@ - the menu caption that can be: |
254 | |||
255 | * a localized string Symbol |
||
256 | 29 | Vinod Singh | * a String |
257 | 4 | Jean-Philippe Lang | * a Proc that can take the project as argument |
258 | 1 | Jean-Philippe Lang | |
259 | 105 | Mizuki ISHIKAWA | * @:before@, @:after@ - specify where the menu item should be inserted (eg. @after: :activity@) |
260 | * @:first@, @:last@ - if set to true, the item will stay at the beginning/end of the menu (eg. @last: true@) |
||
261 | 4 | Jean-Philippe Lang | * @:html@ - a hash of html options that are passed to @link_to@ when rendering the menu item |
262 | 1 | Jean-Philippe Lang | |
263 | 104 | Go MAEDA | In our example, we've added an item to the application menu which is empty by default. |
264 | 105 | Mizuki ISHIKAWA | Restart the application and go to http://localhost:3000/projects: |
265 | 6 | Jean-Philippe Lang | |
266 | 71 | Jean-Philippe Lang | p=. !application_menu.png! |
267 | 6 | Jean-Philippe Lang | |
268 | 105 | Mizuki ISHIKAWA | Now you can access the polls by clicking the Polls tab that appears when the user is not inside a project. |
269 | 6 | Jean-Philippe Lang | |
270 | h3. Extending the project menu |
||
271 | 51 | Igor Zubkov | |
272 | 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. |
273 | 6 | Jean-Philippe Lang | Open @init.rb@ and replace the line that was added just before with these 2 lines: |
274 | |||
275 | <pre><code class="ruby"> |
||
276 | 18 | Jean-Philippe Lang | Redmine::Plugin.register :redmine_polls do |
277 | [...] |
||
278 | 6 | Jean-Philippe Lang | |
279 | 105 | Mizuki ISHIKAWA | permission :polls, { polls: [:index, :vote] }, public: true |
280 | menu :project_menu, :polls, { controller: 'polls', action: 'index' }, caption: 'Polls', after: :activity, param: :project_id |
||
281 | 1 | Jean-Philippe Lang | end |
282 | 6 | Jean-Philippe Lang | </code></pre> |
283 | 67 | Jean-Philippe Lang | |
284 | 6 | 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: |
285 | 67 | Jean-Philippe Lang | |
286 | 1 | Jean-Philippe Lang | p=. !project_menu.png! |
287 | 19 | Jean-Philippe Lang | |
288 | 6 | Jean-Philippe Lang | If you click the Polls tab (in 3rd position), you should notice that the project menu is no longer displayed. |
289 | 1 | Jean-Philippe Lang | To make the project menu visible, you have to initialize the controller's instance variable @@project@. |
290 | |||
291 | 61 | Harry Garrood | Edit your PollsController to do so: |
292 | |||
293 | 6 | Jean-Philippe Lang | <pre><code class="ruby"> |
294 | def index |
||
295 | 18 | Jean-Philippe Lang | @project = Project.find(params[:project_id]) |
296 | 105 | Mizuki ISHIKAWA | @polls = Poll.all # @project.polls |
297 | 39 | Ric Turley | end |
298 | 116 | Tomofumi Murata | |
299 | |||
300 | def vote |
||
301 | poll = Poll.find(params[:id]) |
||
302 | poll.vote(params[:answer]) |
||
303 | if poll.save |
||
304 | flash[:notice] = 'Vote saved.' |
||
305 | end |
||
306 | redirect_to :action => 'index', project_id: params[:project_id] |
||
307 | end |
||
308 | 1 | Jean-Philippe Lang | </code></pre> |
309 | 4 | Jean-Philippe Lang | |
310 | 105 | Mizuki ISHIKAWA | The project id is available in the @:project_id@ param because of the @param: :project_id@ option in the menu item declaration above. |
311 | 18 | Jean-Philippe Lang | |
312 | 71 | Jean-Philippe Lang | Now, you should see the project menu when viewing the polls: |
313 | 10 | Jean-Philippe Lang | |
314 | 1 | Jean-Philippe Lang | p=. !project_menu_pools.png! |
315 | 103 | luigifab ! | |
316 | h3. Removing item in menu |
||
317 | |||
318 | 1 | Jean-Philippe Lang | To remove an item in a menu, you can use @delete_menu_item@ like in this example: |
319 | 103 | luigifab ! | |
320 | <pre><code class="ruby"> |
||
321 | Redmine::Plugin.register :redmine_polls do |
||
322 | [...] |
||
323 | |||
324 | delete_menu_item :top_menu, :my_page |
||
325 | delete_menu_item :top_menu, :help |
||
326 | delete_menu_item :project_menu, :overview |
||
327 | delete_menu_item :project_menu, :activity |
||
328 | delete_menu_item :project_menu, :news |
||
329 | end |
||
330 | </code></pre> |
||
331 | |||
332 | 10 | Jean-Philippe Lang | h2. Adding new permissions |
333 | 20 | Jean-Philippe Lang | |
334 | 18 | Jean-Philippe Lang | For now, anyone can vote for polls. Let's make it more configurable by changing the permission declaration. |
335 | 105 | Mizuki ISHIKAWA | 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). |
336 | 1 | Jean-Philippe Lang | |
337 | 67 | Jean-Philippe Lang | Edit @plugins/polls/init.rb@ to replace the previous permission declaration with these 2 lines: |
338 | 10 | Jean-Philippe Lang | |
339 | <pre><code class="ruby"> |
||
340 | 105 | Mizuki ISHIKAWA | permission :view_polls, polls: :index |
341 | permission :vote_polls, polls: :vote |
||
342 | 1 | Jean-Philippe Lang | </code></pre> |
343 | 10 | Jean-Philippe Lang | |
344 | 89 | Robert Schneider | Restart the application and go to http://localhost:3000/roles/permissions: |
345 | 18 | Jean-Philippe Lang | |
346 | 71 | Jean-Philippe Lang | p=. !permissions1.png! |
347 | 10 | Jean-Philippe Lang | |
348 | You're now able to give these permissions to your existing roles. |
||
349 | 1 | Jean-Philippe Lang | |
350 | 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. |
351 | 10 | Jean-Philippe Lang | |
352 | Here is how it would look like for the @#index@ action: |
||
353 | |||
354 | <pre><code class="ruby"> |
||
355 | 19 | Jean-Philippe Lang | class PollsController < ApplicationController |
356 | 109 | keineahnung 2345 | before_action :find_project, :authorize, only: [:index, :vote] |
357 | 1 | Jean-Philippe Lang | |
358 | 10 | Jean-Philippe Lang | [...] |
359 | |||
360 | def index |
||
361 | 105 | Mizuki ISHIKAWA | @polls = Poll.all # @project.polls |
362 | 10 | Jean-Philippe Lang | end |
363 | |||
364 | [...] |
||
365 | |||
366 | private |
||
367 | |||
368 | def find_project |
||
369 | 18 | Jean-Philippe Lang | # @project variable must be set before calling the authorize filter |
370 | 1 | Jean-Philippe Lang | @project = Project.find(params[:project_id]) |
371 | end |
||
372 | end |
||
373 | 4 | Jean-Philippe Lang | </code></pre> |
374 | 1 | Jean-Philippe Lang | |
375 | 31 | Markus Bockman | Retrieving the current project before the @#vote@ action could be done using a similar way. |
376 | 37 | Randy Syring | After this, viewing and voting polls will be only available to admin users or users that have the appropriate role on the project. |
377 | 31 | Markus Bockman | |
378 | 104 | Go MAEDA | 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. |
379 | 83 | Denis Savitskiy | Simply create an *.yml (eg. @en.yml@) file in @plugins/polls/config/locales@ and fill it with labels like this: |
380 | 86 | Lennart Nordgreen | |
381 | 31 | Markus Bockman | <pre><code class="yaml"> |
382 | "en": |
||
383 | 83 | Denis Savitskiy | permission_view_polls: View Polls |
384 | 1 | Jean-Philippe Lang | permission_vote_polls: Vote Polls |
385 | </code></pre> |
||
386 | 67 | Jean-Philippe Lang | |
387 | 31 | Markus Bockman | In this example the created file is known as @en.yml@, but all other supported language files are also possible too. |
388 | 4 | Jean-Philippe Lang | 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. |
389 | |||
390 | 56 | Thomas Winkel | Restart your application and point the permission section. |
391 | 1 | Jean-Philippe Lang | |
392 | 26 | Eric Davis | h2. Creating a project module |
393 | 11 | Jean-Philippe Lang | |
394 | 1 | 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. |
395 | 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@. |
396 | |||
397 | Edit @init.rb@ and change the permissions declaration: |
||
398 | 18 | Jean-Philippe Lang | |
399 | <pre><code class="ruby"> |
||
400 | project_module :polls do |
||
401 | 105 | Mizuki ISHIKAWA | permission :view_polls, polls: :index |
402 | permission :vote_polls, polls: :vote |
||
403 | 11 | Jean-Philippe Lang | end |
404 | </code></pre> |
||
405 | 18 | Jean-Philippe Lang | |
406 | 11 | Jean-Philippe Lang | Restart the application and go to one of your project settings. |
407 | 29 | Vinod Singh | Click on the Modules tab. You should see the Polls module at the end of the modules list (disabled by default): |
408 | 11 | Jean-Philippe Lang | |
409 | 71 | Jean-Philippe Lang | p=. !modules.png! |
410 | 11 | Jean-Philippe Lang | |
411 | You can now enable/disable polls at project level. |
||
412 | |||
413 | 16 | Jean-Philippe Lang | h2. Improving the plugin views |
414 | |||
415 | h3. Adding stylesheets |
||
416 | 26 | Eric Davis | |
417 | 16 | Jean-Philippe Lang | Let's start by adding a stylesheet to our plugin views. |
418 | 67 | Jean-Philippe Lang | Create a file named @voting.css@ in the @plugins/polls/assets/stylesheets@ directory: |
419 | 16 | Jean-Philippe Lang | |
420 | 83 | Denis Savitskiy | <pre><code class="css"> |
421 | 16 | Jean-Philippe Lang | a.vote { font-size: 120%; } |
422 | a.vote.yes { color: green; } |
||
423 | a.vote.no { color: red; } |
||
424 | 83 | Denis Savitskiy | </code></pre> |
425 | 16 | Jean-Philippe Lang | |
426 | 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. |
427 | 16 | Jean-Philippe Lang | |
428 | 90 | Robert Schneider | The introduced classes need to be used by the links. So change in file @plugins/polls/app/views/polls/index.html.erb@ the link declarations to: |
429 | |||
430 | <pre><code class="erb"> |
||
431 | 110 | keineahnung 2345 | <%= link_to 'Yes', {action: 'vote', id: poll[:id], answer: 'yes', project_id: @project }, method: :post, class: 'vote yes' %> (<%= poll.yes %>) |
432 | <%= link_to 'No', {action: 'vote', id: poll[:id], answer: 'no', project_id: @project }, method: :post, class: 'vote no' %> (<%= poll.no %>) |
||
433 | 90 | Robert Schneider | </code></pre> |
434 | |||
435 | 16 | Jean-Philippe Lang | Then, append the following lines at the end of @index.html.erb@ so that your stylesheet get included in the page header by Redmine: |
436 | 84 | Mischa The Evil | |
437 | 16 | Jean-Philippe Lang | <pre><code class="erb"> |
438 | 73 | Jean-Philippe Lang | <% content_for :header_tags do %> |
439 | 105 | Mizuki ISHIKAWA | <%= stylesheet_link_tag 'voting', plugin: 'polls' %> |
440 | 1 | Jean-Philippe Lang | <% end %> |
441 | 83 | Denis Savitskiy | </code></pre> |
442 | 16 | Jean-Philippe Lang | |
443 | 105 | Mizuki ISHIKAWA | Note that the @plugin: 'polls'@ option is required when calling the @stylesheet_link_tag@ helper. |
444 | 16 | Jean-Philippe Lang | |
445 | Javascripts can be included in plugin views using the @javascript_include_tag@ helper in the same way. |
||
446 | 1 | Jean-Philippe Lang | |
447 | 16 | Jean-Philippe Lang | h3. Setting page title |
448 | 1 | Jean-Philippe Lang | |
449 | 16 | Jean-Philippe Lang | You can set the HTML title from inside your views by using the @html_title@ helper. |
450 | 1 | Jean-Philippe Lang | Example: |
451 | 84 | Mischa The Evil | |
452 | 34 | Tom Bostelmann | <pre><code class="erb"> |
453 | 83 | Denis Savitskiy | <% html_title "Polls" %> |
454 | 34 | Tom Bostelmann | </code></pre> |
455 | 75 | Jean-Philippe Lang | |
456 | h2. Using hooks |
||
457 | 1 | Jean-Philippe Lang | |
458 | 76 | Jean-Philippe Lang | h3. Hooks in views |
459 | |||
460 | Hooks in Redmine views lets you insert custom content to regular Redmine views. For example, looking at source:tags/2.0.0/app/views/projects/show.html.erb#L52 shows that there are 2 hooks available: one named @:view_projects_show_left@ for adding content to the left part and one named @:view_projects_show_right@ for adding content to the right part of the view. |
||
461 | |||
462 | 91 | Robert Schneider | To use one or more hooks in views, you need to create a class that inherits from @Redmine::Hook::ViewListener@ and implement methods named with the hook(s) you want to use. To append some content to the project overview, add a class to your plugin and require it in your @init.rb@, then implement methods whose name match the hook names. |
463 | 76 | Jean-Philippe Lang | |
464 | 92 | Robert Schneider | For our plugin create a file @plugins/polls/lib/polls_hook_listener.rb@ with this content: |
465 | 76 | Jean-Philippe Lang | |
466 | <pre><code class="ruby"> |
||
467 | 1 | Jean-Philippe Lang | class PollsHookListener < Redmine::Hook::ViewListener |
468 | 91 | Robert Schneider | def view_projects_show_left(context = {}) |
469 | 76 | Jean-Philippe Lang | return content_tag("p", "Custom content added to the left") |
470 | 1 | Jean-Philippe Lang | end |
471 | 76 | Jean-Philippe Lang | |
472 | 91 | Robert Schneider | def view_projects_show_right(context = {}) |
473 | 1 | Jean-Philippe Lang | return content_tag("p", "Custom content added to the right") |
474 | end |
||
475 | end |
||
476 | </code></pre> |
||
477 | |||
478 | 91 | Robert Schneider | Prepend this line to @plugins/polls/init.rb@: |
479 | |||
480 | <pre><code class="ruby"> |
||
481 | 92 | Robert Schneider | require_dependency 'polls_hook_listener' |
482 | 91 | Robert Schneider | </code></pre> |
483 | |||
484 | 92 | Robert Schneider | Restart Redmine and have a look into the overview tab of a project. You should see the strings on the left and the right side in the overview. |
485 | 76 | Jean-Philippe Lang | |
486 | 92 | Robert Schneider | You can also use the @render_on@ helper to render a partial. In our plugin you have to replace the just created content in @plugins/polls/lib/polls_hook_listener.rb@ with: |
487 | 76 | Jean-Philippe Lang | |
488 | 1 | Jean-Philippe Lang | <pre><code class="ruby"> |
489 | 76 | Jean-Philippe Lang | class PollsHookListener < Redmine::Hook::ViewListener |
490 | 105 | Mizuki ISHIKAWA | render_on :view_projects_show_left, partial: "polls/project_overview" |
491 | 76 | Jean-Philippe Lang | end |
492 | </code></pre> |
||
493 | |||
494 | 93 | Robert Schneider | Add the partial to your plugin by creating the file @app/views/polls/_project_overview.html.erb@. Its content (use some text like 'Message from Hook!') will be appended to the left part of the project overview. Don't forget to restart Redmine. |
495 | 76 | Jean-Philippe Lang | |
496 | h3. Hooks in controllers |
||
497 | |||
498 | 75 | Jean-Philippe Lang | TODO |
499 | |||
500 | h2. Making your plugin configurable |
||
501 | |||
502 | 81 | Paul Kerr | Each plugin registered with Redmine is displayed on the admin/plugins page. Support for a basic configuration mechanism is supplied by the Settings controller. This feature is enabled by adding the "settings" method to the plugin registration block in a plugin's init.rb file. |
503 | |||
504 | <pre><code class="ruby"> |
||
505 | 106 | Mischa The Evil | Redmine::Plugin.register :polls do |
506 | 81 | Paul Kerr | [ ... ] |
507 | |||
508 | 105 | Mizuki ISHIKAWA | settings default: {'empty' => true}, partial: 'settings/poll_settings' |
509 | 81 | Paul Kerr | end |
510 | </code></pre> |
||
511 | |||
512 | 106 | Mischa The Evil | Adding this will accomplish two things. First, it will add a "Configure" link to the description block for the plugin in the admin/plugins list. Following this link will cause a common plugin configuration template view to be loaded which will in turn render the partial view referenced by :partial. Calling the settings method will also add support in the Setting module for the plugin. The Setting model will store and retrieve a serialized hash based on the plugin name. This hash is accessed using the Setting method name in the form plugin_<plugin name>. For this example, the hash can be accessed by calling Setting.plugin_polls. |
513 | 81 | Paul Kerr | |
514 | p=. !plugin_with_config.png! |
||
515 | |||
516 | The view referenced by the :partial hash key passed to the settings method will be loaded as a partial within the plugin configuration view. The basic page layout is constrained by the plugin configuration view: a form is declared and the submit button is generated. The partial is pulled into the view inside a table div inside the form. Configuration settings for the plugin will be displayed and can be modified via standard HTML form elements. |
||
517 | |||
518 | p=. !plugin_config_view.png! |
||
519 | |||
520 | 85 | Jean-Baptiste Barth | *NB* : if two plugins have the same partial name for settings, the first will override the second's settings page. So be sure you give a unique name to your settings partial. |
521 | |||
522 | 106 | Mischa The Evil | When the page is submitted, the settings_controller will take the parameter hash referenced by 'settings' and store it directly in a serialized format in Setting.plugin_polls. Each time the page is generated the current value of Setting.plugin_polls will be assigned to the local variable settings. |
523 | 81 | Paul Kerr | |
524 | 111 | keineahnung 2345 | Create a file named @plugins/polls/app/views/settings/_poll_settings.erb@ and fill it with following content: |
525 | |||
526 | 81 | Paul Kerr | <pre><code class="erb"> |
527 | <table> |
||
528 | <tbody> |
||
529 | <tr> |
||
530 | 98 | Denis Savitskiy | <th>Notification Default Address</th> |
531 | <td> |
||
532 | 1 | Jean-Philippe Lang | <input type="text" id="settings_notification_default" |
533 | 98 | Denis Savitskiy | value="<%= settings['notification_default'] %>" |
534 | name="settings[notification_default]" > |
||
535 | </td> |
||
536 | 81 | Paul Kerr | </tr> |
537 | </tbody> |
||
538 | </table> |
||
539 | </code></pre> |
||
540 | |||
541 | 106 | Mischa The Evil | In the example above, the configuration form was not created using Rails form helpers. This is because there is no @settings model but only the setting hash. Form helpers will attempt to access attributes using model accessor methods which do not exist. For example, a call to @settings.notification_default will fail. The value set by this form is accessed as Setting.plugin_polls['notification_default']. |
542 | 81 | Paul Kerr | |
543 | 106 | Mischa The Evil | Finally, the :default in the settings method call is to register a value that will be returned from the Setting.plugin_polls call if nothing has been stored in the settings table for this plugin. |
544 | 75 | Jean-Philippe Lang | |
545 | 34 | Tom Bostelmann | h2. Testing your plugin |
546 | |||
547 | 112 | keineahnung 2345 | h3. plugins/polls/test/test_helper.rb |
548 | 34 | Tom Bostelmann | |
549 | Here are the contents of my test helper file: |
||
550 | |||
551 | 1 | Jean-Philippe Lang | <pre> |
552 | 69 | Jean-Philippe Lang | require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper') |
553 | 34 | Tom Bostelmann | </pre> |
554 | |||
555 | 70 | Jean-Philippe Lang | h3. Sample test |
556 | 34 | Tom Bostelmann | |
557 | 113 | keineahnung 2345 | Contents of @plugins/polls/test/functional/polls_controller_test.rb@ for Redmine 4.x: |
558 | 108 | Juno NISHIZAKI | |
559 | <pre><code class="ruby"> |
||
560 | require File.expand_path('../../test_helper', __FILE__) |
||
561 | |||
562 | class PollsControllerTest < ActionController::TestCase |
||
563 | fixtures :projects |
||
564 | |||
565 | def test_index |
||
566 | get :index, params: { project_id: 1 } |
||
567 | |||
568 | assert_response :success |
||
569 | assert_template 'index' |
||
570 | end |
||
571 | end |
||
572 | </code></pre> |
||
573 | |||
574 | 113 | keineahnung 2345 | Contents of @plugins/polls/test/functional/polls_controller_test.rb@ for Redmine 3.x: |
575 | 1 | Jean-Philippe Lang | |
576 | <pre><code class="ruby"> |
||
577 | 69 | Jean-Philippe Lang | require File.expand_path('../../test_helper', __FILE__) |
578 | 34 | Tom Bostelmann | |
579 | 69 | Jean-Philippe Lang | class PollsControllerTest < ActionController::TestCase |
580 | fixtures :projects |
||
581 | 34 | Tom Bostelmann | |
582 | 69 | Jean-Philippe Lang | def test_index |
583 | 105 | Mizuki ISHIKAWA | get :index, project_id: 1 |
584 | 34 | Tom Bostelmann | |
585 | 88 | Niklaus Giger | assert_response :success |
586 | 69 | Jean-Philippe Lang | assert_template 'index' |
587 | 34 | Tom Bostelmann | end |
588 | 52 | Igor Zubkov | end |
589 | 54 | Igor Zubkov | </code></pre> |
590 | 34 | Tom Bostelmann | |
591 | 70 | Jean-Philippe Lang | h3. Running test |
592 | 34 | Tom Bostelmann | |
593 | 70 | Jean-Philippe Lang | Initialize the test database if necessary: |
594 | 68 | Jean-Philippe Lang | |
595 | 34 | Tom Bostelmann | <pre> |
596 | 114 | Jonathan Cormier | $ RAILS_ENV=test bundle exec rake db:drop db:create db:migrate redmine:plugins:migrate redmine:load_default_data |
597 | 48 | Igor Zubkov | </pre> |
598 | 34 | Tom Bostelmann | |
599 | 69 | Jean-Philippe Lang | To execute the polls_controller_test.rb: |
600 | 34 | Tom Bostelmann | |
601 | <pre> |
||
602 | 107 | Mizuki ISHIKAWA | $ RAILS_ENV=test bundle exec rake test TEST=plugins/polls/test/functional/polls_controller_test.rb |
603 | 47 | Mo Morsi | </pre> |
604 | 35 | Tom Bostelmann | |
605 | h3. Testing with permissions |
||
606 | |||
607 | 1 | Jean-Philippe Lang | If your plugin requires membership to a project, add the following to the beginning of your functional tests: |
608 | 47 | Mo Morsi | |
609 | 82 | Denis Savitskiy | <pre><code class="ruby"> |
610 | 47 | Mo Morsi | def test_index |
611 | 1 | Jean-Philippe Lang | @request.session[:user_id] = 2 |
612 | 47 | Mo Morsi | ... |
613 | 1 | Jean-Philippe Lang | end |
614 | 82 | Denis Savitskiy | </code></pre> |
615 | 47 | Mo Morsi | |
616 | 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): |
||
617 | 1 | Jean-Philippe Lang | |
618 | 82 | Denis Savitskiy | <pre><code class="ruby"> |
619 | 47 | Mo Morsi | def test_index |
620 | 1 | Jean-Philippe Lang | Role.find(1).add_permission! :my_permission |
621 | ... |
||
622 | 35 | Tom Bostelmann | end |
623 | 82 | Denis Savitskiy | </code></pre> |
624 | 47 | Mo Morsi | |
625 | |||
626 | You may enable/disable a specific module like so: |
||
627 | |||
628 | 82 | Denis Savitskiy | <pre><code class="ruby"> |
629 | 47 | Mo Morsi | def test_index |
630 | Project.find(1).enabled_module_names = [:mymodule] |
||
631 | ... |
||
632 | 1 | Jean-Philippe Lang | end |
633 | 82 | Denis Savitskiy | </code></pre> |
634 | 94 | @ go2null | |
635 | 95 | @ go2null | h3. Reference file hierarchy |
636 | 94 | @ go2null | |
637 | 96 | @ go2null | Here is a simple list of all the files and directories mentioned in this Tutorial and [[Hooks]]. This is useful to ensure standard paths are used and also useful for newbies to know here files should go. |
638 | 94 | @ go2null | |
639 | <pre> |
||
640 | plugins/PLUGIN/README.rdoc |
||
641 | plugins/PLUGIN/init.rb |
||
642 | plugins/PLUGIN/app/ |
||
643 | plugins/PLUGIN/app/controllers/ |
||
644 | 95 | @ go2null | plugins/PLUGIN/app/controllers/CONTROLLER_controller.rb |
645 | 94 | @ go2null | plugins/PLUGIN/app/helpers/ |
646 | 96 | @ go2null | plugins/PLUGIN/app/helpers/CONTROLLER_helper.rb |
647 | 94 | @ go2null | plugins/PLUGIN/app/models/ |
648 | plugins/PLUGIN/app/models/MODEL.rb |
||
649 | plugins/PLUGIN/app/views/ |
||
650 | 95 | @ go2null | plugins/PLUGIN/app/views/CONTROLLER/ |
651 | 1 | Jean-Philippe Lang | plugins/PLUGIN/app/views/CONTROLLER/_PARTIAL.html.erb |
652 | plugins/PLUGIN/app/views/CONTROLLER/CONTROLLER-ACTION.html.erb |
||
653 | 96 | @ go2null | plugins/PLUGIN/app/views/hooks/ |
654 | plugins/PLUGIN/app/views/hooks/_HOOK.html.erb |
||
655 | 94 | @ go2null | plugins/PLUGIN/app/views/settings/ |
656 | plugins/PLUGIN/app/views/settings/_MODEL_settings.html.erb |
||
657 | plugins/PLUGIN/assets/ |
||
658 | plugins/PLUGIN/assets/images/ |
||
659 | plugins/PLUGIN/assets/javascripts/ |
||
660 | plugins/PLUGIN/assets/stylesheets/ |
||
661 | plugins/PLUGIN/assets/stylesheets/voting.css |
||
662 | plugins/PLUGIN/config/ |
||
663 | plugins/PLUGIN/config/locales/ |
||
664 | plugins/PLUGIN/config/locales/en.yml |
||
665 | 1 | Jean-Philippe Lang | plugins/PLUGIN/config/routes.rb |
666 | 94 | @ go2null | plugins/PLUGIN/db/ |
667 | plugins/PLUGIN/db/migrate/ |
||
668 | 96 | @ go2null | plugins/PLUGIN/db/migrate/001_create_MODELs.rb |
669 | 1 | Jean-Philippe Lang | plugins/PLUGIN/lib/ |
670 | 94 | @ go2null | plugins/PLUGIN/lib/PLUGIN_hook_listener.rb |
671 | 96 | @ go2null | plugins/PLUGIN/lib/PLUGIN/ |
672 | plugins/PLUGIN/lib/PLUGIN/hooks.rb |
||
673 | 97 | @ go2null | plugins/PLUGIN/lib/PLUGIN/MODEL_patch.rb |
674 | 1 | Jean-Philippe Lang | plugins/PLUGIN/lib/tasks/ |
675 | 94 | @ go2null | plugins/PLUGIN/test/ |
676 | plugins/PLUGIN/test/test_helper.rb |
||
677 | plugins/PLUGIN/test/functional/ |
||
678 | 96 | @ go2null | plugins/PLUGIN/test/functional/CONTROLLER_controller_test.rb |
679 | 94 | @ go2null | plugins/PLUGIN/test/unit/ |
680 | plugins/PLUGIN/test/unit/MODEL_test.rb |
||
681 | </pre> |