##// END OF EJS Templates
Adds 'Blocked by' (#1755) and 'Duplicated by' relation types to the dropdown menu for new relations....
Jean-Philippe Lang -
r3398:d6c299f57d82
parent child
Show More
@@ -1,96 +1,101
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_DUPLICATED = "duplicated"
24 TYPE_BLOCKS = "blocks"
25 TYPE_BLOCKS = "blocks"
26 TYPE_BLOCKED = "blocked"
25 TYPE_PRECEDES = "precedes"
27 TYPE_PRECEDES = "precedes"
26 TYPE_FOLLOWS = "follows"
28 TYPE_FOLLOWS = "follows"
27
29
28 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1 },
30 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 },
31 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 },
32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :reverse => TYPE_DUPLICATES },
31 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 4 },
33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4 },
32 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 5 }
34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :reverse => TYPE_BLOCKS },
35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6 },
36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :reverse => TYPE_PRECEDES }
33 }.freeze
37 }.freeze
34
38
35 validates_presence_of :issue_from, :issue_to, :relation_type
39 validates_presence_of :issue_from, :issue_to, :relation_type
36 validates_inclusion_of :relation_type, :in => TYPES.keys
40 validates_inclusion_of :relation_type, :in => TYPES.keys
37 validates_numericality_of :delay, :allow_nil => true
41 validates_numericality_of :delay, :allow_nil => true
38 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
39
43
40 attr_protected :issue_from_id, :issue_to_id
44 attr_protected :issue_from_id, :issue_to_id
41
45
42 def validate
46 def validate
43 if issue_from && issue_to
47 if issue_from && issue_to
44 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
48 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
45 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
49 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
46 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from
50 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from
47 end
51 end
48 end
52 end
49
53
50 def other_issue(issue)
54 def other_issue(issue)
51 (self.issue_from_id == issue.id) ? issue_to : issue_from
55 (self.issue_from_id == issue.id) ? issue_to : issue_from
52 end
56 end
53
57
54 def label_for(issue)
58 def label_for(issue)
55 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
59 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
56 end
60 end
57
61
58 def before_save
62 def before_save
59 reverse_if_needed
63 reverse_if_needed
60
64
61 if TYPE_PRECEDES == relation_type
65 if TYPE_PRECEDES == relation_type
62 self.delay ||= 0
66 self.delay ||= 0
63 else
67 else
64 self.delay = nil
68 self.delay = nil
65 end
69 end
66 set_issue_to_dates
70 set_issue_to_dates
67 end
71 end
68
72
69 def set_issue_to_dates
73 def set_issue_to_dates
70 soonest_start = self.successor_soonest_start
74 soonest_start = self.successor_soonest_start
71 if soonest_start && (!issue_to.start_date || issue_to.start_date < soonest_start)
75 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
76 issue_to.start_date, issue_to.due_date = successor_soonest_start, successor_soonest_start + issue_to.duration
73 issue_to.save
77 issue_to.save
74 end
78 end
75 end
79 end
76
80
77 def successor_soonest_start
81 def successor_soonest_start
78 return nil unless (TYPE_PRECEDES == self.relation_type) && (issue_from.start_date || issue_from.due_date)
82 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
83 (issue_from.due_date || issue_from.start_date) + 1 + delay
80 end
84 end
81
85
82 def <=>(relation)
86 def <=>(relation)
83 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
87 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
84 end
88 end
85
89
86 private
90 private
87
91
92 # Reverses the relation if needed so that it gets stored in the proper way
88 def reverse_if_needed
93 def reverse_if_needed
89 if (TYPE_FOLLOWS == relation_type)
94 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
90 issue_tmp = issue_to
95 issue_tmp = issue_to
91 self.issue_to = issue_from
96 self.issue_to = issue_from
92 self.issue_from = issue_tmp
97 self.issue_from = issue_tmp
93 self.relation_type = TYPE_PRECEDES
98 self.relation_type = TYPES[relation_type][:reverse]
94 end
99 end
95 end
100 end
96 end
101 end
General Comments 0
You need to be logged in to leave comments. Login now