##// 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 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 IssueRelation < ActiveRecord::Base
19 19 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
20 20 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
21 21
22 22 TYPE_RELATES = "relates"
23 23 TYPE_DUPLICATES = "duplicates"
24 24 TYPE_DUPLICATED = "duplicated"
25 25 TYPE_BLOCKS = "blocks"
26 26 TYPE_BLOCKED = "blocked"
27 27 TYPE_PRECEDES = "precedes"
28 28 TYPE_FOLLOWS = "follows"
29 29
30 30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
31 31 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
32 32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
33 33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
34 34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
35 35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
36 36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
37 37 }.freeze
38 38
39 39 validates_presence_of :issue_from, :issue_to, :relation_type
40 40 validates_inclusion_of :relation_type, :in => TYPES.keys
41 41 validates_numericality_of :delay, :allow_nil => true
42 42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
43 43
44 44 attr_protected :issue_from_id, :issue_to_id
45 45
46 46 def after_initialize
47 47 if new_record?
48 48 if relation_type.blank?
49 49 self.relation_type = IssueRelation::TYPE_RELATES
50 50 end
51 51 end
52 52 end
53 53
54 54 def validate
55 55 if issue_from && issue_to
56 56 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
57 57 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
58 58 #detect circular dependencies depending wether the relation should be reversed
59 59 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
60 60 errors.add_to_base :circular_dependency if issue_from.all_dependent_issues.include? issue_to
61 61 else
62 62 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from
63 63 end
64 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 65 end
66 66 end
67 67
68 68 def other_issue(issue)
69 69 (self.issue_from_id == issue.id) ? issue_to : issue_from
70 70 end
71 71
72 72 # Returns the relation type for +issue+
73 73 def relation_type_for(issue)
74 74 if TYPES[relation_type]
75 75 if self.issue_from_id == issue.id
76 76 relation_type
77 77 else
78 78 TYPES[relation_type][:sym]
79 79 end
80 80 end
81 81 end
82 82
83 83 def label_for(issue)
84 84 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
85 85 end
86 86
87 87 def before_save
88 88 reverse_if_needed
89 89
90 90 if TYPE_PRECEDES == relation_type
91 91 self.delay ||= 0
92 92 else
93 93 self.delay = nil
94 94 end
95 95 set_issue_to_dates
96 96 end
97 97
98 98 def set_issue_to_dates
99 99 soonest_start = self.successor_soonest_start
100 100 if soonest_start && issue_to
101 101 issue_to.reschedule_after(soonest_start)
102 102 end
103 103 end
104 104
105 105 def successor_soonest_start
106 106 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
107 107 (issue_from.due_date || issue_from.start_date) + 1 + delay
108 108 end
109 109 end
110 110
111 111 def <=>(relation)
112 112 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
113 113 end
114 114
115 115 private
116 116
117 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 120 def reverse_if_needed
119 121 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
120 122 issue_tmp = issue_to
121 123 self.issue_to = issue_from
122 124 self.issue_from = issue_tmp
123 125 self.relation_type = TYPES[relation_type][:reverse]
124 126 end
125 127 end
126 128 end
@@ -1,103 +1,100
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 IssueRelationTest < ActiveSupport::TestCase
21 21 fixtures :issue_relations, :issues
22 22
23 23 def test_create
24 24 from = Issue.find(1)
25 25 to = Issue.find(2)
26 26
27 27 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_PRECEDES
28 28 assert relation.save
29 29 relation.reload
30 30 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type
31 31 assert_equal from, relation.issue_from
32 32 assert_equal to, relation.issue_to
33 33 end
34 34
35 35 def test_create_minimum
36 36 relation = IssueRelation.new :issue_from => Issue.find(1), :issue_to => Issue.find(2)
37 37 assert relation.save
38 38 assert_equal IssueRelation::TYPE_RELATES, relation.relation_type
39 39 end
40 40
41 41 def test_follows_relation_should_be_reversed
42 42 from = Issue.find(1)
43 43 to = Issue.find(2)
44 44
45 45 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS
46 46 assert relation.save
47 47 relation.reload
48 48 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type
49 49 assert_equal to, relation.issue_from
50 50 assert_equal from, relation.issue_to
51 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 53 def test_follows_relation_should_not_be_reversed_if_validation_fails
57 54 from = Issue.find(1)
58 55 to = Issue.find(2)
59 56
60 57 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS, :delay => 'xx'
61 58 assert !relation.save
62 59 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type
63 60 assert_equal from, relation.issue_from
64 61 assert_equal to, relation.issue_to
65 62 end
66 63
67 64 def test_relation_type_for
68 65 from = Issue.find(1)
69 66 to = Issue.find(2)
70 67
71 68 relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_PRECEDES
72 69 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type_for(from)
73 70 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type_for(to)
74 71 end
75 72
76 73 def test_set_issue_to_dates_without_issue_to
77 74 r = IssueRelation.new(:issue_from => Issue.new(:start_date => Date.today), :relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
78 75 assert_nil r.set_issue_to_dates
79 76 end
80 77
81 78 def test_set_issue_to_dates_without_issues
82 79 r = IssueRelation.new(:relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
83 80 assert_nil r.set_issue_to_dates
84 81 end
85 82
86 83 def test_validates_circular_dependency
87 84 IssueRelation.delete_all
88 85 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => IssueRelation::TYPE_PRECEDES)
89 86 assert IssueRelation.create!(:issue_from => Issue.find(2), :issue_to => Issue.find(3), :relation_type => IssueRelation::TYPE_PRECEDES)
90 87 r = IssueRelation.new(:issue_from => Issue.find(3), :issue_to => Issue.find(1), :relation_type => IssueRelation::TYPE_PRECEDES)
91 88 assert !r.save
92 89 assert_not_nil r.errors.on(:base)
93 90 end
94 91
95 92 def test_validates_circular_dependency_on_reverse_relations
96 93 IssueRelation.delete_all
97 94 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(3), :relation_type => IssueRelation::TYPE_BLOCKS)
98 95 assert IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => IssueRelation::TYPE_BLOCKED)
99 96 r = IssueRelation.new(:issue_from => Issue.find(2), :issue_to => Issue.find(1), :relation_type => IssueRelation::TYPE_BLOCKED)
100 97 assert !r.save
101 98 assert_not_nil r.errors.on(:base)
102 99 end
103 100 end
General Comments 0
You need to be logged in to leave comments. Login now