From a3c89d4f697e958956e129b6bc5f564c9197a89c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 9 Apr 2008 17:45:39 +0000 Subject: [PATCH] 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 --- app/controllers/timelog_controller.rb | 9 +++++++++ app/helpers/timelog_helper.rb | 8 ++++++-- app/views/timelog/_report_criteria.rhtml | 4 ++-- test/fixtures/custom_fields.yml | 2 +- test/fixtures/time_entries.yml | 2 +- test/functional/issues_controller_test.rb | 5 +++-- test/functional/timelog_controller_test.rb | 21 +++++++++++++++++++-- 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index 3081c50dd..2c90093bd 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -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! diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb index e0459581d..db13556a1 100644 --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -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| diff --git a/app/views/timelog/_report_criteria.rhtml b/app/views/timelog/_report_criteria.rhtml index 661e1fdb8..94f3d20f9 100644 --- a/app/views/timelog/_report_criteria.rhtml +++ b/app/views/timelog/_report_criteria.rhtml @@ -1,9 +1,9 @@ -<% @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? -%> <%= '' * level %> -<%= value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value) %> +<%= format_criteria_value(criterias[level], value) %> <%= '' * (criterias.length - level - 1) -%> <% total = 0 -%> <% @periods.each do |period| -%> diff --git a/test/fixtures/custom_fields.yml b/test/fixtures/custom_fields.yml index e58d8e3dc..6be840fcc 100644 --- a/test/fixtures/custom_fields.yml +++ b/test/fixtures/custom_fields.yml @@ -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 diff --git a/test/fixtures/time_entries.yml b/test/fixtures/time_entries.yml index f6876c7b0..4a8a4a2a4 100644 --- a/test/fixtures/time_entries.yml +++ b/test/fixtures/time_entries.yml @@ -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 diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 89769ffe2..042a8f3f2 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -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 diff --git a/test/functional/timelog_controller_test.rb b/test/functional/timelog_controller_test.rb index 4d6cb0b36..e80a67728 100644 --- a/test/functional/timelog_controller_test.rb +++ b/test/functional/timelog_controller_test.rb @@ -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 -- 2.39.5