@@ -1,96 +1,96 | |||||
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 | class IssueRelation < ActiveRecord::Base |
|
18 | class IssueRelation < ActiveRecord::Base | |
19 | belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' |
|
19 | belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' | |
20 | belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' |
|
20 | belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' | |
21 |
|
21 | |||
22 | TYPE_RELATES = "relates" |
|
22 | TYPE_RELATES = "relates" | |
23 | TYPE_DUPLICATES = "duplicates" |
|
23 | TYPE_DUPLICATES = "duplicates" | |
24 | TYPE_BLOCKS = "blocks" |
|
24 | TYPE_BLOCKS = "blocks" | |
25 | TYPE_PRECEDES = "precedes" |
|
25 | TYPE_PRECEDES = "precedes" | |
26 | TYPE_FOLLOWS = "follows" |
|
26 | TYPE_FOLLOWS = "follows" | |
27 |
|
27 | |||
28 | TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1 }, |
|
28 | TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1 }, | |
29 | TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2 }, |
|
29 | TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2 }, | |
30 | TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 3 }, |
|
30 | TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 3 }, | |
31 | TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 4 }, |
|
31 | TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 4 }, | |
32 | TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 5 } |
|
32 | TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 5 } | |
33 | }.freeze |
|
33 | }.freeze | |
34 |
|
34 | |||
35 | validates_presence_of :issue_from, :issue_to, :relation_type |
|
35 | validates_presence_of :issue_from, :issue_to, :relation_type | |
36 |
validates_inclusion_of :relation_type, :in => |
|
36 | validates_inclusion_of :relation_type, :in => TYPES.keys | |
37 | validates_numericality_of :delay, :allow_nil => true |
|
37 | validates_numericality_of :delay, :allow_nil => true | |
38 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id |
|
38 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id | |
39 |
|
39 | |||
40 | attr_protected :issue_from_id, :issue_to_id |
|
40 | attr_protected :issue_from_id, :issue_to_id | |
41 |
|
41 | |||
42 | before_validation :reverse_if_needed |
|
|||
43 |
|
||||
44 | def validate |
|
42 | def validate | |
45 | if issue_from && issue_to |
|
43 | if issue_from && issue_to | |
46 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id |
|
44 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id | |
47 | errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations? |
|
45 | errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations? | |
48 | errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from |
|
46 | errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from | |
49 | end |
|
47 | end | |
50 | end |
|
48 | end | |
51 |
|
49 | |||
52 | def other_issue(issue) |
|
50 | def other_issue(issue) | |
53 | (self.issue_from_id == issue.id) ? issue_to : issue_from |
|
51 | (self.issue_from_id == issue.id) ? issue_to : issue_from | |
54 | end |
|
52 | end | |
55 |
|
53 | |||
56 | def label_for(issue) |
|
54 | def label_for(issue) | |
57 | TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow |
|
55 | TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow | |
58 | end |
|
56 | end | |
59 |
|
57 | |||
60 | def before_save |
|
58 | def before_save | |
|
59 | reverse_if_needed | |||
|
60 | ||||
61 | if TYPE_PRECEDES == relation_type |
|
61 | if TYPE_PRECEDES == relation_type | |
62 | self.delay ||= 0 |
|
62 | self.delay ||= 0 | |
63 | else |
|
63 | else | |
64 | self.delay = nil |
|
64 | self.delay = nil | |
65 | end |
|
65 | end | |
66 | set_issue_to_dates |
|
66 | set_issue_to_dates | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def set_issue_to_dates |
|
69 | def set_issue_to_dates | |
70 | soonest_start = self.successor_soonest_start |
|
70 | soonest_start = self.successor_soonest_start | |
71 | if soonest_start && (!issue_to.start_date || issue_to.start_date < soonest_start) |
|
71 | if soonest_start && (!issue_to.start_date || issue_to.start_date < soonest_start) | |
72 | issue_to.start_date, issue_to.due_date = successor_soonest_start, successor_soonest_start + issue_to.duration |
|
72 | issue_to.start_date, issue_to.due_date = successor_soonest_start, successor_soonest_start + issue_to.duration | |
73 | issue_to.save |
|
73 | issue_to.save | |
74 | end |
|
74 | end | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def successor_soonest_start |
|
77 | def successor_soonest_start | |
78 | return nil unless (TYPE_PRECEDES == self.relation_type) && (issue_from.start_date || issue_from.due_date) |
|
78 | return nil unless (TYPE_PRECEDES == self.relation_type) && (issue_from.start_date || issue_from.due_date) | |
79 | (issue_from.due_date || issue_from.start_date) + 1 + delay |
|
79 | (issue_from.due_date || issue_from.start_date) + 1 + delay | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | def <=>(relation) |
|
82 | def <=>(relation) | |
83 | TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] |
|
83 | TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | private |
|
86 | private | |
87 |
|
87 | |||
88 | def reverse_if_needed |
|
88 | def reverse_if_needed | |
89 | if (TYPE_FOLLOWS == relation_type) |
|
89 | if (TYPE_FOLLOWS == relation_type) | |
90 | issue_tmp = issue_to |
|
90 | issue_tmp = issue_to | |
91 | self.issue_to = issue_from |
|
91 | self.issue_to = issue_from | |
92 | self.issue_from = issue_tmp |
|
92 | self.issue_from = issue_tmp | |
93 | self.relation_type = TYPE_PRECEDES |
|
93 | self.relation_type = TYPE_PRECEDES | |
94 | end |
|
94 | end | |
95 | end |
|
95 | end | |
96 | end |
|
96 | end |
@@ -1,46 +1,57 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2009 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 IssueRelationTest < ActiveSupport::TestCase |
|
20 | class IssueRelationTest < ActiveSupport::TestCase | |
21 | fixtures :issue_relations, :issues |
|
21 | fixtures :issue_relations, :issues | |
22 |
|
22 | |||
23 | def test_create |
|
23 | def test_create | |
24 | from = Issue.find(1) |
|
24 | from = Issue.find(1) | |
25 | to = Issue.find(2) |
|
25 | to = Issue.find(2) | |
26 |
|
26 | |||
27 | relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_PRECEDES |
|
27 | relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_PRECEDES | |
28 | assert relation.save |
|
28 | assert relation.save | |
29 | relation.reload |
|
29 | relation.reload | |
30 | assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type |
|
30 | assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type | |
31 | assert_equal from, relation.issue_from |
|
31 | assert_equal from, relation.issue_from | |
32 | assert_equal to, relation.issue_to |
|
32 | assert_equal to, relation.issue_to | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | def test_follows_relation_should_be_reversed |
|
35 | def test_follows_relation_should_be_reversed | |
36 | from = Issue.find(1) |
|
36 | from = Issue.find(1) | |
37 | to = Issue.find(2) |
|
37 | to = Issue.find(2) | |
38 |
|
38 | |||
39 | relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS |
|
39 | relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS | |
40 | assert relation.save |
|
40 | assert relation.save | |
41 | relation.reload |
|
41 | relation.reload | |
42 | assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type |
|
42 | assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type | |
43 | assert_equal to, relation.issue_from |
|
43 | assert_equal to, relation.issue_from | |
44 | assert_equal from, relation.issue_to |
|
44 | assert_equal from, relation.issue_to | |
45 | end |
|
45 | end | |
|
46 | ||||
|
47 | def test_follows_relation_should_not_be_reversed_if_validation_fails | |||
|
48 | from = Issue.find(1) | |||
|
49 | to = Issue.find(2) | |||
|
50 | ||||
|
51 | relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS, :delay => 'xx' | |||
|
52 | assert !relation.save | |||
|
53 | assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type | |||
|
54 | assert_equal from, relation.issue_from | |||
|
55 | assert_equal to, relation.issue_to | |||
|
56 | end | |||
46 | end |
|
57 | end |
General Comments 0
You need to be logged in to leave comments.
Login now