プロジェクト

全般

プロフィール

プラグインハンズオン サンプル » 履歴 » バージョン 18

Haru Iida, 2011/01/22 16:46

1 1 Haru Iida
h1. プラグインハンズオン サンプル
2
3 18 Haru Iida
destroy アクションのサンプル
4
<pre>
5
def destroy
6
    @issue_template = IssueTemplate.find(params[:template_id])
7
    @issue_template.destroy
8
    redirect_to :action => "index", :id => @project
9
end
10
</pre>
11
12 14 Haru Iida
index アクションのサンプル
13
<pre>
14
def index
15
    @issue_templates =
16
      IssueTemplate.find(:all, 
17
      :conditions => ['project_id = ?', @project.id])
18
end
19
</pre>
20
21 13 Haru Iida
index.html.erbのサンプル
22
23
<pre>
24 15 Haru Iida
<h2>IssueTemplates#index</h2>
25
26
<%= link_to '新規作成', :controller => 'issue_templates',
27
  :action => 'create', :id=>@project %>
28
29 13 Haru Iida
<table class="list">
30
  <thead>
31
    <tr>
32
      <th>#</th>
33
      <th><%=h l(:field_title)%></th>
34 1 Haru Iida
      <th><%=h l(:field_updated_on)%></th>
35 14 Haru Iida
      <th>メモ</th>
36 13 Haru Iida
    </tr>
37
  </thead>
38
  <tbody>
39
    <% @issue_templates.each do |template| %>
40
      <tr class="<%= cycle('odd', 'even')%>">
41
        <td><%= template.id %></td>
42 17 Haru Iida
        <td><%= link_to h(template.title), :action => 'show', :id => @project, :template_id => template.id %></td>
43 13 Haru Iida
        <td><%= format_time(template.updated_at)%> </td>
44
        <td><%=h template.note%></td>
45
      </tr>
46
    <% end %>
47
  </tbody>
48
</table>
49
50
</pre>
51
52 12 Haru Iida
show アクションのサンプル
53
<pre>
54
def show
55
    @issue_template = IssueTemplate.find(params[:template_id])
56
end
57
</pre>
58
59 9 Haru Iida
モデルのサンプル
60
<pre>
61
class IssueTemplate < ActiveRecord::Base
62
  unloadable
63
  belongs_to :project
64
  validates_presence_of :project_id, :title, :description
65
  validates_uniqueness_of :title, :scope => :project_id
66
end
67
</pre>
68
69 5 Haru Iida
create アクションのサンプル
70
71
<pre>
72
def create
73
    # IssueTemplateのインスタンス作成
74
    @issue_template = IssueTemplate.new    
75 7 Haru Iida
    @issue_template.project_id = @project.id
76 5 Haru Iida
    if request.post?
77
      # POSTメソッドだったら値が入力されたということ。
78
      # formに入力された内容をIssueTemplateのインスタンスに設定。
79
      @issue_template.attributes = params[:issue_template]
80
      if @issue_template.save
81
        # 保存に成功した。
82 6 Haru Iida
        # 成功したというメッセージを設定後、詳細画面。
83 5 Haru Iida
        flash[:notice] = l(:notice_successful_create)
84 11 Haru Iida
        redirect_to :action => "show", :id => @project,  :template_id => @issue_template.id
85 5 Haru Iida
      end
86
      # 保存に失敗した場合には再度入力画面が表示される。
87
    end
88
  end
89
</pre>
90
91 4 Haru Iida
init.rbのサンプル
92
<pre>
93
require 'redmine'
94
95
Redmine::Plugin.register :redmine_issue_templates do
96
  name 'Redmine Issue Templates plugin'
97
  author 'Haruyuki Iida'
98
  description 'This is a plugin for Redmine'
99
  version '0.0.1'
100
  url 'http://example.com/path/to/plugin'
101
  author_url 'http://example.com/about'
102
  requires_redmine :version_or_higher => '1.1.0'
103
104
  # 各アクションの権限定義。ここで定義した権限が管理メニューの「ロールと権限」に表示される。
105
  project_module :issue_templates do
106
    permission :edit_issue_templates,
107
      {:issue_templates => [:create, :update, :destroy]}
108
    permission :show_issue_templates,
109
      {:issue_templates => [:index, :show, :load]}
110
  end
111
112
  # プロジェクトメニューに追加するTABの定義
113
  # TABのキャプション、表示位置、TABをクリックした時に呼ばれるアクションなどを定義
114
  # ここではissue_templates_controllerのindexを呼ぶ設定。
115
  menu :project_menu, :issue_templates,
116
    { :controller => 'issue_templates', :action => 'index' },
117
    :caption => "チケットテンプレート", :after => :issues
118
119
end
120
121
</pre>
122
123 3 Haru Iida
index.html.erb のサンプル
124
125 1 Haru Iida
<pre>
126 3 Haru Iida
<h2>IssueTemplates#index</h2>
127 1 Haru Iida
128 3 Haru Iida
<%= link_to '新規作成', :controller => 'issue_templates',
129
  :action => 'create', :id=>@project %>
130
</pre>
131 1 Haru Iida
132
create.html.erb のサンプル
133 3 Haru Iida
134
<pre>
135
136
<h2>テンプレートの作成</h2>
137
138 10 Haru Iida
<%= error_messages_for 'issue_template' %>
139 1 Haru Iida
<% labelled_tabular_form_for :issue_template, @issue_template, :action => 'update' do |f|%>
140
    <p>
141
      <%= f.text_field :title, :size => 50, :required => true %>
142
    </p>
143
    <p>
144
      <%= f.text_field :note, :size => 80, :label => l(:issue_template_note) %>
145
    </p>
146
    <p><%= f.text_area :description, :rows => 8, :cols=>60, :accesskey => accesskey(:edit), :class => 'wiki-edit' ,:required => true %></p>
147
    <%= submit_tag l(:button_apply) %>
148
  <% end %>
149
</pre>