プロジェクト

全般

プロフィール

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

Haru Iida, 2011/01/22 16:36

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