Redmineプラグイン開発 – 日時指定でチケットを取得してみる

感想おまちしてます!

  • NetBeans6.5で作ります。
  • ソースはGoogle Codeを参照のこと。
  • redmineは「c:\fujihara\Ruby\redmine-0.8.0」にあって、WEBrickを使って動かす。

モデルがあるのでControllerとViewを書くだけ。
DaizuTicketReportController

 class DaizuTicketReportController < ApplicationController
before_filter :init
def index
if @assigned_to_id && @start_date && @due_date
# get issues
# get started issue.
@all_issues =
Issue.find(:all,
:conditions => ["start_date >= ? and due_date <= ? and assigned_to_id = ?",
@start_date, @due_date, @assigned_to_id],
:order => "start_date")
end
end
def init
@assigned_to_id = params[:assigned_to_id]
@start_date = params[:start_date]
@due_date = params[:due_date]
@all_users =  User.find(:all, :conditions => ["status = 1"])
if @assigned_to_id
@target_user = User.find(:first, :conditions => ["id = ?", @assigned_to_id])
end
end
end

index.html.erb

 <script src="/javascripts/calendar/calendar.js" type="text/javascript"></script>
<script src="/javascripts/calendar/lang/calendar-ja.js" type="text/javascript"></script>
<script src="/javascripts/calendar/calendar-setup.js" type="text/javascript"></script>
<link href="/stylesheets/calendar.css" media="screen" rel="stylesheet" type="text/css" />
<h2><%= link_to h('大豆トップページ'), :controller => 'daizu_main', :action => 'index' %> > チケットレポート</h2>
<% form_tag({ :controller => 'daizu_ticket_report', :action => 'index' }, :id => 'query_form') do %>
<p><label for="issue_assigned_to_id">担当者</label>
<select name="assigned_to_id">
<% @all_users.each do |user| %>
<option value="<%= user.id %>"><%= user.name %></option>
<% end %>
</select>
</p>
<p><label for="issue_start_date">開始日</label>
<input id="issue_start_date" name="start_date" size="15" type="text" value="<%= params[:start_date] %>" />
<img alt="Calendar" class="calendar-trigger" id="issue_start_date_trigger" src="/images/calendar.png" />
<script type="text/javascript">
//<![CDATA[
Calendar.setup({inputField : 'issue_start_date', ifFormat : '%Y-%m-%d', button : 'issue_start_date_trigger' });
//]]>
</script></p>
<p><label for="issue_due_date">期限日</label>
<input id="issue_due_date" name="due_date" size="15" type="text" value="<%= params[:due_date] %>" />
<img alt="Calendar" class="calendar-trigger" id="issue_due_date_trigger" src="/images/calendar.png" />
<script type="text/javascript">
//<![CDATA[
Calendar.setup({inputField : 'issue_due_date', ifFormat : '%Y-%m-%d', button : 'issue_due_date_trigger' });
//]]>
</script></p>
<button type="submit">GO</button>
<% end %>
<% if @all_issues %>
<p>[from:<%= params[:start_date] %> to:<%= params[:due_date] %>] <%= @target_user %>のチケットレポート。</p>
<table class="list issues">
<tr>
<th>ID</th>
<th>プロジェクト</th>
<th>トラッカー</th>
<th>題名</th>
<th>担当者</th>
<th>ステータス</th>
<th>開始日</th>
<th>期限日</th>
<th>進捗%</th>
</tr>
<% @all_issues.each do |issue| %>
<tr>
<td><%= issue.id %></td>
<td><%= Project.find(:first, :conditions => ["id = ?", issue.project_id]) %></td>
<td><%= Tracker.find(:first, :conditions => ["id = ?", issue.tracker_id]) %></td>
<td><%= issue.subject %></td>
<td><%= User.find(:first, :conditions => ["id = ?", issue.assigned_to_id]) %></td>
<td><%= IssueStatus.find(:first, :conditions => ["id = ?", issue.status_id])  %></td>
<td><%= issue.start_date %></td>
<td><%= issue.due_date %></td>
<td>
<table class="progress" style="width: 80px;">
<tr>
<% if issue.done_ratio <= 0  %>
<td class="todo" style="width:100%;"></td>
<% elsif issue.done_ratio >= 100 %>
<td class="closed" style="width:100%;"></td>
<% else %>
<td class="closed" style="<%= "width:" + issue.done_ratio.to_s + "%;" %>"></td>
<td class="todo" style="<%= "width:" + (100 - issue.done_ratio.to_i).to_s + "%;" %>"></td>
<% end %>
</tr>
</table>
<p class="pourcent"><%= issue.done_ratio.to_s + "%" %></p>
</td>
</tr>
<% end %>
</table>
<% end %>

チケット登録画面のJavascriptカレンダーを使う場合は、(もっといい方法があるとおもうけど)以下をまず記述。

 <script src="/javascripts/calendar/calendar.js" type="text/javascript"></script>
<script src="/javascripts/calendar/lang/calendar-ja.js" type="text/javascript"></script>
<script src="/javascripts/calendar/calendar-setup.js" type="text/javascript"></script>
<link href="/stylesheets/calendar.css" media="screen" rel="stylesheet" type="text/css" />

でもって下みたいな感じでかけば簡単にできた。たぶん、そういうメソッドがあるんだろうけどなー。

   <p><label for="issue_start_date">開始日</label>
<input id="issue_start_date" name="start_date" size="15" type="text" value="<%= params[:start_date] %>" />
<img alt="Calendar" class="calendar-trigger" id="issue_start_date_trigger" src="/images/calendar.png" />
<script type="text/javascript">
//<![CDATA[
Calendar.setup({inputField : 'issue_start_date', ifFormat : '%Y-%m-%d', button : 'issue_start_date_trigger' });
//]]>
</script></p>

http://localhost:3000/daizu_ticket_reportにアクセスして、時間を入力してSubmitすれば・・・

pic20090212_233202

おおお!できましたね!