@@ -1,69 +1,69 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class IssueStatus < ActiveRecord::Base |
|
18 | class IssueStatus < ActiveRecord::Base | |
19 | before_destroy :check_integrity |
|
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", :dependent => :delete_all | |
21 | acts_as_list |
|
21 | acts_as_list | |
22 |
|
22 | |||
23 | validates_presence_of :name |
|
23 | validates_presence_of :name | |
24 | validates_uniqueness_of :name |
|
24 | validates_uniqueness_of :name | |
25 | validates_length_of :name, :maximum => 30 |
|
25 | validates_length_of :name, :maximum => 30 | |
26 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i |
|
26 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i | |
27 |
|
27 | |||
28 |
def |
|
28 | def after_save | |
29 |
IssueStatus.update_all |
|
29 | IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default? | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | # Returns the default status for new issues |
|
32 | # Returns the default status for new issues | |
33 | def self.default |
|
33 | def self.default | |
34 | find(:first, :conditions =>["is_default=?", true]) |
|
34 | find(:first, :conditions =>["is_default=?", true]) | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | # Returns an array of all statuses the given role can switch to |
|
37 | # Returns an array of all statuses the given role can switch to | |
38 | # Uses association cache when called more than one time |
|
38 | # Uses association cache when called more than one time | |
39 | def new_statuses_allowed_to(role, tracker) |
|
39 | def new_statuses_allowed_to(role, tracker) | |
40 | new_statuses = workflows.select {|w| w.role_id == role.id && w.tracker_id == tracker.id}.collect{|w| w.new_status} if role && tracker |
|
40 | new_statuses = workflows.select {|w| w.role_id == role.id && w.tracker_id == tracker.id}.collect{|w| w.new_status} if role && tracker | |
41 | new_statuses ? new_statuses.compact.sort{|x, y| x.position <=> y.position } : [] |
|
41 | new_statuses ? new_statuses.compact.sort{|x, y| x.position <=> y.position } : [] | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | # Same thing as above but uses a database query |
|
44 | # Same thing as above but uses a database query | |
45 | # More efficient than the previous method if called just once |
|
45 | # More efficient than the previous method if called just once | |
46 | def find_new_statuses_allowed_to(role, tracker) |
|
46 | def find_new_statuses_allowed_to(role, tracker) | |
47 | new_statuses = workflows.find(:all, |
|
47 | new_statuses = workflows.find(:all, | |
48 | :include => :new_status, |
|
48 | :include => :new_status, | |
49 | :conditions => ["role_id=? and tracker_id=?", role.id, tracker.id]).collect{ |w| w.new_status }.compact if role && tracker |
|
49 | :conditions => ["role_id=? and tracker_id=?", role.id, tracker.id]).collect{ |w| w.new_status }.compact if role && tracker | |
50 | new_statuses ? new_statuses.sort{|x, y| x.position <=> y.position } : [] |
|
50 | new_statuses ? new_statuses.sort{|x, y| x.position <=> y.position } : [] | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | def new_status_allowed_to?(status, role, tracker) |
|
53 | def new_status_allowed_to?(status, role, tracker) | |
54 | status && role && tracker ? |
|
54 | status && role && tracker ? | |
55 | !workflows.find(:first, :conditions => {:new_status_id => status.id, :role_id => role.id, :tracker_id => tracker.id}).nil? : |
|
55 | !workflows.find(:first, :conditions => {:new_status_id => status.id, :role_id => role.id, :tracker_id => tracker.id}).nil? : | |
56 | false |
|
56 | false | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def <=>(status) |
|
59 | def <=>(status) | |
60 | position <=> status.position |
|
60 | position <=> status.position | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def to_s; name end |
|
63 | def to_s; name end | |
64 |
|
64 | |||
65 | private |
|
65 | private | |
66 | def check_integrity |
|
66 | def check_integrity | |
67 | raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id]) |
|
67 | raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id]) | |
68 | end |
|
68 | end | |
69 | end |
|
69 | end |
@@ -1,49 +1,56 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 |
|
19 | |||
20 | class IssueStatusTest < Test::Unit::TestCase |
|
20 | class IssueStatusTest < Test::Unit::TestCase | |
21 | fixtures :issue_statuses |
|
21 | fixtures :issue_statuses | |
22 |
|
22 | |||
23 | def test_create |
|
23 | def test_create | |
24 | status = IssueStatus.new :name => "Assigned" |
|
24 | status = IssueStatus.new :name => "Assigned" | |
25 | assert !status.save |
|
25 | assert !status.save | |
26 | # status name uniqueness |
|
26 | # status name uniqueness | |
27 | assert_equal 1, status.errors.count |
|
27 | assert_equal 1, status.errors.count | |
28 |
|
28 | |||
29 | status.name = "Test Status" |
|
29 | status.name = "Test Status" | |
30 | assert status.save |
|
30 | assert status.save | |
31 | assert !status.is_default |
|
31 | assert !status.is_default | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def test_default |
|
34 | def test_default | |
35 | status = IssueStatus.default |
|
35 | status = IssueStatus.default | |
36 | assert_kind_of IssueStatus, status |
|
36 | assert_kind_of IssueStatus, status | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_change_default |
|
39 | def test_change_default | |
40 | status = IssueStatus.find(2) |
|
40 | status = IssueStatus.find(2) | |
41 | assert !status.is_default |
|
41 | assert !status.is_default | |
42 | status.is_default = true |
|
42 | status.is_default = true | |
43 | assert status.save |
|
43 | assert status.save | |
44 | status.reload |
|
44 | status.reload | |
45 |
|
45 | |||
46 | assert_equal status, IssueStatus.default |
|
46 | assert_equal status, IssueStatus.default | |
47 | assert !IssueStatus.find(1).is_default |
|
47 | assert !IssueStatus.find(1).is_default | |
48 | end |
|
48 | end | |
|
49 | ||||
|
50 | def test_reorder_should_not_clear_default_status | |||
|
51 | status = IssueStatus.default | |||
|
52 | status.move_to_bottom | |||
|
53 | status.reload | |||
|
54 | assert status.is_default? | |||
|
55 | end | |||
49 | end |
|
56 | end |
General Comments 0
You need to be logged in to leave comments.
Login now