プラグインハンズオン サンプル » 履歴 » バージョン 11
Haru Iida, 2011/01/22 16:09
1 | 1 | Haru Iida | h1. プラグインハンズオン サンプル |
---|---|---|---|
2 | |||
3 | 9 | Haru Iida | モデルのサンプル |
4 | <pre> |
||
5 | class IssueTemplate < ActiveRecord::Base |
||
6 | unloadable |
||
7 | belongs_to :project |
||
8 | validates_presence_of :project_id, :title, :description |
||
9 | validates_uniqueness_of :title, :scope => :project_id |
||
10 | end |
||
11 | </pre> |
||
12 | |||
13 | 5 | Haru Iida | create アクションのサンプル |
14 | |||
15 | <pre> |
||
16 | def create |
||
17 | # IssueTemplateのインスタンス作成 |
||
18 | @issue_template = IssueTemplate.new |
||
19 | 7 | Haru Iida | @issue_template.project_id = @project.id |
20 | 5 | Haru Iida | if request.post? |
21 | # POSTメソッドだったら値が入力されたということ。 |
||
22 | # formに入力された内容をIssueTemplateのインスタンスに設定。 |
||
23 | @issue_template.attributes = params[:issue_template] |
||
24 | if @issue_template.save |
||
25 | # 保存に成功した。 |
||
26 | 6 | Haru Iida | # 成功したというメッセージを設定後、詳細画面。 |
27 | 5 | Haru Iida | flash[:notice] = l(:notice_successful_create) |
28 | 11 | Haru Iida | redirect_to :action => "show", :id => @project, :template_id => @issue_template.id |
29 | 5 | Haru Iida | end |
30 | # 保存に失敗した場合には再度入力画面が表示される。 |
||
31 | end |
||
32 | end |
||
33 | </pre> |
||
34 | |||
35 | 4 | Haru Iida | init.rbのサンプル |
36 | <pre> |
||
37 | require 'redmine' |
||
38 | |||
39 | Redmine::Plugin.register :redmine_issue_templates do |
||
40 | name 'Redmine Issue Templates plugin' |
||
41 | author 'Haruyuki Iida' |
||
42 | description 'This is a plugin for Redmine' |
||
43 | version '0.0.1' |
||
44 | url 'http://example.com/path/to/plugin' |
||
45 | author_url 'http://example.com/about' |
||
46 | requires_redmine :version_or_higher => '1.1.0' |
||
47 | |||
48 | # 各アクションの権限定義。ここで定義した権限が管理メニューの「ロールと権限」に表示される。 |
||
49 | project_module :issue_templates do |
||
50 | permission :edit_issue_templates, |
||
51 | {:issue_templates => [:create, :update, :destroy]} |
||
52 | permission :show_issue_templates, |
||
53 | {:issue_templates => [:index, :show, :load]} |
||
54 | end |
||
55 | |||
56 | # プロジェクトメニューに追加するTABの定義 |
||
57 | # TABのキャプション、表示位置、TABをクリックした時に呼ばれるアクションなどを定義 |
||
58 | # ここではissue_templates_controllerのindexを呼ぶ設定。 |
||
59 | menu :project_menu, :issue_templates, |
||
60 | { :controller => 'issue_templates', :action => 'index' }, |
||
61 | :caption => "チケットテンプレート", :after => :issues |
||
62 | |||
63 | end |
||
64 | |||
65 | </pre> |
||
66 | |||
67 | 3 | Haru Iida | index.html.erb のサンプル |
68 | |||
69 | 1 | Haru Iida | <pre> |
70 | 3 | Haru Iida | <h2>IssueTemplates#index</h2> |
71 | 1 | Haru Iida | |
72 | 3 | Haru Iida | <%= link_to '新規作成', :controller => 'issue_templates', |
73 | :action => 'create', :id=>@project %> |
||
74 | </pre> |
||
75 | 1 | Haru Iida | |
76 | create.html.erb のサンプル |
||
77 | 3 | Haru Iida | |
78 | <pre> |
||
79 | |||
80 | <h2>テンプレートの作成</h2> |
||
81 | |||
82 | 10 | Haru Iida | <%= error_messages_for 'issue_template' %> |
83 | 1 | Haru Iida | <% labelled_tabular_form_for :issue_template, @issue_template, :action => 'update' do |f|%> |
84 | <p> |
||
85 | <%= f.text_field :title, :size => 50, :required => true %> |
||
86 | </p> |
||
87 | <p> |
||
88 | <%= f.text_field :note, :size => 80, :label => l(:issue_template_note) %> |
||
89 | </p> |
||
90 | <p><%= f.text_area :description, :rows => 8, :cols=>60, :accesskey => accesskey(:edit), :class => 'wiki-edit' ,:required => true %></p> |
||
91 | <%= submit_tag l(:button_apply) %> |
||
92 | <% end %> |
||
93 | </pre> |