From f085508121efdcda7f59fb9408dd998ea825d881 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Tue, 21 May 2013 13:42:34 +0000 Subject: [PATCH] gantt: sort versions by start date (#7335) git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11898 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redmine/helpers/gantt.rb | 9 ++++++++ test/unit/lib/redmine/helpers/gantt_test.rb | 25 +++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/redmine/helpers/gantt.rb b/lib/redmine/helpers/gantt.rb index e8e7ac458..1625801f9 100644 --- a/lib/redmine/helpers/gantt.rb +++ b/lib/redmine/helpers/gantt.rb @@ -220,6 +220,7 @@ module Redmine return if abort? end versions = project_versions(project) + self.class.sort_versions!(versions) versions.each do |version| render_version(project, version, options) end @@ -690,6 +691,14 @@ module Redmine ancesters_start_date end + def self.sort_versions!(versions) + versions.sort! {|a, b| sort_version_logic(a) <=> sort_version_logic(b)} + end + + def self.sort_version_logic(version) + [(version.start_date || Date.new()), version.id] + end + def current_limit if @max_rows @max_rows - @number_of_rows diff --git a/test/unit/lib/redmine/helpers/gantt_test.rb b/test/unit/lib/redmine/helpers/gantt_test.rb index d427ce707..1c46c6098 100644 --- a/test/unit/lib/redmine/helpers/gantt_test.rb +++ b/test/unit/lib/redmine/helpers/gantt_test.rb @@ -828,4 +828,29 @@ class Redmine::Helpers::GanttHelperTest < ActionView::TestCase issue1_child1_child2.id, issue1_child1_child1.id, issue2.id], issues.map{|v| v.id} end + + def test_sort_versions + project = Project.generate! + version1 = Version.create!(:project => project, :name => 'test1') + version2 = Version.create!(:project => project, :name => 'test2') + version3 = Version.create!(:project => project, :name => 'test3') + version4 = Version.create!(:project => project, :name => 'test4') + issue1 = Issue.generate!(:subject => "test", :project => project, + :fixed_version => version1) + issue2 = Issue.generate!(:subject => "test", :project => project, + :fixed_version => version2) + issue3 = Issue.generate!(:subject => "test", :project => project, + :fixed_version => version3, :start_date => (today - 1)) + issue4 = Issue.generate!(:subject => "test", :project => project, + :fixed_version => version4, :start_date => (today - 2)) + assert_nil version1.start_date + assert_nil version2.start_date + assert version1.id < version2.id + assert_equal today - 1, version3.start_date + assert_equal today - 2, version4.start_date + versions = [version4, version3, version2, version1] + Redmine::Helpers::Gantt.sort_versions!(versions) + assert_equal [version1.id, version2.id, version4.id, version3.id], + versions.map{|v| v.id} + end end