##// END OF EJS Templates
Document why relation is reversed after validation....
Jean-Philippe Lang -
r6058:79f25c08f8ce
parent child
Show More
@@ -1,126 +1,128
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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_DUPLICATED = "duplicated"
24 TYPE_DUPLICATED = "duplicated"
25 TYPE_BLOCKS = "blocks"
25 TYPE_BLOCKS = "blocks"
26 TYPE_BLOCKED = "blocked"
26 TYPE_BLOCKED = "blocked"
27 TYPE_PRECEDES = "precedes"
27 TYPE_PRECEDES = "precedes"
28 TYPE_FOLLOWS = "follows"
28 TYPE_FOLLOWS = "follows"
29
29
30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
31 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
31 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
37 }.freeze
37 }.freeze
38
38
39 validates_presence_of :issue_from, :issue_to, :relation_type
39 validates_presence_of :issue_from, :issue_to, :relation_type
40 validates_inclusion_of :relation_type, :in => TYPES.keys
40 validates_inclusion_of :relation_type, :in => TYPES.keys
41 validates_numericality_of :delay, :allow_nil => true
41 validates_numericality_of :delay, :allow_nil => true
42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
43
43
44 attr_protected :issue_from_id, :issue_to_id
44 attr_protected :issue_from_id, :issue_to_id
45
45
46 def after_initialize
46 def after_initialize
47 if new_record?
47 if new_record?
48 if relation_type.blank?
48 if relation_type.blank?
49 self.relation_type = IssueRelation::TYPE_RELATES
49 self.relation_type = IssueRelation::TYPE_RELATES
50 end
50 end
51 end
51 end
52 end
52 end
53
53
54 def validate
54 def validate
55 if issue_from && issue_to
55 if issue_from && issue_to
56 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
56 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
57 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
57 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
58 #detect circular dependencies depending wether the relation should be reversed
58 #detect circular dependencies depending wether the relation should be reversed
59 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
59 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
60 errors.add_to_base :circular_dependency if issue_from.all_dependent_issues.include? issue_to
60 errors.add_to_base :circular_dependency if issue_from.all_dependent_issues.include? issue_to
61 else
61 else
62 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from
62 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from
63 end
63 end
64 errors.add_to_base :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
64 errors.add_to_base :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
65 end
65 end
66 end
66 end
67
67
68 def other_issue(issue)
68 def other_issue(issue)
69 (self.issue_from_id == issue.id) ? issue_to : issue_from
69 (self.issue_from_id == issue.id) ? issue_to : issue_from
70 end
70 end
71
71
72 # Returns the relation type for +issue+
72 # Returns the relation type for +issue+
73 def relation_type_for(issue)
73 def relation_type_for(issue)
74 if TYPES[relation_type]
74 if TYPES[relation_type]
75 if self.issue_from_id == issue.id
75 if self.issue_from_id == issue.id
76 relation_type
76 relation_type
77 else
77 else
78 TYPES[relation_type][:sym]
78 TYPES[relation_type][:sym]
79 end
79 end
80 end
80 end
81 end
81 end
82
82
83 def label_for(issue)
83 def label_for(issue)
84 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
84 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
85 end
85 end
86
86
87 def before_save
87 def before_save
88 reverse_if_needed
88 reverse_if_needed
89
89
90 if TYPE_PRECEDES == relation_type
90 if TYPE_PRECEDES == relation_type
91 self.delay ||= 0
91 self.delay ||= 0
92 else
92 else
93 self.delay = nil
93 self.delay = nil
94 end
94 end
95 set_issue_to_dates
95 set_issue_to_dates
96 end
96 end
97
97
98 def set_issue_to_dates
98 def set_issue_to_dates
99 soonest_start = self.successor_soonest_start
99 soonest_start = self.successor_soonest_start
100 if soonest_start && issue_to
100 if soonest_start && issue_to
101 issue_to.reschedule_after(soonest_start)
101 issue_to.reschedule_after(soonest_start)
102 end
102 end
103 end
103 end
104
104
105 def successor_soonest_start
105 def successor_soonest_start
106 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
106 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
107 (issue_from.due_date || issue_from.start_date) + 1 + delay
107 (issue_from.due_date || issue_from.start_date) + 1 + delay
108 end
108 end
109 end
109 end
110
110
111 def <=>(relation)
111 def <=>(relation)
112 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
112 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
113 end
113 end
114
114
115 private
115 private
116
116
117 # Reverses the relation if needed so that it gets stored in the proper way
117 # Reverses the relation if needed so that it gets stored in the proper way
118 # Should not be reversed before validation so that it can be displayed back
119 # as entered on new relation form
118 def reverse_if_needed
120 def reverse_if_needed
119 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
121 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
120 issue_tmp = issue_to
122 issue_tmp = issue_to
121 self.issue_to = issue_from
123 self.issue_to = issue_from
122 self.issue_from = issue_tmp
124 self.issue_from = issue_tmp
123 self.relation_type = TYPES[relation_type][:reverse]
125 self.relation_type = TYPES[relation_type][:reverse]
124 end
126 end
125 end
127 end
126 end
128 end
@@ -1,103 +1,100
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
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_create_minimum
35 def test_create_minimum
36 relation = IssueRelation.new :issue_from => Issue.find(1), :issue_to => Issue.find(2)
36 relation = IssueRelation.new :issue_from => Issue.find(1), :issue_to => Issue.find(2)
37 assert relation.save
37 assert relation.save
38 assert_equal IssueRelation::TYPE_RELATES, relation.relation_type
38 assert_equal IssueRelation::TYPE_RELATES, relation.relation_type
39 end
39 end
40
40
41 def test_follows_relation_should_be_reversed
41 def test_follows_relation_should_be_reversed
42 from = Issue.find(1)
42 from = Issue.find(1)
43 to = Issue.find(2)
43 to = Issue.find(2)
44
44
45 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS
45 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS
46 assert relation.save
46 assert relation.save
47 relation.reload
47 relation.reload
48 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type
48 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type
49 assert_equal to, relation.issue_from
49 assert_equal to, relation.issue_from
50 assert_equal from, relation.issue_to
50 assert_equal from, relation.issue_to
51 end
51 end
52
52
53 # TODO : document why it shouldn't be reversed if validation fails : having
54 # relations reversed before the validation would allow simpler code for the
55 # validation
56 def test_follows_relation_should_not_be_reversed_if_validation_fails
53 def test_follows_relation_should_not_be_reversed_if_validation_fails
57 from = Issue.find(1)
54 from = Issue.find(1)
58 to = Issue.find(2)
55 to = Issue.find(2)
59
56
60 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS, :delay => 'xx'
57 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS, :delay => 'xx'
61 assert !relation.save
58 assert !relation.save
62 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type
59 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type
63 assert_equal from, relation.issue_from
60 assert_equal from, relation.issue_from
64 assert_equal to, relation.issue_to
61 assert_equal to, relation.issue_to
65 end
62 end
66
63
67 def test_relation_type_for
64 def test_relation_type_for
68 from = Issue.find(1)
65 from = Issue.find(1)
69 to = Issue.find(2)
66 to = Issue.find(2)
70
67
71 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_PRECEDES
68 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_PRECEDES
72 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type_for(from)
69 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type_for(from)
73 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type_for(to)
70 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type_for(to)
74 end
71 end
75
72
76 def test_set_issue_to_dates_without_issue_to
73 def test_set_issue_to_dates_without_issue_to
77 r = IssueRelation.new(:issue_from => Issue.new(:start_date => Date.today), :relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
74 r = IssueRelation.new(:issue_from => Issue.new(:start_date => Date.today), :relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
78 assert_nil r.set_issue_to_dates
75 assert_nil r.set_issue_to_dates
79 end
76 end
80
77
81 def test_set_issue_to_dates_without_issues
78 def test_set_issue_to_dates_without_issues
82 r = IssueRelation.new(:relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
79 r = IssueRelation.new(:relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
83 assert_nil r.set_issue_to_dates
80 assert_nil r.set_issue_to_dates
84 end
81 end
85
82
86 def test_validates_circular_dependency
83 def test_validates_circular_dependency
87 IssueRelation.delete_all
84 IssueRelation.delete_all
88 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => IssueRelation::TYPE_PRECEDES)
85 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => IssueRelation::TYPE_PRECEDES)
89 assert IssueRelation.create!(:issue_from => Issue.find(2), :issue_to => Issue.find(3), :relation_type => IssueRelation::TYPE_PRECEDES)
86 assert IssueRelation.create!(:issue_from => Issue.find(2), :issue_to => Issue.find(3), :relation_type => IssueRelation::TYPE_PRECEDES)
90 r = IssueRelation.new(:issue_from => Issue.find(3), :issue_to => Issue.find(1), :relation_type => IssueRelation::TYPE_PRECEDES)
87 r = IssueRelation.new(:issue_from => Issue.find(3), :issue_to => Issue.find(1), :relation_type => IssueRelation::TYPE_PRECEDES)
91 assert !r.save
88 assert !r.save
92 assert_not_nil r.errors.on(:base)
89 assert_not_nil r.errors.on(:base)
93 end
90 end
94
91
95 def test_validates_circular_dependency_on_reverse_relations
92 def test_validates_circular_dependency_on_reverse_relations
96 IssueRelation.delete_all
93 IssueRelation.delete_all
97 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(3), :relation_type => IssueRelation::TYPE_BLOCKS)
94 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(3), :relation_type => IssueRelation::TYPE_BLOCKS)
98 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => IssueRelation::TYPE_BLOCKED)
95 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => IssueRelation::TYPE_BLOCKED)
99 r = IssueRelation.new(:issue_from => Issue.find(2), :issue_to => Issue.find(1), :relation_type => IssueRelation::TYPE_BLOCKED)
96 r = IssueRelation.new(:issue_from => Issue.find(2), :issue_to => Issue.find(1), :relation_type => IssueRelation::TYPE_BLOCKED)
100 assert !r.save
97 assert !r.save
101 assert_not_nil r.errors.on(:base)
98 assert_not_nil r.errors.on(:base)
102 end
99 end
103 end
100 end
General Comments 0
You need to be logged in to leave comments. Login now