Redmine Vote Plugin
Révision | 490139b9ffa85c0e7e262b695ee57f8f211f2cc6 (tree) |
---|---|
l'heure | 2012-07-15 19:59:52 |
Auteur | root <root@ip-1...> |
Commiter | root |
PluginTutorial
@@ -0,0 +1,295 @@ | ||
1 | +プラグインチュートリアル | |
2 | +日本語部分翻訳 | |
3 | + | |
4 | +2012年7月15日 福島英俊 | |
5 | + | |
6 | +http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial | |
7 | + | |
8 | +Redmine 2.x用のチュートリアルです。 | |
9 | + | |
10 | +新しいプラグインの作成 | |
11 | +======================== | |
12 | + | |
13 | +RAILS_ENV環境変数を設定します。 | |
14 | + | |
15 | +$ export RAILS_ENV="production" | |
16 | + | |
17 | +Redmine plugin generatorを使って新しいプラグインを作成します。 | |
18 | + | |
19 | +ruby script/rails generate redmine_plugin <plugin_name> | |
20 | + | |
21 | +Redmineのディレクトリに移動し、下記のコマンドを実行します。 | |
22 | + | |
23 | +$ ruby script/rails generate redmine_plugin Polls | |
24 | + create plugins/polls/app | |
25 | + create plugins/polls/app/controllers | |
26 | + create plugins/polls/app/helpers | |
27 | + create plugins/polls/app/models | |
28 | + create plugins/polls/app/views | |
29 | + create plugins/polls/db/migrate | |
30 | + create plugins/polls/lib/tasks | |
31 | + create plugins/polls/assets/images | |
32 | + create plugins/polls/assets/javascripts | |
33 | + create plugins/polls/assets/stylesheets | |
34 | + create plugins/polls/config/locales | |
35 | + create plugins/polls/test | |
36 | + create plugins/polls/README.rdoc | |
37 | + create plugins/polls/init.rb | |
38 | + create plugins/polls/config/routes.rb | |
39 | + create plugins/polls/config/locales/en.yml | |
40 | + create plugins/polls/test/test_helper.rb | |
41 | + | |
42 | +plugins/pollsディレクトリに、プラグインの構造がつくられます。plugins/polls/init.rbを編集します。 | |
43 | + | |
44 | +Redmine::Plugin.register :polls do | |
45 | + name 'Polls plugin' | |
46 | + author 'John Smith' | |
47 | + description 'A plugin for managing polls' | |
48 | + version '0.0.1' | |
49 | +end | |
50 | + | |
51 | +アプリケーションを再起動します。ブラウザで、http://localhost:3000/admin/pluginsを開きます。 | |
52 | + | |
53 | +モデルの作成 | |
54 | +============== | |
55 | + | |
56 | +モデルを作成します。 | |
57 | + | |
58 | +ruby script/rails generate redmine_plugin_model <plugin_name> <model_name> [field[:type][:index] field[:type][:index] ...] | |
59 | + | |
60 | +これは、Pollモデルと、マイグレーションファイル、001_create_polls.rbを生成します。 | |
61 | + | |
62 | +class CreatePolls < ActiveRecord::Migration | |
63 | + def change | |
64 | + create_table :polls do |t| | |
65 | + t.string :question | |
66 | + t.integer :yes, :default => 0 | |
67 | + t.integer :no, :default => 0 | |
68 | + end | |
69 | + end | |
70 | +end | |
71 | + | |
72 | +あなたはマイグレーションファイルを修正できます。それから、下記のコマンドでデータベスを変更します。 | |
73 | + | |
74 | +$ rake redmine:plugins:migrate | |
75 | + | |
76 | +Migrating polls (Polls plugin)... | |
77 | +== CreatePolls: migrating ==================================================== | |
78 | +-- create_table(:polls) | |
79 | + -> 0.0410s | |
80 | +== CreatePolls: migrated (0.0420s) =========================================== | |
81 | + | |
82 | +plugins/polls/app/models/poll.rbファイルを編集します。コントローラーから呼び出される#voteメソッドを追加します。 | |
83 | + | |
84 | +class Poll < ActiveRecord::Base | |
85 | + def vote(answer) | |
86 | + increment(answer == 'yes' ? :yes : :no) | |
87 | + end | |
88 | +end | |
89 | + | |
90 | +コントローラーの作成 | |
91 | +====================== | |
92 | + | |
93 | +プラグインのコントローラーを作成します。 | |
94 | + | |
95 | +ruby script/rails generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>] | |
96 | + | |
97 | +$ ruby script/rails generate redmine_plugin_controller Polls polls index vote | |
98 | + create plugins/polls/app/controllers/polls_controller.rb | |
99 | + create plugins/polls/app/helpers/polls_helper.rb | |
100 | + create plugins/polls/test/functional/polls_controller_test.rb | |
101 | + create plugins/polls/app/views/polls/index.html.erb | |
102 | + create plugins/polls/app/views/polls/vote.html.erb | |
103 | + | |
104 | +2つのアクシション(#index と #vote)を持つPollsControllerがつくられます。 | |
105 | +2つのアクションを実装するためにplugins/polls/app/controllers/polls_controller.rbを編集します。 | |
106 | + | |
107 | +class PollsController < ApplicationController | |
108 | + unloadable | |
109 | + | |
110 | + def index | |
111 | + @polls = Poll.all | |
112 | + end | |
113 | + | |
114 | + def vote | |
115 | + poll = Poll.find(params[:id]) | |
116 | + poll.vote(params[:answer]) | |
117 | + if poll.save | |
118 | + flash[:notice] = 'Vote saved.' | |
119 | + end | |
120 | + redirect_to :action => 'index' | |
121 | + end | |
122 | +end | |
123 | + | |
124 | +ポールを表示するplugins/polls/app/views/polls/index.html.erbを編集します。 | |
125 | + | |
126 | +<h2>Polls</h2> | |
127 | + | |
128 | +<% @polls.each do |poll| %> | |
129 | + <p> | |
130 | + <%= poll.question %>? | |
131 | + <%= link_to 'Yes', { :action => 'vote', :id => poll[:id], :answer => 'yes' }, :method => :post %> (<%= poll.yes %>) / | |
132 | + <%= link_to 'No', { :action => 'vote', :id => poll[:id], :answer => 'no' }, :method => :post %> (<%= poll.no %>) | |
133 | + </p> | |
134 | +<% end %> | |
135 | + | |
136 | +ルーティングの追加 | |
137 | +==================== | |
138 | + | |
139 | +Redmineはデフォルトのワイルドカードルート(':controller/:action/:id')を提供しません。プラグインは、固有のconfig/routes.rbファイルで、ルーティングを明示しなけければなりません。そこで、plugins/polls/config/routes.rbを編集し、2つのアクションのための2つのルートを追加します。 | |
140 | + | |
141 | +get 'polls', :to => 'polls#index' | |
142 | +post 'post/:id/vote', :to => 'polls#vote' | |
143 | + | |
144 | +Railsのルーティングについてのより詳しい情報は、下記ページを見てください。 | |
145 | + | |
146 | +http://guides.rubyonrails.org/routing.html | |
147 | + | |
148 | +アプリケーションを再起動し、ブラウザーでhttp://localhost:3000/pollsを開きます。2つのポールが表示され、投票することが可能です。 | |
149 | + | |
150 | +国際化 | |
151 | +======== | |
152 | + | |
153 | +翻訳ファイルは、config/localesに保存します。例:plugins/polls/config/locales/ | |
154 | + | |
155 | +メニューの拡張 | |
156 | +================ | |
157 | + | |
158 | +これで、コントローラーは動作しますが、ユーザーはURLを知らなければなりません。Redmine plugin APIを使って、標準メニューを拡張することができます。それでは、アプリケーションメニューに、新しい項目を追加しましょう。 | |
159 | + | |
160 | +アプリケーションメニューの拡張 | |
161 | +-------------------------------- | |
162 | + | |
163 | +plugin registration blockの最後に下記の行を追加するためにプラグインディレクトリのrootにあるplugins/polls/init.rbを編集します。 | |
164 | + | |
165 | +Redmine::Plugin.register :redmine_polls do | |
166 | + [...] | |
167 | + | |
168 | + menu :application_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls' | |
169 | +end | |
170 | + | |
171 | +文法: | |
172 | + | |
173 | +menu(menu_name, item_name, url, options={}) | |
174 | + | |
175 | +下記の5つのメニューを拡張可能です。 | |
176 | + | |
177 | + :top_menu - the top left menu | |
178 | + :account_menu - the top right menu with sign in/sign out links | |
179 | + :application_menu - the main menu displayed when the user is not inside a project | |
180 | + :project_menu - the main menu displayed when the user is inside a project | |
181 | + :admin_menu - the menu displayed on the Administration page (can only insert after Settings, before Plugins) | |
182 | + | |
183 | +オプション: | |
184 | + | |
185 | + :param - the parameter key that is used for the project id (default is :id) | |
186 | + :if - a Proc that is called before rendering the item, the item is displayed only if it returns true | |
187 | + :caption - the menu caption that can be: | |
188 | + a localized string Symbol | |
189 | + a String | |
190 | + a Proc that can take the project as argument | |
191 | + | |
192 | + :before, :after - specify where the menu item should be inserted (eg. :after => :activity) | |
193 | + :first, :last - if set to true, the item will stay at the beginning/end of the menu (eg. :last => true) | |
194 | + :html - a hash of html options that are passed to link_to when rendering the menu item | |
195 | + | |
196 | +プロジェクトメニューの拡張 | |
197 | +---------------------------- | |
198 | + | |
199 | +(飛ばし) | |
200 | + | |
201 | +新しい権限の追加 | |
202 | +================== | |
203 | + | |
204 | +今のところ、だれでも調査に投票可能です。権限の定義を変更することによって、設定可能にしましょう。私たちは、2つのプロジェクトベースの権限を定義するつもりです。1つは、投票結果を見ること、もう1つは投票することです。 | |
205 | + | |
206 | +権限定義を変更するためにplugins/polls/init.rbを編集します。 | |
207 | + | |
208 | +permission :view_polls, :polls => :index | |
209 | +permission :vote_polls, :polls => :vote | |
210 | + | |
211 | +アプリケーションを再起動し、http://localhost:3000/roles/reportを開きます。 | |
212 | + | |
213 | +あなたは今、これらの権限を既にある役割に追加することができます。 | |
214 | + | |
215 | +もちろん、カレントユーザーの権限によって、アクションが実際に保護されるようにPollsController にいくつかのコードを追加する必要があります。 | |
216 | + | |
217 | +#indexアクションがどのようになるか示します。 | |
218 | + | |
219 | +class PollsController < ApplicationController | |
220 | + unloadable | |
221 | + | |
222 | + before_filter :find_project, :authorize, :only => :index | |
223 | + | |
224 | + [...] | |
225 | + | |
226 | + def index | |
227 | + @polls = Poll.find(:all) # @project.polls | |
228 | + end | |
229 | + | |
230 | + [...] | |
231 | + | |
232 | + private | |
233 | + | |
234 | + def find_project | |
235 | + # @project variable must be set before calling the authorize filter | |
236 | + @project = Project.find(params[:project_id]) | |
237 | + end | |
238 | +end | |
239 | + | |
240 | +Retrieving the current project before the #vote action could be done using a similar way. | |
241 | +After this, viewing and voting polls will be only available to admin users or users that have the appropriate role on the project. | |
242 | + | |
243 | +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. | |
244 | +Simply create an *.yml (eg. en.yml) file in plugins/polls/config/locales and fill it with labels like this: | |
245 | + | |
246 | +en: | |
247 | + permission_view_polls: View Polls | |
248 | + permission_vote_polls: Vote Polls | |
249 | + | |
250 | +In this example the created file is known as en.yml, but all other supported language files are also possible too. | |
251 | +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. | |
252 | + | |
253 | +Restart your application and point the permission section. | |
254 | + | |
255 | +プロジェクトモジュールの作成 | |
256 | +============================== | |
257 | + | |
258 | +プラグイン表示の改善 | |
259 | +====================== | |
260 | + | |
261 | +CSSの追加 | |
262 | +----------- | |
263 | + | |
264 | +ページタイトルの設定 | |
265 | +---------------------- | |
266 | + | |
267 | +フックの使用 | |
268 | +============== | |
269 | + | |
270 | +ビューでのフック | |
271 | +----------------- | |
272 | + | |
273 | +コントローラーでのフック | |
274 | +-------------------------- | |
275 | + | |
276 | +プラグインの設定の作成 | |
277 | +======================== | |
278 | + | |
279 | +TODO | |
280 | + | |
281 | +プラグインのテスト | |
282 | +==================== | |
283 | + | |
284 | +以上 | |
285 | + | |
286 | + | |
287 | + | |
288 | + | |
289 | + | |
290 | + | |
291 | + | |
292 | + | |
293 | + | |
294 | + | |
295 | + |