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