Commit a3c89d4f authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

Custom fields (list and boolean) can be used as criteria in time report (#1012).

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1340 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent f025b16b
......@@ -26,6 +26,8 @@ class TimelogController < ApplicationController
include SortHelper
helper :issues
include TimelogHelper
helper :custom_fields
include CustomFieldsHelper
def report
@available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
......@@ -51,6 +53,13 @@ class TimelogController < ApplicationController
:label => :label_issue}
}
# Add list and boolean custom fields as available criterias
@project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
@available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
:format => cf.field_format,
:label => cf.name}
end
@criterias = params[:criterias] || []
@criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
@criterias.uniq!
......
......@@ -77,6 +77,10 @@ module TimelogHelper
export
end
def format_criteria_value(criteria, value)
value.blank? ? l(:label_none) : ((k = @available_criterias[criteria][:klass]) ? k.find_by_id(value.to_i) : format_value(value, @available_criterias[criteria][:format]))
end
def report_to_csv(criterias, periods, hours)
export = StringIO.new
CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
......@@ -103,11 +107,11 @@ module TimelogHelper
end
def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
hours.collect {|h| h[criterias[level]]}.uniq.each do |value|
hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
hours_for_value = select_hours(hours, criterias[level], value)
next if hours_for_value.empty?
row = [''] * level
row << to_utf8(value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value))
row << to_utf8(format_criteria_value(criterias[level], value))
row += [''] * (criterias.length - level - 1)
total = 0
periods.each do |period|
......
<% @hours.collect {|h| h[criterias[level]]}.uniq.each do |value| %>
<% @hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| %>
<% hours_for_value = select_hours(hours, criterias[level], value) -%>
<% next if hours_for_value.empty? -%>
<tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>">
<%= '<td></td>' * level %>
<td><%= value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value) %></td>
<td><%= format_criteria_value(criterias[level], value) %></td>
<%= '<td></td>' * (criterias.length - level - 1) -%>
<% total = 0 -%>
<% @periods.each do |period| -%>
......
......@@ -3,7 +3,7 @@ custom_fields_001:
name: Database
min_length: 0
regexp: ""
is_for_all: false
is_for_all: true
type: IssueCustomField
max_length: 0
possible_values: MySQL|PostgreSQL|Oracle
......
......@@ -36,7 +36,7 @@ time_entries_003:
updated_on: 2007-04-21 12:20:48 +02:00
activity_id: 9
spent_on: 2007-04-21
issue_id: 2
issue_id: 3
id: 3
hours: 1.0
user_id: 1
......
......@@ -450,10 +450,11 @@ class IssuesControllerTest < Test::Unit::TestCase
end
def test_destroy_issue_with_no_time_entries
assert_nil TimeEntry.find_by_issue_id(2)
@request.session[:user_id] = 2
post :destroy, :id => 3
post :destroy, :id => 2
assert_redirected_to 'projects/ecookbook/issues'
assert_nil Issue.find_by_id(3)
assert_nil Issue.find_by_id(2)
end
def test_destroy_issues_with_time_entries
......
......@@ -22,7 +22,7 @@ require 'timelog_controller'
class TimelogController; def rescue_action(e) raise e end; end
class TimelogControllerTest < Test::Unit::TestCase
fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
def setup
@controller = TimelogController.new
......@@ -112,6 +112,23 @@ class TimelogControllerTest < Test::Unit::TestCase
assert_equal "162.90", "%.2f" % assigns(:total_hours)
end
def test_report_custom_field_criteria
get :report, :project_id => 1, :criterias => ['project', 'cf_1']
assert_response :success
assert_template 'report'
assert_not_nil assigns(:total_hours)
assert_not_nil assigns(:criterias)
assert_equal 2, assigns(:criterias).size
assert_equal "162.90", "%.2f" % assigns(:total_hours)
# Custom field column
assert_tag :tag => 'th', :content => 'Database'
# Custom field row
assert_tag :tag => 'td', :content => 'MySQL',
:sibling => { :tag => 'td', :attributes => { :class => 'hours' },
:child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
:content => '1' }}
end
def test_report_one_criteria_no_result
get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
assert_response :success
......@@ -186,6 +203,6 @@ class TimelogControllerTest < Test::Unit::TestCase
assert_response :success
assert_equal 'text/csv', @response.content_type
assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment