Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/redmine/helpers/time_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def run
reorder(nil).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはソートが解除されますが、@scopeにクエリーで指定したのソート順が入っているので、それを適用すればいいのでは?

group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).
joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).
order(@criteria.collect{|criteria| @available_criteria[criteria][:order]}.compact).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もう一度考えてみれば、ここは本当はこういう感じでやりたい

results = @scope.includes(:activity).
                 reorder(nil).
                 group(...).
                 joins(...)

@criteria.collect{|criteria| @available_criteria[criteria][:order]}.compact.each do |order_by_criteria|
  results = order_by_criteria(results)
end

result.sum(:hours).each do |hash, hours|
  ...

orderの項目を変換するブロックにしたらいい

@available_criteria = {
          'project' => {:sql => "#{TimeEntry.table_name}.project_id",
                        ...
                        order: ->(results) { results.order("#{Project.table_name}.name") }

これで、joinsとか必要だった場合に、その「メソッド」の任せたらいい。

sum(:hours).each do |hash, hours|
h = {'hours' => hours}
(@criteria + time_columns).each_with_index do |name, i|
Expand Down Expand Up @@ -106,7 +107,10 @@ def load_available_criteria
@available_criteria = {
'project' => {:sql => "#{TimeEntry.table_name}.project_id",
:klass => Project,
:label => :label_project},
:label => :label_project,
:joins => :project,
:order => "#{Project.table_name}.name"
},
'status' => {:sql => "#{Issue.table_name}.status_id",
:klass => IssueStatus,
:label => :field_status},
Expand Down
67 changes: 67 additions & 0 deletions test/unit/time_report_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2020 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class TimeReportTest < ActiveSupport::TestCase
fixtures :trackers, :users

def setup
User.current = nil
TimeEntry.destroy_all
end

def report_by(criteria)
project = nil
issue = nil # Unused argument. TODO: Remove
query = TimeEntryQuery.new
Redmine::Helpers::TimeReport.new(project, issue, criteria, 'month', query.results_scope)
end

def test_project_sorted_by_name
project_b = Project.generate!(name: 'Project B')
project_a = Project.generate!(name: 'Project A')
TimeEntry.generate!(project: project_b)
TimeEntry.generate!(project: project_a)
report = report_by(['project'])

result_projects = report.hours.map { |row| row['project'] }

assert_equal [project_a.id, project_b.id], result_projects
end

# TODO: This test is passing. Why???
# def test_status_sorted_by_position
# status_b = IssueStatus.generate!(position: 2)
# status_a = IssueStatus.generate!(position: 1)

# issue_b = Issue.generate!
# issue_b.update(status: status_b)
# issue_a = Issue.generate!
# issue_a.update(status: status_a)

# TimeEntry.generate!(issue: issue_a)
# TimeEntry.generate!(issue: issue_b)
# report = report_by(['status'])

# result_statuses = report.hours.map { |row| row['status'] }

# assert_equal [status_a.id, status_b.id], result_statuses
# end
end