@@ -1,166 +1,182 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 used to represent the relations of an issue |
|
19 | 19 | class IssueRelations < Array |
|
20 | 20 | include Redmine::I18n |
|
21 | 21 | |
|
22 | 22 | def initialize(issue, *args) |
|
23 | 23 | @issue = issue |
|
24 | 24 | super(*args) |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | def to_s(*args) |
|
28 | 28 | map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ') |
|
29 | 29 | end |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | class IssueRelation < ActiveRecord::Base |
|
33 | 33 | belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' |
|
34 | 34 | belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' |
|
35 | 35 | |
|
36 | 36 | TYPE_RELATES = "relates" |
|
37 | 37 | TYPE_DUPLICATES = "duplicates" |
|
38 | 38 | TYPE_DUPLICATED = "duplicated" |
|
39 | 39 | TYPE_BLOCKS = "blocks" |
|
40 | 40 | TYPE_BLOCKED = "blocked" |
|
41 | 41 | TYPE_PRECEDES = "precedes" |
|
42 | 42 | TYPE_FOLLOWS = "follows" |
|
43 | 43 | TYPE_COPIED_TO = "copied_to" |
|
44 | 44 | TYPE_COPIED_FROM = "copied_from" |
|
45 | 45 | |
|
46 | TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES }, | |
|
47 |
|
|
|
48 | TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, | |
|
49 |
|
|
|
50 | TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS }, | |
|
51 |
|
|
|
52 |
|
|
|
53 | TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from, :order => 8, :sym => TYPE_COPIED_FROM }, | |
|
54 | TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to, :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO } | |
|
55 | }.freeze | |
|
46 | TYPES = { | |
|
47 | TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, | |
|
48 | :order => 1, :sym => TYPE_RELATES }, | |
|
49 | TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, | |
|
50 | :order => 2, :sym => TYPE_DUPLICATED }, | |
|
51 | TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, | |
|
52 | :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, | |
|
53 | TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, | |
|
54 | :order => 4, :sym => TYPE_BLOCKED }, | |
|
55 | TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, | |
|
56 | :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS }, | |
|
57 | TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, | |
|
58 | :order => 6, :sym => TYPE_FOLLOWS }, | |
|
59 | TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, | |
|
60 | :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }, | |
|
61 | TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from, | |
|
62 | :order => 8, :sym => TYPE_COPIED_FROM }, | |
|
63 | TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to, | |
|
64 | :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO } | |
|
65 | }.freeze | |
|
56 | 66 | |
|
57 | 67 | validates_presence_of :issue_from, :issue_to, :relation_type |
|
58 | 68 | validates_inclusion_of :relation_type, :in => TYPES.keys |
|
59 | 69 | validates_numericality_of :delay, :allow_nil => true |
|
60 | 70 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id |
|
61 | ||
|
62 | 71 | validate :validate_issue_relation |
|
63 | 72 | |
|
64 | 73 | attr_protected :issue_from_id, :issue_to_id |
|
65 | ||
|
66 | 74 | before_save :handle_issue_order |
|
67 | 75 | |
|
68 | 76 | def visible?(user=User.current) |
|
69 | 77 | (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user)) |
|
70 | 78 | end |
|
71 | 79 | |
|
72 | 80 | def deletable?(user=User.current) |
|
73 | 81 | visible?(user) && |
|
74 | 82 | ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) || |
|
75 | 83 | (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project))) |
|
76 | 84 | end |
|
77 | 85 | |
|
78 | 86 | def initialize(attributes=nil, *args) |
|
79 | 87 | super |
|
80 | 88 | if new_record? |
|
81 | 89 | if relation_type.blank? |
|
82 | 90 | self.relation_type = IssueRelation::TYPE_RELATES |
|
83 | 91 | end |
|
84 | 92 | end |
|
85 | 93 | end |
|
86 | 94 | |
|
87 | 95 | def validate_issue_relation |
|
88 | 96 | if issue_from && issue_to |
|
89 | 97 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id |
|
90 | errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations? | |
|
91 | #detect circular dependencies depending wether the relation should be reversed | |
|
98 | unless issue_from.project_id == issue_to.project_id || | |
|
99 | Setting.cross_project_issue_relations? | |
|
100 | errors.add :issue_to_id, :not_same_project | |
|
101 | end | |
|
102 | # detect circular dependencies depending wether the relation should be reversed | |
|
92 | 103 | if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] |
|
93 | 104 | errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to |
|
94 | 105 | else |
|
95 | 106 | errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from |
|
96 | 107 | end |
|
97 |
|
|
|
108 | if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to) | |
|
109 | errors.add :base, :cant_link_an_issue_with_a_descendant | |
|
110 | end | |
|
98 | 111 | end |
|
99 | 112 | end |
|
100 | 113 | |
|
101 | 114 | def other_issue(issue) |
|
102 | 115 | (self.issue_from_id == issue.id) ? issue_to : issue_from |
|
103 | 116 | end |
|
104 | 117 | |
|
105 | 118 | # Returns the relation type for +issue+ |
|
106 | 119 | def relation_type_for(issue) |
|
107 | 120 | if TYPES[relation_type] |
|
108 | 121 | if self.issue_from_id == issue.id |
|
109 | 122 | relation_type |
|
110 | 123 | else |
|
111 | 124 | TYPES[relation_type][:sym] |
|
112 | 125 | end |
|
113 | 126 | end |
|
114 | 127 | end |
|
115 | 128 | |
|
116 | 129 | def label_for(issue) |
|
117 | TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow | |
|
130 | TYPES[relation_type] ? | |
|
131 | TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : | |
|
132 | :unknow | |
|
118 | 133 | end |
|
119 | 134 | |
|
120 | 135 | def css_classes_for(issue) |
|
121 | 136 | "rel-#{relation_type_for(issue)}" |
|
122 | 137 | end |
|
123 | 138 | |
|
124 | 139 | def handle_issue_order |
|
125 | 140 | reverse_if_needed |
|
126 | 141 | |
|
127 | 142 | if TYPE_PRECEDES == relation_type |
|
128 | 143 | self.delay ||= 0 |
|
129 | 144 | else |
|
130 | 145 | self.delay = nil |
|
131 | 146 | end |
|
132 | 147 | set_issue_to_dates |
|
133 | 148 | end |
|
134 | 149 | |
|
135 | 150 | def set_issue_to_dates |
|
136 | 151 | soonest_start = self.successor_soonest_start |
|
137 | 152 | if soonest_start && issue_to |
|
138 | 153 | issue_to.reschedule_on!(soonest_start) |
|
139 | 154 | end |
|
140 | 155 | end |
|
141 | 156 | |
|
142 | 157 | def successor_soonest_start |
|
143 |
if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && |
|
|
158 | if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && | |
|
159 | (issue_from.start_date || issue_from.due_date) | |
|
144 | 160 | (issue_from.due_date || issue_from.start_date) + 1 + delay |
|
145 | 161 | end |
|
146 | 162 | end |
|
147 | 163 | |
|
148 | 164 | def <=>(relation) |
|
149 | 165 | r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] |
|
150 | 166 | r == 0 ? id <=> relation.id : r |
|
151 | 167 | end |
|
152 | 168 | |
|
153 | 169 | private |
|
154 | 170 | |
|
155 | 171 | # Reverses the relation if needed so that it gets stored in the proper way |
|
156 | 172 | # Should not be reversed before validation so that it can be displayed back |
|
157 | 173 | # as entered on new relation form |
|
158 | 174 | def reverse_if_needed |
|
159 | 175 | if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] |
|
160 | 176 | issue_tmp = issue_to |
|
161 | 177 | self.issue_to = issue_from |
|
162 | 178 | self.issue_from = issue_tmp |
|
163 | 179 | self.relation_type = TYPES[relation_type][:reverse] |
|
164 | 180 | end |
|
165 | 181 | end |
|
166 | 182 | end |
General Comments 0
You need to be logged in to leave comments.
Login now