##// END OF EJS Templates
remove trailing white space from app/models/issue_relation.rb...
Toshi MARUYAMA -
r15691:3f0e720f56e0
parent child
Show More
@@ -1,248 +1,248
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 # Class used to represent the relations of an issue
20 20 class Relations < Array
21 21 include Redmine::I18n
22 22
23 23 def initialize(issue, *args)
24 24 @issue = issue
25 25 super(*args)
26 26 end
27 27
28 28 def to_s(*args)
29 29 map {|relation| relation.to_s(@issue)}.join(', ')
30 30 end
31 31 end
32 32
33 33 include Redmine::SafeAttributes
34 34
35 35 belongs_to :issue_from, :class_name => 'Issue'
36 36 belongs_to :issue_to, :class_name => 'Issue'
37 37
38 38 TYPE_RELATES = "relates"
39 39 TYPE_DUPLICATES = "duplicates"
40 40 TYPE_DUPLICATED = "duplicated"
41 41 TYPE_BLOCKS = "blocks"
42 42 TYPE_BLOCKED = "blocked"
43 43 TYPE_PRECEDES = "precedes"
44 44 TYPE_FOLLOWS = "follows"
45 45 TYPE_COPIED_TO = "copied_to"
46 46 TYPE_COPIED_FROM = "copied_from"
47 47
48 48 TYPES = {
49 49 TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to,
50 50 :order => 1, :sym => TYPE_RELATES },
51 51 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by,
52 52 :order => 2, :sym => TYPE_DUPLICATED },
53 53 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates,
54 54 :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
55 55 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by,
56 56 :order => 4, :sym => TYPE_BLOCKED },
57 57 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks,
58 58 :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
59 59 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows,
60 60 :order => 6, :sym => TYPE_FOLLOWS },
61 61 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes,
62 62 :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES },
63 63 TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from,
64 64 :order => 8, :sym => TYPE_COPIED_FROM },
65 65 TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to,
66 66 :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO }
67 67 }.freeze
68 68
69 69 validates_presence_of :issue_from, :issue_to, :relation_type
70 70 validates_inclusion_of :relation_type, :in => TYPES.keys
71 71 validates_numericality_of :delay, :allow_nil => true
72 72 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
73 73 validate :validate_issue_relation
74 74
75 75 attr_protected :issue_from_id, :issue_to_id
76 76 before_save :handle_issue_order
77 77 after_create :call_issues_relation_added_callback
78 78 after_destroy :call_issues_relation_removed_callback
79 79
80 80 safe_attributes 'relation_type',
81 81 'delay',
82 82 'issue_to_id'
83 83
84 84 def safe_attributes=(attrs, user=User.current)
85 85 return unless attrs.is_a?(Hash)
86 86 attrs = attrs.deep_dup
87 87
88 88 if issue_id = attrs.delete('issue_to_id')
89 89 if issue_id.to_s.strip.match(/\A#?(\d+)\z/)
90 90 issue_id = $1.to_i
91 91 self.issue_to = Issue.visible(user).find_by_id(issue_id)
92 92 end
93 93 end
94
94
95 95 super(attrs)
96 96 end
97 97
98 98 def visible?(user=User.current)
99 99 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
100 100 end
101 101
102 102 def deletable?(user=User.current)
103 103 visible?(user) &&
104 104 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
105 105 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
106 106 end
107 107
108 108 def initialize(attributes=nil, *args)
109 109 super
110 110 if new_record?
111 111 if relation_type.blank?
112 112 self.relation_type = IssueRelation::TYPE_RELATES
113 113 end
114 114 end
115 115 end
116 116
117 117 def validate_issue_relation
118 118 if issue_from && issue_to
119 119 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
120 120 unless issue_from.project_id == issue_to.project_id ||
121 121 Setting.cross_project_issue_relations?
122 122 errors.add :issue_to_id, :not_same_project
123 123 end
124 124 if circular_dependency?
125 125 errors.add :base, :circular_dependency
126 126 end
127 127 if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
128 128 errors.add :base, :cant_link_an_issue_with_a_descendant
129 129 end
130 130 end
131 131 end
132 132
133 133 def other_issue(issue)
134 134 (self.issue_from_id == issue.id) ? issue_to : issue_from
135 135 end
136 136
137 137 # Returns the relation type for +issue+
138 138 def relation_type_for(issue)
139 139 if TYPES[relation_type]
140 140 if self.issue_from_id == issue.id
141 141 relation_type
142 142 else
143 143 TYPES[relation_type][:sym]
144 144 end
145 145 end
146 146 end
147 147
148 148 def label_for(issue)
149 149 TYPES[relation_type] ?
150 150 TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
151 151 :unknow
152 152 end
153 153
154 154 def to_s(issue=nil)
155 155 issue ||= issue_from
156 156 issue_text = block_given? ? yield(other_issue(issue)) : "##{other_issue(issue).try(:id)}"
157 157 s = []
158 158 s << l(label_for(issue))
159 159 s << "(#{l('datetime.distance_in_words.x_days', :count => delay)})" if delay && delay != 0
160 160 s << issue_text
161 161 s.join(' ')
162 162 end
163 163
164 164 def css_classes_for(issue)
165 165 "rel-#{relation_type_for(issue)}"
166 166 end
167 167
168 168 def handle_issue_order
169 169 reverse_if_needed
170 170
171 171 if TYPE_PRECEDES == relation_type
172 172 self.delay ||= 0
173 173 else
174 174 self.delay = nil
175 175 end
176 176 set_issue_to_dates
177 177 end
178 178
179 179 def set_issue_to_dates
180 180 soonest_start = self.successor_soonest_start
181 181 if soonest_start && issue_to
182 182 issue_to.reschedule_on!(soonest_start)
183 183 end
184 184 end
185 185
186 186 def successor_soonest_start
187 187 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
188 188 (issue_from.start_date || issue_from.due_date)
189 189 (issue_from.due_date || issue_from.start_date) + 1 + delay
190 190 end
191 191 end
192 192
193 193 def <=>(relation)
194 194 r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
195 195 r == 0 ? id <=> relation.id : r
196 196 end
197 197
198 198 def init_journals(user)
199 199 issue_from.init_journal(user) if issue_from
200 200 issue_to.init_journal(user) if issue_to
201 201 end
202 202
203 203 private
204 204
205 205 # Reverses the relation if needed so that it gets stored in the proper way
206 206 # Should not be reversed before validation so that it can be displayed back
207 207 # as entered on new relation form
208 208 def reverse_if_needed
209 209 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
210 210 issue_tmp = issue_to
211 211 self.issue_to = issue_from
212 212 self.issue_from = issue_tmp
213 213 self.relation_type = TYPES[relation_type][:reverse]
214 214 end
215 215 end
216 216
217 217 # Returns true if the relation would create a circular dependency
218 218 def circular_dependency?
219 219 case relation_type
220 220 when 'follows'
221 221 issue_from.would_reschedule? issue_to
222 222 when 'precedes'
223 223 issue_to.would_reschedule? issue_from
224 224 when 'blocked'
225 225 issue_from.blocks? issue_to
226 226 when 'blocks'
227 227 issue_to.blocks? issue_from
228 228 else
229 229 false
230 230 end
231 231 end
232 232
233 233 def call_issues_relation_added_callback
234 234 call_issues_callback :relation_added
235 235 end
236 236
237 237 def call_issues_relation_removed_callback
238 238 call_issues_callback :relation_removed
239 239 end
240 240
241 241 def call_issues_callback(name)
242 242 [issue_from, issue_to].each do |issue|
243 243 if issue
244 244 issue.send name, self
245 245 end
246 246 end
247 247 end
248 248 end
General Comments 0
You need to be logged in to leave comments. Login now