プロジェクト

全般

プロフィール

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

Haru Iida, 2011/01/22 16:12

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