Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class IssueRelation < ActiveRecord::Base
|
Chris@0
|
19 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
|
Chris@0
|
20 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
|
Chris@909
|
21
|
Chris@0
|
22 TYPE_RELATES = "relates"
|
Chris@0
|
23 TYPE_DUPLICATES = "duplicates"
|
Chris@0
|
24 TYPE_DUPLICATED = "duplicated"
|
Chris@0
|
25 TYPE_BLOCKS = "blocks"
|
Chris@0
|
26 TYPE_BLOCKED = "blocked"
|
Chris@0
|
27 TYPE_PRECEDES = "precedes"
|
Chris@0
|
28 TYPE_FOLLOWS = "follows"
|
Chris@909
|
29
|
Chris@0
|
30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
|
Chris@0
|
31 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
|
Chris@0
|
32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
|
Chris@0
|
33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
|
Chris@0
|
34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
|
Chris@0
|
35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
|
Chris@0
|
36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
|
Chris@0
|
37 }.freeze
|
Chris@909
|
38
|
Chris@0
|
39 validates_presence_of :issue_from, :issue_to, :relation_type
|
Chris@0
|
40 validates_inclusion_of :relation_type, :in => TYPES.keys
|
Chris@0
|
41 validates_numericality_of :delay, :allow_nil => true
|
Chris@0
|
42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
|
Chris@909
|
43
|
Chris@909
|
44 validate :validate_issue_relation
|
Chris@909
|
45
|
Chris@0
|
46 attr_protected :issue_from_id, :issue_to_id
|
Chris@909
|
47
|
Chris@909
|
48 before_save :handle_issue_order
|
Chris@909
|
49
|
Chris@909
|
50 def visible?(user=User.current)
|
Chris@909
|
51 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
|
Chris@909
|
52 end
|
Chris@909
|
53
|
Chris@909
|
54 def deletable?(user=User.current)
|
Chris@909
|
55 visible?(user) &&
|
Chris@909
|
56 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
|
Chris@909
|
57 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
|
Chris@909
|
58 end
|
Chris@909
|
59
|
Chris@909
|
60 def after_initialize
|
Chris@909
|
61 if new_record?
|
Chris@909
|
62 if relation_type.blank?
|
Chris@909
|
63 self.relation_type = IssueRelation::TYPE_RELATES
|
Chris@909
|
64 end
|
Chris@909
|
65 end
|
Chris@909
|
66 end
|
Chris@909
|
67
|
Chris@909
|
68 def validate_issue_relation
|
Chris@0
|
69 if issue_from && issue_to
|
Chris@0
|
70 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
|
Chris@0
|
71 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
|
Chris@507
|
72 #detect circular dependencies depending wether the relation should be reversed
|
Chris@507
|
73 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
|
Chris@909
|
74 errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
|
Chris@507
|
75 else
|
Chris@909
|
76 errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
|
Chris@507
|
77 end
|
Chris@909
|
78 errors.add :base, :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
|
Chris@0
|
79 end
|
Chris@0
|
80 end
|
Chris@909
|
81
|
Chris@0
|
82 def other_issue(issue)
|
Chris@0
|
83 (self.issue_from_id == issue.id) ? issue_to : issue_from
|
Chris@0
|
84 end
|
Chris@909
|
85
|
Chris@0
|
86 # Returns the relation type for +issue+
|
Chris@0
|
87 def relation_type_for(issue)
|
Chris@0
|
88 if TYPES[relation_type]
|
Chris@0
|
89 if self.issue_from_id == issue.id
|
Chris@0
|
90 relation_type
|
Chris@0
|
91 else
|
Chris@0
|
92 TYPES[relation_type][:sym]
|
Chris@0
|
93 end
|
Chris@0
|
94 end
|
Chris@0
|
95 end
|
Chris@909
|
96
|
Chris@0
|
97 def label_for(issue)
|
Chris@0
|
98 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
|
Chris@0
|
99 end
|
Chris@909
|
100
|
Chris@909
|
101 def handle_issue_order
|
Chris@0
|
102 reverse_if_needed
|
Chris@909
|
103
|
Chris@0
|
104 if TYPE_PRECEDES == relation_type
|
Chris@0
|
105 self.delay ||= 0
|
Chris@0
|
106 else
|
Chris@0
|
107 self.delay = nil
|
Chris@0
|
108 end
|
Chris@0
|
109 set_issue_to_dates
|
Chris@0
|
110 end
|
Chris@909
|
111
|
Chris@0
|
112 def set_issue_to_dates
|
Chris@0
|
113 soonest_start = self.successor_soonest_start
|
Chris@119
|
114 if soonest_start && issue_to
|
Chris@0
|
115 issue_to.reschedule_after(soonest_start)
|
Chris@0
|
116 end
|
Chris@0
|
117 end
|
Chris@909
|
118
|
Chris@0
|
119 def successor_soonest_start
|
Chris@119
|
120 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
|
Chris@119
|
121 (issue_from.due_date || issue_from.start_date) + 1 + delay
|
Chris@119
|
122 end
|
Chris@0
|
123 end
|
Chris@909
|
124
|
Chris@0
|
125 def <=>(relation)
|
Chris@0
|
126 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
|
Chris@0
|
127 end
|
Chris@909
|
128
|
Chris@0
|
129 private
|
Chris@909
|
130
|
Chris@0
|
131 # Reverses the relation if needed so that it gets stored in the proper way
|
Chris@909
|
132 # Should not be reversed before validation so that it can be displayed back
|
Chris@909
|
133 # as entered on new relation form
|
Chris@0
|
134 def reverse_if_needed
|
Chris@0
|
135 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
|
Chris@0
|
136 issue_tmp = issue_to
|
Chris@0
|
137 self.issue_to = issue_from
|
Chris@0
|
138 self.issue_from = issue_tmp
|
Chris@0
|
139 self.relation_type = TYPES[relation_type][:reverse]
|
Chris@0
|
140 end
|
Chris@0
|
141 end
|
Chris@0
|
142 end
|