プロジェクト

全般

プロフィール

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

Haru Iida, 2011/01/22 16:33

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