プラグインハンズオン サンプル » 履歴 » リビジョン 15
リビジョン 14 (Haru Iida, 2011/01/22 16:33) → リビジョン 15/20 (Haru Iida, 2011/01/22 16:34)
h1. プラグインハンズオン サンプル
index アクションのサンプル
<pre>
def index
@issue_templates =
IssueTemplate.find(:all,
:conditions => ['project_id = ?', @project.id])
end
</pre>
index.html.erbのサンプル
<pre>
<h2>IssueTemplates#index</h2>
<%= link_to '新規作成', :controller => 'issue_templates',
:action => 'create', :id=>@project %>
<table class="list">
<thead>
<tr>
<th>#</th>
<th><%=h l(:field_title)%></th>
<th><%=h l(:field_updated_on)%></th>
<th>メモ</th>
</tr>
</thead>
<tbody>
<% @issue_templates.each do |template| %>
<tr class="<%= cycle('odd', 'even')%>">
<td><%= template.id %></td>
<td><%= link_to h(template.title), :action => 'show', :id => template.id, :template_id => @template %></td>
<td><%= format_time(template.updated_at)%> </td>
<td><%=h template.note%></td>
</tr>
<% end %>
</tbody>
</table>
</pre>
show アクションのサンプル
<pre>
def show
@issue_template = IssueTemplate.find(params[:template_id])
end
</pre>
モデルのサンプル
<pre>
class IssueTemplate < ActiveRecord::Base
unloadable
belongs_to :project
validates_presence_of :project_id, :title, :description
validates_uniqueness_of :title, :scope => :project_id
end
</pre>
create アクションのサンプル
<pre>
def create
# IssueTemplateのインスタンス作成
@issue_template = IssueTemplate.new
@issue_template.project_id = @project.id
if request.post?
# POSTメソッドだったら値が入力されたということ。
# formに入力された内容をIssueTemplateのインスタンスに設定。
@issue_template.attributes = params[:issue_template]
if @issue_template.save
# 保存に成功した。
# 成功したというメッセージを設定後、詳細画面。
flash[:notice] = l(:notice_successful_create)
redirect_to :action => "show", :id => @project, :template_id => @issue_template.id
end
# 保存に失敗した場合には再度入力画面が表示される。
end
end
</pre>
init.rbのサンプル
<pre>
require 'redmine'
Redmine::Plugin.register :redmine_issue_templates do
name 'Redmine Issue Templates plugin'
author 'Haruyuki Iida'
description 'This is a plugin for Redmine'
version '0.0.1'
url 'http://example.com/path/to/plugin'
author_url 'http://example.com/about'
requires_redmine :version_or_higher => '1.1.0'
# 各アクションの権限定義。ここで定義した権限が管理メニューの「ロールと権限」に表示される。
project_module :issue_templates do
permission :edit_issue_templates,
{:issue_templates => [:create, :update, :destroy]}
permission :show_issue_templates,
{:issue_templates => [:index, :show, :load]}
end
# プロジェクトメニューに追加するTABの定義
# TABのキャプション、表示位置、TABをクリックした時に呼ばれるアクションなどを定義
# ここではissue_templates_controllerのindexを呼ぶ設定。
menu :project_menu, :issue_templates,
{ :controller => 'issue_templates', :action => 'index' },
:caption => "チケットテンプレート", :after => :issues
end
</pre>
index.html.erb のサンプル
<pre>
<h2>IssueTemplates#index</h2>
<%= link_to '新規作成', :controller => 'issue_templates',
:action => 'create', :id=>@project %>
</pre>
create.html.erb のサンプル
<pre>
<h2>テンプレートの作成</h2>
<%= error_messages_for 'issue_template' %>
<% labelled_tabular_form_for :issue_template, @issue_template, :action => 'update' do |f|%>
<p>
<%= f.text_field :title, :size => 50, :required => true %>
</p>
<p>
<%= f.text_field :note, :size => 80, :label => l(:issue_template_note) %>
</p>
<p><%= f.text_area :description, :rows => 8, :cols=>60, :accesskey => accesskey(:edit), :class => 'wiki-edit' ,:required => true %></p>
<%= submit_tag l(:button_apply) %>
<% end %>
</pre>