##// END OF EJS Templates
Merged r3881 from trunk....
Jean-Philippe Lang -
r3768:52a12aaca884
parent child
Show More
@@ -1,92 +1,99
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssueStatus < ActiveRecord::Base
19 19 before_destroy :check_integrity
20 has_many :workflows, :foreign_key => "old_status_id", :dependent => :delete_all
20 has_many :workflows, :foreign_key => "old_status_id"
21 21 acts_as_list
22
23 before_destroy :delete_workflows
22 24
23 25 validates_presence_of :name
24 26 validates_uniqueness_of :name
25 27 validates_length_of :name, :maximum => 30
26 28 validates_format_of :name, :with => /^[\w\s\'\-]*$/i
27 29 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
28 30
29 31 def after_save
30 32 IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
31 33 end
32 34
33 35 # Returns the default status for new issues
34 36 def self.default
35 37 find(:first, :conditions =>["is_default=?", true])
36 38 end
37 39
38 40 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
39 41 def self.update_issue_done_ratios
40 42 if Issue.use_status_for_done_ratio?
41 43 IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status|
42 44 Issue.update_all(["done_ratio = ?", status.default_done_ratio],
43 45 ["status_id = ?", status.id])
44 46 end
45 47 end
46 48
47 49 return Issue.use_status_for_done_ratio?
48 50 end
49 51
50 52 # Returns an array of all statuses the given role can switch to
51 53 # Uses association cache when called more than one time
52 54 def new_statuses_allowed_to(roles, tracker)
53 55 if roles && tracker
54 56 role_ids = roles.collect(&:id)
55 57 new_statuses = workflows.select {|w| role_ids.include?(w.role_id) && w.tracker_id == tracker.id}.collect{|w| w.new_status}.compact.sort
56 58 else
57 59 []
58 60 end
59 61 end
60 62
61 63 # Same thing as above but uses a database query
62 64 # More efficient than the previous method if called just once
63 65 def find_new_statuses_allowed_to(roles, tracker)
64 66 if roles && tracker
65 67 workflows.find(:all,
66 68 :include => :new_status,
67 69 :conditions => { :role_id => roles.collect(&:id),
68 70 :tracker_id => tracker.id}).collect{ |w| w.new_status }.compact.sort
69 71 else
70 72 []
71 73 end
72 74 end
73 75
74 76 def new_status_allowed_to?(status, roles, tracker)
75 77 if status && roles && tracker
76 78 !workflows.find(:first, :conditions => {:new_status_id => status.id, :role_id => roles.collect(&:id), :tracker_id => tracker.id}).nil?
77 79 else
78 80 false
79 81 end
80 82 end
81 83
82 84 def <=>(status)
83 85 position <=> status.position
84 86 end
85 87
86 88 def to_s; name end
87 89
88 90 private
89 91 def check_integrity
90 92 raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id])
91 93 end
94
95 # Deletes associated workflows
96 def delete_workflows
97 Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
98 end
92 99 end
@@ -1,105 +1,107
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class IssueStatusTest < ActiveSupport::TestCase
21 21 fixtures :issue_statuses, :issues
22 22
23 23 def test_create
24 24 status = IssueStatus.new :name => "Assigned"
25 25 assert !status.save
26 26 # status name uniqueness
27 27 assert_equal 1, status.errors.count
28 28
29 29 status.name = "Test Status"
30 30 assert status.save
31 31 assert !status.is_default
32 32 end
33 33
34 34 def test_destroy
35 count_before = IssueStatus.count
36 35 status = IssueStatus.find(3)
37 assert status.destroy
38 assert_equal count_before - 1, IssueStatus.count
36 assert_difference 'IssueStatus.count', -1 do
37 assert status.destroy
38 end
39 assert_nil Workflow.first(:conditions => {:old_status_id => status.id})
40 assert_nil Workflow.first(:conditions => {:new_status_id => status.id})
39 41 end
40 42
41 43 def test_destroy_status_in_use
42 44 # Status assigned to an Issue
43 45 status = Issue.find(1).status
44 46 assert_raise(RuntimeError, "Can't delete status") { status.destroy }
45 47 end
46 48
47 49 def test_default
48 50 status = IssueStatus.default
49 51 assert_kind_of IssueStatus, status
50 52 end
51 53
52 54 def test_change_default
53 55 status = IssueStatus.find(2)
54 56 assert !status.is_default
55 57 status.is_default = true
56 58 assert status.save
57 59 status.reload
58 60
59 61 assert_equal status, IssueStatus.default
60 62 assert !IssueStatus.find(1).is_default
61 63 end
62 64
63 65 def test_reorder_should_not_clear_default_status
64 66 status = IssueStatus.default
65 67 status.move_to_bottom
66 68 status.reload
67 69 assert status.is_default?
68 70 end
69 71
70 72 context "#update_done_ratios" do
71 73 setup do
72 74 @issue = Issue.find(1)
73 75 @issue_status = IssueStatus.find(1)
74 76 @issue_status.update_attribute(:default_done_ratio, 50)
75 77 end
76 78
77 79 context "with Setting.issue_done_ratio using the issue_field" do
78 80 setup do
79 81 Setting.issue_done_ratio = 'issue_field'
80 82 end
81 83
82 84 should "change nothing" do
83 85 IssueStatus.update_issue_done_ratios
84 86
85 87 assert_equal 0, Issue.count(:conditions => {:done_ratio => 50})
86 88 end
87 89 end
88 90
89 91 context "with Setting.issue_done_ratio using the issue_status" do
90 92 setup do
91 93 Setting.issue_done_ratio = 'issue_status'
92 94 end
93 95
94 96 should "update all of the issue's done_ratios to match their Issue Status" do
95 97 IssueStatus.update_issue_done_ratios
96 98
97 99 issues = Issue.find([1,3,4,5,6,7,9,10])
98 100 issues.each do |issue|
99 101 assert_equal @issue_status, issue.status
100 102 assert_equal 50, issue.read_attribute(:done_ratio)
101 103 end
102 104 end
103 105 end
104 106 end
105 107 end
General Comments 0
You need to be logged in to leave comments. Login now