@@ -1,104 +1,105 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 | 20 | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" |
|
21 | 21 | acts_as_list |
|
22 | 22 | |
|
23 | 23 | before_destroy :delete_workflow_rules |
|
24 | 24 | after_save :update_default |
|
25 | 25 | |
|
26 | 26 | validates_presence_of :name |
|
27 | 27 | validates_uniqueness_of :name |
|
28 | 28 | validates_length_of :name, :maximum => 30 |
|
29 | 29 | validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true |
|
30 | 30 | |
|
31 | scope :sorted, order("#{table_name}.position ASC") | |
|
31 | 32 | scope :named, lambda {|arg| where(["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip])} |
|
32 | 33 | |
|
33 | 34 | def update_default |
|
34 | 35 | IssueStatus.update_all({:is_default => false}, ['id <> ?', id]) if self.is_default? |
|
35 | 36 | end |
|
36 | 37 | |
|
37 | 38 | # Returns the default status for new issues |
|
38 | 39 | def self.default |
|
39 | 40 | where(:is_default => true).first |
|
40 | 41 | end |
|
41 | 42 | |
|
42 | 43 | # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+ |
|
43 | 44 | def self.update_issue_done_ratios |
|
44 | 45 | if Issue.use_status_for_done_ratio? |
|
45 | 46 | IssueStatus.where("default_done_ratio >= 0").all.each do |status| |
|
46 | 47 | Issue.update_all({:done_ratio => status.default_done_ratio}, {:status_id => status.id}) |
|
47 | 48 | end |
|
48 | 49 | end |
|
49 | 50 | |
|
50 | 51 | return Issue.use_status_for_done_ratio? |
|
51 | 52 | end |
|
52 | 53 | |
|
53 | 54 | # Returns an array of all statuses the given role can switch to |
|
54 | 55 | # Uses association cache when called more than one time |
|
55 | 56 | def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
|
56 | 57 | if roles && tracker |
|
57 | 58 | role_ids = roles.collect(&:id) |
|
58 | 59 | transitions = workflows.select do |w| |
|
59 | 60 | role_ids.include?(w.role_id) && |
|
60 | 61 | w.tracker_id == tracker.id && |
|
61 | 62 | ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee)) |
|
62 | 63 | end |
|
63 | 64 | transitions.map(&:new_status).compact.sort |
|
64 | 65 | else |
|
65 | 66 | [] |
|
66 | 67 | end |
|
67 | 68 | end |
|
68 | 69 | |
|
69 | 70 | # Same thing as above but uses a database query |
|
70 | 71 | # More efficient than the previous method if called just once |
|
71 | 72 | def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
|
72 | 73 | if roles.present? && tracker |
|
73 | 74 | conditions = "(author = :false AND assignee = :false)" |
|
74 | 75 | conditions << " OR author = :true" if author |
|
75 | 76 | conditions << " OR assignee = :true" if assignee |
|
76 | 77 | |
|
77 | 78 | workflows. |
|
78 | 79 | includes(:new_status). |
|
79 | 80 | where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})", |
|
80 | 81 | {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false} |
|
81 | 82 | ]).all. |
|
82 | 83 | map(&:new_status).compact.sort |
|
83 | 84 | else |
|
84 | 85 | [] |
|
85 | 86 | end |
|
86 | 87 | end |
|
87 | 88 | |
|
88 | 89 | def <=>(status) |
|
89 | 90 | position <=> status.position |
|
90 | 91 | end |
|
91 | 92 | |
|
92 | 93 | def to_s; name end |
|
93 | 94 | |
|
94 | 95 | private |
|
95 | 96 | |
|
96 | 97 | def check_integrity |
|
97 | 98 | raise "Can't delete status" if Issue.where(:status_id => id).any? |
|
98 | 99 | end |
|
99 | 100 | |
|
100 | 101 | # Deletes associated workflows |
|
101 | 102 | def delete_workflow_rules |
|
102 | 103 | WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) |
|
103 | 104 | end |
|
104 | 105 | end |
@@ -1,120 +1,124 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class IssueStatusTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :issue_statuses, :issues, :roles, :trackers |
|
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 | 35 | status = IssueStatus.find(3) |
|
36 | 36 | assert_difference 'IssueStatus.count', -1 do |
|
37 | 37 | assert status.destroy |
|
38 | 38 | end |
|
39 | 39 | assert_nil WorkflowTransition.first(:conditions => {:old_status_id => status.id}) |
|
40 | 40 | assert_nil WorkflowTransition.first(:conditions => {:new_status_id => status.id}) |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def test_destroy_status_in_use |
|
44 | 44 | # Status assigned to an Issue |
|
45 | 45 | status = Issue.find(1).status |
|
46 | 46 | assert_raise(RuntimeError, "Can't delete status") { status.destroy } |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def test_default |
|
50 | 50 | status = IssueStatus.default |
|
51 | 51 | assert_kind_of IssueStatus, status |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def test_change_default |
|
55 | 55 | status = IssueStatus.find(2) |
|
56 | 56 | assert !status.is_default |
|
57 | 57 | status.is_default = true |
|
58 | 58 | assert status.save |
|
59 | 59 | status.reload |
|
60 | 60 | |
|
61 | 61 | assert_equal status, IssueStatus.default |
|
62 | 62 | assert !IssueStatus.find(1).is_default |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def test_reorder_should_not_clear_default_status |
|
66 | 66 | status = IssueStatus.default |
|
67 | 67 | status.move_to_bottom |
|
68 | 68 | status.reload |
|
69 | 69 | assert status.is_default? |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def test_new_statuses_allowed_to |
|
73 | 73 | WorkflowTransition.delete_all |
|
74 | 74 | |
|
75 | 75 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false) |
|
76 | 76 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false) |
|
77 | 77 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true) |
|
78 | 78 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true) |
|
79 | 79 | status = IssueStatus.find(1) |
|
80 | 80 | role = Role.find(1) |
|
81 | 81 | tracker = Tracker.find(1) |
|
82 | 82 | |
|
83 | 83 | assert_equal [2], status.new_statuses_allowed_to([role], tracker, false, false).map(&:id) |
|
84 | 84 | assert_equal [2], status.find_new_statuses_allowed_to([role], tracker, false, false).map(&:id) |
|
85 | 85 | |
|
86 | 86 | assert_equal [2, 3, 5], status.new_statuses_allowed_to([role], tracker, true, false).map(&:id) |
|
87 | 87 | assert_equal [2, 3, 5], status.find_new_statuses_allowed_to([role], tracker, true, false).map(&:id) |
|
88 | 88 | |
|
89 | 89 | assert_equal [2, 4, 5], status.new_statuses_allowed_to([role], tracker, false, true).map(&:id) |
|
90 | 90 | assert_equal [2, 4, 5], status.find_new_statuses_allowed_to([role], tracker, false, true).map(&:id) |
|
91 | 91 | |
|
92 | 92 | assert_equal [2, 3, 4, 5], status.new_statuses_allowed_to([role], tracker, true, true).map(&:id) |
|
93 | 93 | assert_equal [2, 3, 4, 5], status.find_new_statuses_allowed_to([role], tracker, true, true).map(&:id) |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def test_update_done_ratios_with_issue_done_ratio_set_to_issue_field_should_change_nothing |
|
97 | 97 | IssueStatus.find(1).update_attribute(:default_done_ratio, 50) |
|
98 | 98 | |
|
99 | 99 | with_settings :issue_done_ratio => 'issue_field' do |
|
100 | 100 | IssueStatus.update_issue_done_ratios |
|
101 | 101 | assert_equal 0, Issue.count(:conditions => {:done_ratio => 50}) |
|
102 | 102 | end |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def test_update_done_ratios_with_issue_done_ratio_set_to_issue_status_should_update_issues |
|
106 | 106 | IssueStatus.find(1).update_attribute(:default_done_ratio, 50) |
|
107 | 107 | |
|
108 | 108 | with_settings :issue_done_ratio => 'issue_status' do |
|
109 | 109 | IssueStatus.update_issue_done_ratios |
|
110 | 110 | issues = Issue.all(:conditions => {:status_id => 1}) |
|
111 | 111 | assert_equal [50], issues.map {|issue| issue.read_attribute(:done_ratio)}.uniq |
|
112 | 112 | end |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | def test_sorted_scope | |
|
116 | assert_equal IssueStatus.all.sort, IssueStatus.sorted.all | |
|
117 | end | |
|
118 | ||
|
115 | 119 | def test_named_scope |
|
116 | 120 | status = IssueStatus.named("resolved").first |
|
117 | 121 | assert_not_nil status |
|
118 | 122 | assert_equal "Resolved", status.name |
|
119 | 123 | end |
|
120 | 124 | end |
General Comments 0
You need to be logged in to leave comments.
Login now