@@ -1,1593 +1,1593 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 Issue < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | include Redmine::Utils::DateCalculation |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | |
|
23 | 23 | belongs_to :project |
|
24 | 24 | belongs_to :tracker |
|
25 | 25 | belongs_to :status, :class_name => 'IssueStatus' |
|
26 | 26 | belongs_to :author, :class_name => 'User' |
|
27 | 27 | belongs_to :assigned_to, :class_name => 'Principal' |
|
28 | 28 | belongs_to :fixed_version, :class_name => 'Version' |
|
29 | 29 | belongs_to :priority, :class_name => 'IssuePriority' |
|
30 | 30 | belongs_to :category, :class_name => 'IssueCategory' |
|
31 | 31 | |
|
32 | 32 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
33 | 33 | has_many :visible_journals, |
|
34 | 34 | lambda {where(["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false])}, |
|
35 | 35 | :class_name => 'Journal', |
|
36 | 36 | :as => :journalized |
|
37 | 37 | |
|
38 | 38 | has_many :time_entries, :dependent => :destroy |
|
39 | 39 | has_and_belongs_to_many :changesets, lambda {order("#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC")} |
|
40 | 40 | |
|
41 | 41 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
42 | 42 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
43 | 43 | |
|
44 | 44 | acts_as_nested_set :scope => 'root_id', :dependent => :destroy |
|
45 | 45 | acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed |
|
46 | 46 | acts_as_customizable |
|
47 | 47 | acts_as_watchable |
|
48 | 48 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
49 | 49 | # sort by id so that limited eager loading doesn't break with postgresql |
|
50 | 50 | :order_column => "#{table_name}.id", |
|
51 | 51 | :scope => lambda { joins(:project). |
|
52 | 52 | joins("LEFT OUTER JOIN #{Journal.table_name} ON #{Journal.table_name}.journalized_type='Issue'" + |
|
53 | 53 | " AND #{Journal.table_name}.journalized_id = #{Issue.table_name}.id" + |
|
54 | 54 | " AND (#{Journal.table_name}.private_notes = #{connection.quoted_false}" + |
|
55 | 55 | " OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))") } |
|
56 | 56 | |
|
57 | 57 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, |
|
58 | 58 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, |
|
59 | 59 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } |
|
60 | 60 | |
|
61 | 61 | acts_as_activity_provider :scope => preload(:project, :author, :tracker), |
|
62 | 62 | :author_key => :author_id |
|
63 | 63 | |
|
64 | 64 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) |
|
65 | 65 | |
|
66 | 66 | attr_reader :current_journal |
|
67 | 67 | delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true |
|
68 | 68 | |
|
69 | 69 | validates_presence_of :subject, :priority, :project, :tracker, :author, :status |
|
70 | 70 | |
|
71 | 71 | validates_length_of :subject, :maximum => 255 |
|
72 | 72 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
73 | 73 | validates :estimated_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid} |
|
74 | 74 | validates :start_date, :date => true |
|
75 | 75 | validates :due_date, :date => true |
|
76 | 76 | validate :validate_issue, :validate_required_fields |
|
77 | 77 | attr_protected :id |
|
78 | 78 | |
|
79 | 79 | scope :visible, lambda {|*args| |
|
80 | 80 | includes(:project). |
|
81 | 81 | references(:project). |
|
82 | 82 | where(Issue.visible_condition(args.shift || User.current, *args)) |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | scope :open, lambda {|*args| |
|
86 | 86 | is_closed = args.size > 0 ? !args.first : false |
|
87 | 87 | joins(:status). |
|
88 | 88 | where("#{IssueStatus.table_name}.is_closed = ?", is_closed) |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | scope :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") } |
|
92 | 92 | scope :on_active_project, lambda { |
|
93 | 93 | joins(:project). |
|
94 | 94 | where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE) |
|
95 | 95 | } |
|
96 | 96 | scope :fixed_version, lambda {|versions| |
|
97 | 97 | ids = [versions].flatten.compact.map {|v| v.is_a?(Version) ? v.id : v} |
|
98 | 98 | ids.any? ? where(:fixed_version_id => ids) : where('1=0') |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | before_create :default_assign |
|
102 | 102 | before_save :close_duplicates, :update_done_ratio_from_issue_status, |
|
103 | 103 | :force_updated_on_change, :update_closed_on, :set_assigned_to_was |
|
104 | 104 | after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} |
|
105 | 105 | after_save :reschedule_following_issues, :update_nested_set_attributes, |
|
106 | 106 | :update_parent_attributes, :create_journal |
|
107 | 107 | # Should be after_create but would be called before previous after_save callbacks |
|
108 | 108 | after_save :after_create_from_copy |
|
109 | 109 | after_destroy :update_parent_attributes |
|
110 | 110 | after_create :send_notification |
|
111 | 111 | # Keep it at the end of after_save callbacks |
|
112 | 112 | after_save :clear_assigned_to_was |
|
113 | 113 | |
|
114 | 114 | # Returns a SQL conditions string used to find all issues visible by the specified user |
|
115 | 115 | def self.visible_condition(user, options={}) |
|
116 | 116 | Project.allowed_to_condition(user, :view_issues, options) do |role, user| |
|
117 | 117 | if user.id && user.logged? |
|
118 | 118 | case role.issues_visibility |
|
119 | 119 | when 'all' |
|
120 | 120 | nil |
|
121 | 121 | when 'default' |
|
122 | 122 | user_ids = [user.id] + user.groups.map(&:id).compact |
|
123 | 123 | "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
124 | 124 | when 'own' |
|
125 | 125 | user_ids = [user.id] + user.groups.map(&:id).compact |
|
126 | 126 | "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
127 | 127 | else |
|
128 | 128 | '1=0' |
|
129 | 129 | end |
|
130 | 130 | else |
|
131 | 131 | "(#{table_name}.is_private = #{connection.quoted_false})" |
|
132 | 132 | end |
|
133 | 133 | end |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | # Returns true if usr or current user is allowed to view the issue |
|
137 | 137 | def visible?(usr=nil) |
|
138 | 138 | (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| |
|
139 | 139 | if user.logged? |
|
140 | 140 | case role.issues_visibility |
|
141 | 141 | when 'all' |
|
142 | 142 | true |
|
143 | 143 | when 'default' |
|
144 | 144 | !self.is_private? || (self.author == user || user.is_or_belongs_to?(assigned_to)) |
|
145 | 145 | when 'own' |
|
146 | 146 | self.author == user || user.is_or_belongs_to?(assigned_to) |
|
147 | 147 | else |
|
148 | 148 | false |
|
149 | 149 | end |
|
150 | 150 | else |
|
151 | 151 | !self.is_private? |
|
152 | 152 | end |
|
153 | 153 | end |
|
154 | 154 | end |
|
155 | 155 | |
|
156 | 156 | # Returns true if user or current user is allowed to edit or add a note to the issue |
|
157 | 157 | def editable?(user=User.current) |
|
158 | 158 | user.allowed_to?(:edit_issues, project) || user.allowed_to?(:add_issue_notes, project) |
|
159 | 159 | end |
|
160 | 160 | |
|
161 | 161 | def initialize(attributes=nil, *args) |
|
162 | 162 | super |
|
163 | 163 | if new_record? |
|
164 | 164 | # set default values for new records only |
|
165 | 165 | self.status ||= IssueStatus.default |
|
166 | 166 | self.priority ||= IssuePriority.default |
|
167 | 167 | self.watcher_user_ids = [] |
|
168 | 168 | end |
|
169 | 169 | end |
|
170 | 170 | |
|
171 | 171 | def create_or_update |
|
172 | 172 | super |
|
173 | 173 | ensure |
|
174 | 174 | @status_was = nil |
|
175 | 175 | end |
|
176 | 176 | private :create_or_update |
|
177 | 177 | |
|
178 | 178 | # AR#Persistence#destroy would raise and RecordNotFound exception |
|
179 | 179 | # if the issue was already deleted or updated (non matching lock_version). |
|
180 | 180 | # This is a problem when bulk deleting issues or deleting a project |
|
181 | 181 | # (because an issue may already be deleted if its parent was deleted |
|
182 | 182 | # first). |
|
183 | 183 | # The issue is reloaded by the nested_set before being deleted so |
|
184 | 184 | # the lock_version condition should not be an issue but we handle it. |
|
185 | 185 | def destroy |
|
186 | 186 | super |
|
187 | 187 | rescue ActiveRecord::RecordNotFound |
|
188 | 188 | # Stale or already deleted |
|
189 | 189 | begin |
|
190 | 190 | reload |
|
191 | 191 | rescue ActiveRecord::RecordNotFound |
|
192 | 192 | # The issue was actually already deleted |
|
193 | 193 | @destroyed = true |
|
194 | 194 | return freeze |
|
195 | 195 | end |
|
196 | 196 | # The issue was stale, retry to destroy |
|
197 | 197 | super |
|
198 | 198 | end |
|
199 | 199 | |
|
200 | 200 | alias :base_reload :reload |
|
201 | 201 | def reload(*args) |
|
202 | 202 | @workflow_rule_by_attribute = nil |
|
203 | 203 | @assignable_versions = nil |
|
204 | 204 | @relations = nil |
|
205 | 205 | base_reload(*args) |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
209 | 209 | def available_custom_fields |
|
210 | 210 | (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields) : [] |
|
211 | 211 | end |
|
212 | 212 | |
|
213 | 213 | def visible_custom_field_values(user=nil) |
|
214 | 214 | user_real = user || User.current |
|
215 | 215 | custom_field_values.select do |value| |
|
216 | 216 | value.custom_field.visible_by?(project, user_real) |
|
217 | 217 | end |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | # Copies attributes from another issue, arg can be an id or an Issue |
|
221 | 221 | def copy_from(arg, options={}) |
|
222 | 222 | issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) |
|
223 | 223 | self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on") |
|
224 | 224 | self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
225 | 225 | self.status = issue.status |
|
226 | 226 | self.author = User.current |
|
227 | 227 | unless options[:attachments] == false |
|
228 | 228 | self.attachments = issue.attachments.map do |attachement| |
|
229 | 229 | attachement.copy(:container => self) |
|
230 | 230 | end |
|
231 | 231 | end |
|
232 | 232 | @copied_from = issue |
|
233 | 233 | @copy_options = options |
|
234 | 234 | self |
|
235 | 235 | end |
|
236 | 236 | |
|
237 | 237 | # Returns an unsaved copy of the issue |
|
238 | 238 | def copy(attributes=nil, copy_options={}) |
|
239 | 239 | copy = self.class.new.copy_from(self, copy_options) |
|
240 | 240 | copy.attributes = attributes if attributes |
|
241 | 241 | copy |
|
242 | 242 | end |
|
243 | 243 | |
|
244 | 244 | # Returns true if the issue is a copy |
|
245 | 245 | def copy? |
|
246 | 246 | @copied_from.present? |
|
247 | 247 | end |
|
248 | 248 | |
|
249 | 249 | # Moves/copies an issue to a new project and tracker |
|
250 | 250 | # Returns the moved/copied issue on success, false on failure |
|
251 | 251 | def move_to_project(new_project, new_tracker=nil, options={}) |
|
252 | 252 | ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead." |
|
253 | 253 | |
|
254 | 254 | if options[:copy] |
|
255 | 255 | issue = self.copy |
|
256 | 256 | else |
|
257 | 257 | issue = self |
|
258 | 258 | end |
|
259 | 259 | |
|
260 | 260 | issue.init_journal(User.current, options[:notes]) |
|
261 | 261 | |
|
262 | 262 | # Preserve previous behaviour |
|
263 | 263 | # #move_to_project doesn't change tracker automatically |
|
264 | 264 | issue.send :project=, new_project, true |
|
265 | 265 | if new_tracker |
|
266 | 266 | issue.tracker = new_tracker |
|
267 | 267 | end |
|
268 | 268 | # Allow bulk setting of attributes on the issue |
|
269 | 269 | if options[:attributes] |
|
270 | 270 | issue.attributes = options[:attributes] |
|
271 | 271 | end |
|
272 | 272 | |
|
273 | 273 | issue.save ? issue : false |
|
274 | 274 | end |
|
275 | 275 | |
|
276 | 276 | def status_id=(sid) |
|
277 | 277 | self.status = nil |
|
278 | 278 | result = write_attribute(:status_id, sid) |
|
279 | 279 | @workflow_rule_by_attribute = nil |
|
280 | 280 | result |
|
281 | 281 | end |
|
282 | 282 | |
|
283 | 283 | def priority_id=(pid) |
|
284 | 284 | self.priority = nil |
|
285 | 285 | write_attribute(:priority_id, pid) |
|
286 | 286 | end |
|
287 | 287 | |
|
288 | 288 | def category_id=(cid) |
|
289 | 289 | self.category = nil |
|
290 | 290 | write_attribute(:category_id, cid) |
|
291 | 291 | end |
|
292 | 292 | |
|
293 | 293 | def fixed_version_id=(vid) |
|
294 | 294 | self.fixed_version = nil |
|
295 | 295 | write_attribute(:fixed_version_id, vid) |
|
296 | 296 | end |
|
297 | 297 | |
|
298 | 298 | def tracker_id=(tid) |
|
299 | 299 | self.tracker = nil |
|
300 | 300 | result = write_attribute(:tracker_id, tid) |
|
301 | 301 | @custom_field_values = nil |
|
302 | 302 | @workflow_rule_by_attribute = nil |
|
303 | 303 | result |
|
304 | 304 | end |
|
305 | 305 | |
|
306 | 306 | def project_id=(project_id) |
|
307 | 307 | if project_id.to_s != self.project_id.to_s |
|
308 | 308 | self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) |
|
309 | 309 | end |
|
310 | 310 | end |
|
311 | 311 | |
|
312 | 312 | def project=(project, keep_tracker=false) |
|
313 | 313 | project_was = self.project |
|
314 | 314 | write_attribute(:project_id, project ? project.id : nil) |
|
315 | 315 | association_instance_set('project', project) |
|
316 | 316 | if project_was && project && project_was != project |
|
317 | 317 | @assignable_versions = nil |
|
318 | 318 | |
|
319 | 319 | unless keep_tracker || project.trackers.include?(tracker) |
|
320 | 320 | self.tracker = project.trackers.first |
|
321 | 321 | end |
|
322 | 322 | # Reassign to the category with same name if any |
|
323 | 323 | if category |
|
324 | 324 | self.category = project.issue_categories.find_by_name(category.name) |
|
325 | 325 | end |
|
326 | 326 | # Keep the fixed_version if it's still valid in the new_project |
|
327 | 327 | if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) |
|
328 | 328 | self.fixed_version = nil |
|
329 | 329 | end |
|
330 | 330 | # Clear the parent task if it's no longer valid |
|
331 | 331 | unless valid_parent_project? |
|
332 | 332 | self.parent_issue_id = nil |
|
333 | 333 | end |
|
334 | 334 | @custom_field_values = nil |
|
335 | 335 | end |
|
336 | 336 | end |
|
337 | 337 | |
|
338 | 338 | def description=(arg) |
|
339 | 339 | if arg.is_a?(String) |
|
340 | 340 | arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") |
|
341 | 341 | end |
|
342 | 342 | write_attribute(:description, arg) |
|
343 | 343 | end |
|
344 | 344 | |
|
345 | 345 | # Overrides assign_attributes so that project and tracker get assigned first |
|
346 | 346 | def assign_attributes_with_project_and_tracker_first(new_attributes, *args) |
|
347 | 347 | return if new_attributes.nil? |
|
348 | 348 | attrs = new_attributes.dup |
|
349 | 349 | attrs.stringify_keys! |
|
350 | 350 | |
|
351 | 351 | %w(project project_id tracker tracker_id).each do |attr| |
|
352 | 352 | if attrs.has_key?(attr) |
|
353 | 353 | send "#{attr}=", attrs.delete(attr) |
|
354 | 354 | end |
|
355 | 355 | end |
|
356 | 356 | send :assign_attributes_without_project_and_tracker_first, attrs, *args |
|
357 | 357 | end |
|
358 | 358 | # Do not redefine alias chain on reload (see #4838) |
|
359 | 359 | alias_method_chain(:assign_attributes, :project_and_tracker_first) unless method_defined?(:assign_attributes_without_project_and_tracker_first) |
|
360 | 360 | |
|
361 | 361 | def attributes=(new_attributes) |
|
362 | 362 | assign_attributes new_attributes |
|
363 | 363 | end |
|
364 | 364 | |
|
365 | 365 | def estimated_hours=(h) |
|
366 | 366 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
367 | 367 | end |
|
368 | 368 | |
|
369 | 369 | safe_attributes 'project_id', |
|
370 | 370 | :if => lambda {|issue, user| |
|
371 | 371 | if issue.new_record? |
|
372 | 372 | issue.copy? |
|
373 | 373 | elsif user.allowed_to?(:move_issues, issue.project) |
|
374 | 374 | Issue.allowed_target_projects_on_move.count > 1 |
|
375 | 375 | end |
|
376 | 376 | } |
|
377 | 377 | |
|
378 | 378 | safe_attributes 'tracker_id', |
|
379 | 379 | 'status_id', |
|
380 | 380 | 'category_id', |
|
381 | 381 | 'assigned_to_id', |
|
382 | 382 | 'priority_id', |
|
383 | 383 | 'fixed_version_id', |
|
384 | 384 | 'subject', |
|
385 | 385 | 'description', |
|
386 | 386 | 'start_date', |
|
387 | 387 | 'due_date', |
|
388 | 388 | 'done_ratio', |
|
389 | 389 | 'estimated_hours', |
|
390 | 390 | 'custom_field_values', |
|
391 | 391 | 'custom_fields', |
|
392 | 392 | 'lock_version', |
|
393 | 393 | 'notes', |
|
394 | 394 | :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) } |
|
395 | 395 | |
|
396 | 396 | safe_attributes 'status_id', |
|
397 | 397 | 'assigned_to_id', |
|
398 | 398 | 'fixed_version_id', |
|
399 | 399 | 'done_ratio', |
|
400 | 400 | 'lock_version', |
|
401 | 401 | 'notes', |
|
402 | 402 | :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? } |
|
403 | 403 | |
|
404 | 404 | safe_attributes 'notes', |
|
405 | 405 | :if => lambda {|issue, user| user.allowed_to?(:add_issue_notes, issue.project)} |
|
406 | 406 | |
|
407 | 407 | safe_attributes 'private_notes', |
|
408 | 408 | :if => lambda {|issue, user| !issue.new_record? && user.allowed_to?(:set_notes_private, issue.project)} |
|
409 | 409 | |
|
410 | 410 | safe_attributes 'watcher_user_ids', |
|
411 | 411 | :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)} |
|
412 | 412 | |
|
413 | 413 | safe_attributes 'is_private', |
|
414 | 414 | :if => lambda {|issue, user| |
|
415 | 415 | user.allowed_to?(:set_issues_private, issue.project) || |
|
416 | 416 | (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project)) |
|
417 | 417 | } |
|
418 | 418 | |
|
419 | 419 | safe_attributes 'parent_issue_id', |
|
420 | 420 | :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) && |
|
421 | 421 | user.allowed_to?(:manage_subtasks, issue.project)} |
|
422 | 422 | |
|
423 | 423 | def safe_attribute_names(user=nil) |
|
424 | 424 | names = super |
|
425 | 425 | names -= disabled_core_fields |
|
426 | 426 | names -= read_only_attribute_names(user) |
|
427 | 427 | names |
|
428 | 428 | end |
|
429 | 429 | |
|
430 | 430 | # Safely sets attributes |
|
431 | 431 | # Should be called from controllers instead of #attributes= |
|
432 | 432 | # attr_accessible is too rough because we still want things like |
|
433 | 433 | # Issue.new(:project => foo) to work |
|
434 | 434 | def safe_attributes=(attrs, user=User.current) |
|
435 | 435 | return unless attrs.is_a?(Hash) |
|
436 | 436 | |
|
437 | 437 | attrs = attrs.deep_dup |
|
438 | 438 | |
|
439 | 439 | # Project and Tracker must be set before since new_statuses_allowed_to depends on it. |
|
440 | 440 | if (p = attrs.delete('project_id')) && safe_attribute?('project_id') |
|
441 | 441 | if allowed_target_projects(user).where(:id => p.to_i).exists? |
|
442 | 442 | self.project_id = p |
|
443 | 443 | end |
|
444 | 444 | end |
|
445 | 445 | |
|
446 | 446 | if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id') |
|
447 | 447 | self.tracker_id = t |
|
448 | 448 | end |
|
449 | 449 | |
|
450 | 450 | if (s = attrs.delete('status_id')) && safe_attribute?('status_id') |
|
451 | 451 | if new_statuses_allowed_to(user).collect(&:id).include?(s.to_i) |
|
452 | 452 | self.status_id = s |
|
453 | 453 | end |
|
454 | 454 | end |
|
455 | 455 | |
|
456 | 456 | attrs = delete_unsafe_attributes(attrs, user) |
|
457 | 457 | return if attrs.empty? |
|
458 | 458 | |
|
459 | 459 | unless leaf? |
|
460 | 460 | attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)} |
|
461 | 461 | end |
|
462 | 462 | |
|
463 | 463 | if attrs['parent_issue_id'].present? |
|
464 | 464 | s = attrs['parent_issue_id'].to_s |
|
465 | 465 | unless (m = s.match(%r{\A#?(\d+)\z})) && (m[1] == parent_id.to_s || Issue.visible(user).exists?(m[1])) |
|
466 | 466 | @invalid_parent_issue_id = attrs.delete('parent_issue_id') |
|
467 | 467 | end |
|
468 | 468 | end |
|
469 | 469 | |
|
470 | 470 | if attrs['custom_field_values'].present? |
|
471 | 471 | editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s} |
|
472 | 472 | attrs['custom_field_values'].select! {|k, v| editable_custom_field_ids.include?(k.to_s)} |
|
473 | 473 | end |
|
474 | 474 | |
|
475 | 475 | if attrs['custom_fields'].present? |
|
476 | 476 | editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s} |
|
477 | 477 | attrs['custom_fields'].select! {|c| editable_custom_field_ids.include?(c['id'].to_s)} |
|
478 | 478 | end |
|
479 | 479 | |
|
480 | 480 | # mass-assignment security bypass |
|
481 | 481 | assign_attributes attrs, :without_protection => true |
|
482 | 482 | end |
|
483 | 483 | |
|
484 | 484 | def disabled_core_fields |
|
485 | 485 | tracker ? tracker.disabled_core_fields : [] |
|
486 | 486 | end |
|
487 | 487 | |
|
488 | 488 | # Returns the custom_field_values that can be edited by the given user |
|
489 | 489 | def editable_custom_field_values(user=nil) |
|
490 | 490 | visible_custom_field_values(user).reject do |value| |
|
491 | 491 | read_only_attribute_names(user).include?(value.custom_field_id.to_s) |
|
492 | 492 | end |
|
493 | 493 | end |
|
494 | 494 | |
|
495 | 495 | # Returns the custom fields that can be edited by the given user |
|
496 | 496 | def editable_custom_fields(user=nil) |
|
497 | 497 | editable_custom_field_values(user).map(&:custom_field).uniq |
|
498 | 498 | end |
|
499 | 499 | |
|
500 | 500 | # Returns the names of attributes that are read-only for user or the current user |
|
501 | 501 | # For users with multiple roles, the read-only fields are the intersection of |
|
502 | 502 | # read-only fields of each role |
|
503 | 503 | # The result is an array of strings where sustom fields are represented with their ids |
|
504 | 504 | # |
|
505 | 505 | # Examples: |
|
506 | 506 | # issue.read_only_attribute_names # => ['due_date', '2'] |
|
507 | 507 | # issue.read_only_attribute_names(user) # => [] |
|
508 | 508 | def read_only_attribute_names(user=nil) |
|
509 | 509 | workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'readonly'}.keys |
|
510 | 510 | end |
|
511 | 511 | |
|
512 | 512 | # Returns the names of required attributes for user or the current user |
|
513 | 513 | # For users with multiple roles, the required fields are the intersection of |
|
514 | 514 | # required fields of each role |
|
515 | 515 | # The result is an array of strings where sustom fields are represented with their ids |
|
516 | 516 | # |
|
517 | 517 | # Examples: |
|
518 | 518 | # issue.required_attribute_names # => ['due_date', '2'] |
|
519 | 519 | # issue.required_attribute_names(user) # => [] |
|
520 | 520 | def required_attribute_names(user=nil) |
|
521 | 521 | workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'required'}.keys |
|
522 | 522 | end |
|
523 | 523 | |
|
524 | 524 | # Returns true if the attribute is required for user |
|
525 | 525 | def required_attribute?(name, user=nil) |
|
526 | 526 | required_attribute_names(user).include?(name.to_s) |
|
527 | 527 | end |
|
528 | 528 | |
|
529 | 529 | # Returns a hash of the workflow rule by attribute for the given user |
|
530 | 530 | # |
|
531 | 531 | # Examples: |
|
532 | 532 | # issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'} |
|
533 | 533 | def workflow_rule_by_attribute(user=nil) |
|
534 | 534 | return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil? |
|
535 | 535 | |
|
536 | 536 | user_real = user || User.current |
|
537 | 537 | roles = user_real.admin ? Role.all : user_real.roles_for_project(project) |
|
538 | 538 | return {} if roles.empty? |
|
539 | 539 | |
|
540 | 540 | result = {} |
|
541 | 541 | workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)) |
|
542 | 542 | if workflow_permissions.any? |
|
543 | 543 | workflow_rules = workflow_permissions.inject({}) do |h, wp| |
|
544 | 544 | h[wp.field_name] ||= [] |
|
545 | 545 | h[wp.field_name] << wp.rule |
|
546 | 546 | h |
|
547 | 547 | end |
|
548 | 548 | workflow_rules.each do |attr, rules| |
|
549 | 549 | next if rules.size < roles.size |
|
550 | 550 | uniq_rules = rules.uniq |
|
551 | 551 | if uniq_rules.size == 1 |
|
552 | 552 | result[attr] = uniq_rules.first |
|
553 | 553 | else |
|
554 | 554 | result[attr] = 'required' |
|
555 | 555 | end |
|
556 | 556 | end |
|
557 | 557 | end |
|
558 | 558 | @workflow_rule_by_attribute = result if user.nil? |
|
559 | 559 | result |
|
560 | 560 | end |
|
561 | 561 | private :workflow_rule_by_attribute |
|
562 | 562 | |
|
563 | 563 | def done_ratio |
|
564 | 564 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
565 | 565 | status.default_done_ratio |
|
566 | 566 | else |
|
567 | 567 | read_attribute(:done_ratio) |
|
568 | 568 | end |
|
569 | 569 | end |
|
570 | 570 | |
|
571 | 571 | def self.use_status_for_done_ratio? |
|
572 | 572 | Setting.issue_done_ratio == 'issue_status' |
|
573 | 573 | end |
|
574 | 574 | |
|
575 | 575 | def self.use_field_for_done_ratio? |
|
576 | 576 | Setting.issue_done_ratio == 'issue_field' |
|
577 | 577 | end |
|
578 | 578 | |
|
579 | 579 | def validate_issue |
|
580 | 580 | if due_date && start_date && (start_date_changed? || due_date_changed?) && due_date < start_date |
|
581 | 581 | errors.add :due_date, :greater_than_start_date |
|
582 | 582 | end |
|
583 | 583 | |
|
584 | 584 | if start_date && start_date_changed? && soonest_start && start_date < soonest_start |
|
585 | 585 | errors.add :start_date, :earlier_than_minimum_start_date, :date => format_date(soonest_start) |
|
586 | 586 | end |
|
587 | 587 | |
|
588 | 588 | if fixed_version |
|
589 | 589 | if !assignable_versions.include?(fixed_version) |
|
590 | 590 | errors.add :fixed_version_id, :inclusion |
|
591 | 591 | elsif reopening? && fixed_version.closed? |
|
592 | 592 | errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version) |
|
593 | 593 | end |
|
594 | 594 | end |
|
595 | 595 | |
|
596 | 596 | # Checks that the issue can not be added/moved to a disabled tracker |
|
597 | 597 | if project && (tracker_id_changed? || project_id_changed?) |
|
598 | 598 | unless project.trackers.include?(tracker) |
|
599 | 599 | errors.add :tracker_id, :inclusion |
|
600 | 600 | end |
|
601 | 601 | end |
|
602 | 602 | |
|
603 | 603 | # Checks parent issue assignment |
|
604 | 604 | if @invalid_parent_issue_id.present? |
|
605 | 605 | errors.add :parent_issue_id, :invalid |
|
606 | 606 | elsif @parent_issue |
|
607 | 607 | if !valid_parent_project?(@parent_issue) |
|
608 | 608 | errors.add :parent_issue_id, :invalid |
|
609 | 609 | elsif (@parent_issue != parent) && (all_dependent_issues.include?(@parent_issue) || @parent_issue.all_dependent_issues.include?(self)) |
|
610 | 610 | errors.add :parent_issue_id, :invalid |
|
611 | 611 | elsif !new_record? |
|
612 | 612 | # moving an existing issue |
|
613 | 613 | if @parent_issue.root_id != root_id |
|
614 | 614 | # we can always move to another tree |
|
615 | 615 | elsif move_possible?(@parent_issue) |
|
616 | 616 | # move accepted inside tree |
|
617 | 617 | else |
|
618 | 618 | errors.add :parent_issue_id, :invalid |
|
619 | 619 | end |
|
620 | 620 | end |
|
621 | 621 | end |
|
622 | 622 | end |
|
623 | 623 | |
|
624 | 624 | # Validates the issue against additional workflow requirements |
|
625 | 625 | def validate_required_fields |
|
626 | 626 | user = new_record? ? author : current_journal.try(:user) |
|
627 | 627 | |
|
628 | 628 | required_attribute_names(user).each do |attribute| |
|
629 | 629 | if attribute =~ /^\d+$/ |
|
630 | 630 | attribute = attribute.to_i |
|
631 | 631 | v = custom_field_values.detect {|v| v.custom_field_id == attribute } |
|
632 | 632 | if v && v.value.blank? |
|
633 | 633 | errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank') |
|
634 | 634 | end |
|
635 | 635 | else |
|
636 | 636 | if respond_to?(attribute) && send(attribute).blank? |
|
637 | 637 | errors.add attribute, :blank |
|
638 | 638 | end |
|
639 | 639 | end |
|
640 | 640 | end |
|
641 | 641 | end |
|
642 | 642 | |
|
643 | 643 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
644 | 644 | # even if the user turns off the setting later |
|
645 | 645 | def update_done_ratio_from_issue_status |
|
646 | 646 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
647 | 647 | self.done_ratio = status.default_done_ratio |
|
648 | 648 | end |
|
649 | 649 | end |
|
650 | 650 | |
|
651 | 651 | def init_journal(user, notes = "") |
|
652 | 652 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
653 | 653 | if new_record? |
|
654 | 654 | @current_journal.notify = false |
|
655 | 655 | else |
|
656 | 656 | @attributes_before_change = attributes.dup |
|
657 | 657 | @custom_values_before_change = {} |
|
658 | 658 | self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
659 | 659 | end |
|
660 | 660 | @current_journal |
|
661 | 661 | end |
|
662 | 662 | |
|
663 | 663 | # Returns the id of the last journal or nil |
|
664 | 664 | def last_journal_id |
|
665 | 665 | if new_record? |
|
666 | 666 | nil |
|
667 | 667 | else |
|
668 | 668 | journals.maximum(:id) |
|
669 | 669 | end |
|
670 | 670 | end |
|
671 | 671 | |
|
672 | 672 | # Returns a scope for journals that have an id greater than journal_id |
|
673 | 673 | def journals_after(journal_id) |
|
674 | 674 | scope = journals.reorder("#{Journal.table_name}.id ASC") |
|
675 | 675 | if journal_id.present? |
|
676 | 676 | scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i) |
|
677 | 677 | end |
|
678 | 678 | scope |
|
679 | 679 | end |
|
680 | 680 | |
|
681 | 681 | # Returns the initial status of the issue |
|
682 | 682 | # Returns nil for a new issue |
|
683 | 683 | def status_was |
|
684 | 684 | if status_id_was && status_id_was.to_i > 0 |
|
685 | 685 | @status_was ||= IssueStatus.find_by_id(status_id_was) |
|
686 | 686 | end |
|
687 | 687 | end |
|
688 | 688 | |
|
689 | 689 | # Return true if the issue is closed, otherwise false |
|
690 | 690 | def closed? |
|
691 | 691 | status.present? && status.is_closed? |
|
692 | 692 | end |
|
693 | 693 | |
|
694 | 694 | # Returns true if the issue was closed when loaded |
|
695 | 695 | def was_closed? |
|
696 | 696 | status_was.present? && status_was.is_closed? |
|
697 | 697 | end |
|
698 | 698 | |
|
699 | 699 | # Return true if the issue is being reopened |
|
700 | 700 | def reopening? |
|
701 | 701 | if new_record? |
|
702 | 702 | false |
|
703 | 703 | else |
|
704 | 704 | status_id_changed? && !closed? && was_closed? |
|
705 | 705 | end |
|
706 | 706 | end |
|
707 | 707 | alias :reopened? :reopening? |
|
708 | 708 | |
|
709 | 709 | # Return true if the issue is being closed |
|
710 | 710 | def closing? |
|
711 | 711 | if new_record? |
|
712 | 712 | closed? |
|
713 | 713 | else |
|
714 | 714 | status_id_changed? && closed? && !was_closed? |
|
715 | 715 | end |
|
716 | 716 | end |
|
717 | 717 | |
|
718 | 718 | # Returns true if the issue is overdue |
|
719 | 719 | def overdue? |
|
720 | 720 | due_date.present? && (due_date < Date.today) && !closed? |
|
721 | 721 | end |
|
722 | 722 | |
|
723 | 723 | # Is the amount of work done less than it should for the due date |
|
724 | 724 | def behind_schedule? |
|
725 | 725 | return false if start_date.nil? || due_date.nil? |
|
726 | 726 | done_date = start_date + ((due_date - start_date + 1) * done_ratio / 100).floor |
|
727 | 727 | return done_date <= Date.today |
|
728 | 728 | end |
|
729 | 729 | |
|
730 | 730 | # Does this issue have children? |
|
731 | 731 | def children? |
|
732 | 732 | !leaf? |
|
733 | 733 | end |
|
734 | 734 | |
|
735 | 735 | # Users the issue can be assigned to |
|
736 | 736 | def assignable_users |
|
737 | users = project.assignable_users | |
|
737 | users = project.assignable_users.to_a | |
|
738 | 738 | users << author if author |
|
739 | 739 | users << assigned_to if assigned_to |
|
740 | 740 | users.uniq.sort |
|
741 | 741 | end |
|
742 | 742 | |
|
743 | 743 | # Versions that the issue can be assigned to |
|
744 | 744 | def assignable_versions |
|
745 | 745 | return @assignable_versions if @assignable_versions |
|
746 | 746 | |
|
747 | 747 | versions = project.shared_versions.open.to_a |
|
748 | 748 | if fixed_version |
|
749 | 749 | if fixed_version_id_changed? |
|
750 | 750 | # nothing to do |
|
751 | 751 | elsif project_id_changed? |
|
752 | 752 | if project.shared_versions.include?(fixed_version) |
|
753 | 753 | versions << fixed_version |
|
754 | 754 | end |
|
755 | 755 | else |
|
756 | 756 | versions << fixed_version |
|
757 | 757 | end |
|
758 | 758 | end |
|
759 | 759 | @assignable_versions = versions.uniq.sort |
|
760 | 760 | end |
|
761 | 761 | |
|
762 | 762 | # Returns true if this issue is blocked by another issue that is still open |
|
763 | 763 | def blocked? |
|
764 | 764 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? |
|
765 | 765 | end |
|
766 | 766 | |
|
767 | 767 | # Returns an array of statuses that user is able to apply |
|
768 | 768 | def new_statuses_allowed_to(user=User.current, include_default=false) |
|
769 | 769 | if new_record? && @copied_from |
|
770 | 770 | [IssueStatus.default, @copied_from.status].compact.uniq.sort |
|
771 | 771 | else |
|
772 | 772 | initial_status = nil |
|
773 | 773 | if new_record? |
|
774 | 774 | initial_status = IssueStatus.default |
|
775 | 775 | elsif status_id_was |
|
776 | 776 | initial_status = IssueStatus.find_by_id(status_id_was) |
|
777 | 777 | end |
|
778 | 778 | initial_status ||= status |
|
779 | 779 | |
|
780 | 780 | initial_assigned_to_id = assigned_to_id_changed? ? assigned_to_id_was : assigned_to_id |
|
781 | 781 | assignee_transitions_allowed = initial_assigned_to_id.present? && |
|
782 | 782 | (user.id == initial_assigned_to_id || user.group_ids.include?(initial_assigned_to_id)) |
|
783 | 783 | |
|
784 | 784 | statuses = initial_status.find_new_statuses_allowed_to( |
|
785 | 785 | user.admin ? Role.all : user.roles_for_project(project), |
|
786 | 786 | tracker, |
|
787 | 787 | author == user, |
|
788 | 788 | assignee_transitions_allowed |
|
789 | 789 | ) |
|
790 | 790 | statuses << initial_status unless statuses.empty? |
|
791 | 791 | statuses << IssueStatus.default if include_default |
|
792 | 792 | statuses = statuses.compact.uniq.sort |
|
793 | 793 | blocked? ? statuses.reject {|s| s.is_closed?} : statuses |
|
794 | 794 | end |
|
795 | 795 | end |
|
796 | 796 | |
|
797 | 797 | # Returns the previous assignee if changed |
|
798 | 798 | def assigned_to_was |
|
799 | 799 | # assigned_to_id_was is reset before after_save callbacks |
|
800 | 800 | user_id = @previous_assigned_to_id || assigned_to_id_was |
|
801 | 801 | if user_id && user_id != assigned_to_id |
|
802 | 802 | @assigned_to_was ||= User.find_by_id(user_id) |
|
803 | 803 | end |
|
804 | 804 | end |
|
805 | 805 | |
|
806 | 806 | # Returns the users that should be notified |
|
807 | 807 | def notified_users |
|
808 | 808 | notified = [] |
|
809 | 809 | # Author and assignee are always notified unless they have been |
|
810 | 810 | # locked or don't want to be notified |
|
811 | 811 | notified << author if author |
|
812 | 812 | if assigned_to |
|
813 | 813 | notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to]) |
|
814 | 814 | end |
|
815 | 815 | if assigned_to_was |
|
816 | 816 | notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was]) |
|
817 | 817 | end |
|
818 | 818 | notified = notified.select {|u| u.active? && u.notify_about?(self)} |
|
819 | 819 | |
|
820 | 820 | notified += project.notified_users |
|
821 | 821 | notified.uniq! |
|
822 | 822 | # Remove users that can not view the issue |
|
823 | 823 | notified.reject! {|user| !visible?(user)} |
|
824 | 824 | notified |
|
825 | 825 | end |
|
826 | 826 | |
|
827 | 827 | # Returns the email addresses that should be notified |
|
828 | 828 | def recipients |
|
829 | 829 | notified_users.collect(&:mail) |
|
830 | 830 | end |
|
831 | 831 | |
|
832 | 832 | def each_notification(users, &block) |
|
833 | 833 | if users.any? |
|
834 | 834 | if custom_field_values.detect {|value| !value.custom_field.visible?} |
|
835 | 835 | users_by_custom_field_visibility = users.group_by do |user| |
|
836 | 836 | visible_custom_field_values(user).map(&:custom_field_id).sort |
|
837 | 837 | end |
|
838 | 838 | users_by_custom_field_visibility.values.each do |users| |
|
839 | 839 | yield(users) |
|
840 | 840 | end |
|
841 | 841 | else |
|
842 | 842 | yield(users) |
|
843 | 843 | end |
|
844 | 844 | end |
|
845 | 845 | end |
|
846 | 846 | |
|
847 | 847 | # Returns the number of hours spent on this issue |
|
848 | 848 | def spent_hours |
|
849 | 849 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
850 | 850 | end |
|
851 | 851 | |
|
852 | 852 | # Returns the total number of hours spent on this issue and its descendants |
|
853 | 853 | # |
|
854 | 854 | # Example: |
|
855 | 855 | # spent_hours => 0.0 |
|
856 | 856 | # spent_hours => 50.2 |
|
857 | 857 | def total_spent_hours |
|
858 | 858 | @total_spent_hours ||= |
|
859 | 859 | self_and_descendants. |
|
860 | 860 | joins("LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"). |
|
861 | 861 | sum("#{TimeEntry.table_name}.hours").to_f || 0.0 |
|
862 | 862 | end |
|
863 | 863 | |
|
864 | 864 | def relations |
|
865 | 865 | @relations ||= IssueRelation::Relations.new(self, (relations_from + relations_to).sort) |
|
866 | 866 | end |
|
867 | 867 | |
|
868 | 868 | # Preloads relations for a collection of issues |
|
869 | 869 | def self.load_relations(issues) |
|
870 | 870 | if issues.any? |
|
871 | 871 | relations = IssueRelation.where("issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)).all |
|
872 | 872 | issues.each do |issue| |
|
873 | 873 | issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} |
|
874 | 874 | end |
|
875 | 875 | end |
|
876 | 876 | end |
|
877 | 877 | |
|
878 | 878 | # Preloads visible spent time for a collection of issues |
|
879 | 879 | def self.load_visible_spent_hours(issues, user=User.current) |
|
880 | 880 | if issues.any? |
|
881 | 881 | hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours) |
|
882 | 882 | issues.each do |issue| |
|
883 | 883 | issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) |
|
884 | 884 | end |
|
885 | 885 | end |
|
886 | 886 | end |
|
887 | 887 | |
|
888 | 888 | # Preloads visible relations for a collection of issues |
|
889 | 889 | def self.load_visible_relations(issues, user=User.current) |
|
890 | 890 | if issues.any? |
|
891 | 891 | issue_ids = issues.map(&:id) |
|
892 | 892 | # Relations with issue_from in given issues and visible issue_to |
|
893 | 893 | relations_from = IssueRelation.joins(:issue_to => :project). |
|
894 | 894 | where(visible_condition(user)).where(:issue_from_id => issue_ids).to_a |
|
895 | 895 | # Relations with issue_to in given issues and visible issue_from |
|
896 | 896 | relations_to = IssueRelation.joins(:issue_from => :project). |
|
897 | 897 | where(visible_condition(user)). |
|
898 | 898 | where(:issue_to_id => issue_ids).to_a |
|
899 | 899 | issues.each do |issue| |
|
900 | 900 | relations = |
|
901 | 901 | relations_from.select {|relation| relation.issue_from_id == issue.id} + |
|
902 | 902 | relations_to.select {|relation| relation.issue_to_id == issue.id} |
|
903 | 903 | |
|
904 | 904 | issue.instance_variable_set "@relations", IssueRelation::Relations.new(issue, relations.sort) |
|
905 | 905 | end |
|
906 | 906 | end |
|
907 | 907 | end |
|
908 | 908 | |
|
909 | 909 | # Finds an issue relation given its id. |
|
910 | 910 | def find_relation(relation_id) |
|
911 | 911 | IssueRelation.where("issue_to_id = ? OR issue_from_id = ?", id, id).find(relation_id) |
|
912 | 912 | end |
|
913 | 913 | |
|
914 | 914 | # Returns all the other issues that depend on the issue |
|
915 | 915 | # The algorithm is a modified breadth first search (bfs) |
|
916 | 916 | def all_dependent_issues(except=[]) |
|
917 | 917 | # The found dependencies |
|
918 | 918 | dependencies = [] |
|
919 | 919 | |
|
920 | 920 | # The visited flag for every node (issue) used by the breadth first search |
|
921 | 921 | eNOT_DISCOVERED = 0 # The issue is "new" to the algorithm, it has not seen it before. |
|
922 | 922 | |
|
923 | 923 | ePROCESS_ALL = 1 # The issue is added to the queue. Process both children and relations of |
|
924 | 924 | # the issue when it is processed. |
|
925 | 925 | |
|
926 | 926 | ePROCESS_RELATIONS_ONLY = 2 # The issue was added to the queue and will be output as dependent issue, |
|
927 | 927 | # but its children will not be added to the queue when it is processed. |
|
928 | 928 | |
|
929 | 929 | eRELATIONS_PROCESSED = 3 # The related issues, the parent issue and the issue itself have been added to |
|
930 | 930 | # the queue, but its children have not been added. |
|
931 | 931 | |
|
932 | 932 | ePROCESS_CHILDREN_ONLY = 4 # The relations and the parent of the issue have been added to the queue, but |
|
933 | 933 | # the children still need to be processed. |
|
934 | 934 | |
|
935 | 935 | eALL_PROCESSED = 5 # The issue and all its children, its parent and its related issues have been |
|
936 | 936 | # added as dependent issues. It needs no further processing. |
|
937 | 937 | |
|
938 | 938 | issue_status = Hash.new(eNOT_DISCOVERED) |
|
939 | 939 | |
|
940 | 940 | # The queue |
|
941 | 941 | queue = [] |
|
942 | 942 | |
|
943 | 943 | # Initialize the bfs, add start node (self) to the queue |
|
944 | 944 | queue << self |
|
945 | 945 | issue_status[self] = ePROCESS_ALL |
|
946 | 946 | |
|
947 | 947 | while (!queue.empty?) do |
|
948 | 948 | current_issue = queue.shift |
|
949 | 949 | current_issue_status = issue_status[current_issue] |
|
950 | 950 | dependencies << current_issue |
|
951 | 951 | |
|
952 | 952 | # Add parent to queue, if not already in it. |
|
953 | 953 | parent = current_issue.parent |
|
954 | 954 | parent_status = issue_status[parent] |
|
955 | 955 | |
|
956 | 956 | if parent && (parent_status == eNOT_DISCOVERED) && !except.include?(parent) |
|
957 | 957 | queue << parent |
|
958 | 958 | issue_status[parent] = ePROCESS_RELATIONS_ONLY |
|
959 | 959 | end |
|
960 | 960 | |
|
961 | 961 | # Add children to queue, but only if they are not already in it and |
|
962 | 962 | # the children of the current node need to be processed. |
|
963 | 963 | if (current_issue_status == ePROCESS_CHILDREN_ONLY || current_issue_status == ePROCESS_ALL) |
|
964 | 964 | current_issue.children.each do |child| |
|
965 | 965 | next if except.include?(child) |
|
966 | 966 | |
|
967 | 967 | if (issue_status[child] == eNOT_DISCOVERED) |
|
968 | 968 | queue << child |
|
969 | 969 | issue_status[child] = ePROCESS_ALL |
|
970 | 970 | elsif (issue_status[child] == eRELATIONS_PROCESSED) |
|
971 | 971 | queue << child |
|
972 | 972 | issue_status[child] = ePROCESS_CHILDREN_ONLY |
|
973 | 973 | elsif (issue_status[child] == ePROCESS_RELATIONS_ONLY) |
|
974 | 974 | queue << child |
|
975 | 975 | issue_status[child] = ePROCESS_ALL |
|
976 | 976 | end |
|
977 | 977 | end |
|
978 | 978 | end |
|
979 | 979 | |
|
980 | 980 | # Add related issues to the queue, if they are not already in it. |
|
981 | 981 | current_issue.relations_from.map(&:issue_to).each do |related_issue| |
|
982 | 982 | next if except.include?(related_issue) |
|
983 | 983 | |
|
984 | 984 | if (issue_status[related_issue] == eNOT_DISCOVERED) |
|
985 | 985 | queue << related_issue |
|
986 | 986 | issue_status[related_issue] = ePROCESS_ALL |
|
987 | 987 | elsif (issue_status[related_issue] == eRELATIONS_PROCESSED) |
|
988 | 988 | queue << related_issue |
|
989 | 989 | issue_status[related_issue] = ePROCESS_CHILDREN_ONLY |
|
990 | 990 | elsif (issue_status[related_issue] == ePROCESS_RELATIONS_ONLY) |
|
991 | 991 | queue << related_issue |
|
992 | 992 | issue_status[related_issue] = ePROCESS_ALL |
|
993 | 993 | end |
|
994 | 994 | end |
|
995 | 995 | |
|
996 | 996 | # Set new status for current issue |
|
997 | 997 | if (current_issue_status == ePROCESS_ALL) || (current_issue_status == ePROCESS_CHILDREN_ONLY) |
|
998 | 998 | issue_status[current_issue] = eALL_PROCESSED |
|
999 | 999 | elsif (current_issue_status == ePROCESS_RELATIONS_ONLY) |
|
1000 | 1000 | issue_status[current_issue] = eRELATIONS_PROCESSED |
|
1001 | 1001 | end |
|
1002 | 1002 | end # while |
|
1003 | 1003 | |
|
1004 | 1004 | # Remove the issues from the "except" parameter from the result array |
|
1005 | 1005 | dependencies -= except |
|
1006 | 1006 | dependencies.delete(self) |
|
1007 | 1007 | |
|
1008 | 1008 | dependencies |
|
1009 | 1009 | end |
|
1010 | 1010 | |
|
1011 | 1011 | # Returns an array of issues that duplicate this one |
|
1012 | 1012 | def duplicates |
|
1013 | 1013 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
1014 | 1014 | end |
|
1015 | 1015 | |
|
1016 | 1016 | # Returns the due date or the target due date if any |
|
1017 | 1017 | # Used on gantt chart |
|
1018 | 1018 | def due_before |
|
1019 | 1019 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
1020 | 1020 | end |
|
1021 | 1021 | |
|
1022 | 1022 | # Returns the time scheduled for this issue. |
|
1023 | 1023 | # |
|
1024 | 1024 | # Example: |
|
1025 | 1025 | # Start Date: 2/26/09, End Date: 3/04/09 |
|
1026 | 1026 | # duration => 6 |
|
1027 | 1027 | def duration |
|
1028 | 1028 | (start_date && due_date) ? due_date - start_date : 0 |
|
1029 | 1029 | end |
|
1030 | 1030 | |
|
1031 | 1031 | # Returns the duration in working days |
|
1032 | 1032 | def working_duration |
|
1033 | 1033 | (start_date && due_date) ? working_days(start_date, due_date) : 0 |
|
1034 | 1034 | end |
|
1035 | 1035 | |
|
1036 | 1036 | def soonest_start(reload=false) |
|
1037 | 1037 | @soonest_start = nil if reload |
|
1038 | 1038 | @soonest_start ||= ( |
|
1039 | 1039 | relations_to(reload).collect{|relation| relation.successor_soonest_start} + |
|
1040 | 1040 | [(@parent_issue || parent).try(:soonest_start)] |
|
1041 | 1041 | ).compact.max |
|
1042 | 1042 | end |
|
1043 | 1043 | |
|
1044 | 1044 | # Sets start_date on the given date or the next working day |
|
1045 | 1045 | # and changes due_date to keep the same working duration. |
|
1046 | 1046 | def reschedule_on(date) |
|
1047 | 1047 | wd = working_duration |
|
1048 | 1048 | date = next_working_date(date) |
|
1049 | 1049 | self.start_date = date |
|
1050 | 1050 | self.due_date = add_working_days(date, wd) |
|
1051 | 1051 | end |
|
1052 | 1052 | |
|
1053 | 1053 | # Reschedules the issue on the given date or the next working day and saves the record. |
|
1054 | 1054 | # If the issue is a parent task, this is done by rescheduling its subtasks. |
|
1055 | 1055 | def reschedule_on!(date) |
|
1056 | 1056 | return if date.nil? |
|
1057 | 1057 | if leaf? |
|
1058 | 1058 | if start_date.nil? || start_date != date |
|
1059 | 1059 | if start_date && start_date > date |
|
1060 | 1060 | # Issue can not be moved earlier than its soonest start date |
|
1061 | 1061 | date = [soonest_start(true), date].compact.max |
|
1062 | 1062 | end |
|
1063 | 1063 | reschedule_on(date) |
|
1064 | 1064 | begin |
|
1065 | 1065 | save |
|
1066 | 1066 | rescue ActiveRecord::StaleObjectError |
|
1067 | 1067 | reload |
|
1068 | 1068 | reschedule_on(date) |
|
1069 | 1069 | save |
|
1070 | 1070 | end |
|
1071 | 1071 | end |
|
1072 | 1072 | else |
|
1073 | 1073 | leaves.each do |leaf| |
|
1074 | 1074 | if leaf.start_date |
|
1075 | 1075 | # Only move subtask if it starts at the same date as the parent |
|
1076 | 1076 | # or if it starts before the given date |
|
1077 | 1077 | if start_date == leaf.start_date || date > leaf.start_date |
|
1078 | 1078 | leaf.reschedule_on!(date) |
|
1079 | 1079 | end |
|
1080 | 1080 | else |
|
1081 | 1081 | leaf.reschedule_on!(date) |
|
1082 | 1082 | end |
|
1083 | 1083 | end |
|
1084 | 1084 | end |
|
1085 | 1085 | end |
|
1086 | 1086 | |
|
1087 | 1087 | def <=>(issue) |
|
1088 | 1088 | if issue.nil? |
|
1089 | 1089 | -1 |
|
1090 | 1090 | elsif root_id != issue.root_id |
|
1091 | 1091 | (root_id || 0) <=> (issue.root_id || 0) |
|
1092 | 1092 | else |
|
1093 | 1093 | (lft || 0) <=> (issue.lft || 0) |
|
1094 | 1094 | end |
|
1095 | 1095 | end |
|
1096 | 1096 | |
|
1097 | 1097 | def to_s |
|
1098 | 1098 | "#{tracker} ##{id}: #{subject}" |
|
1099 | 1099 | end |
|
1100 | 1100 | |
|
1101 | 1101 | # Returns a string of css classes that apply to the issue |
|
1102 | 1102 | def css_classes(user=User.current) |
|
1103 | 1103 | s = "issue tracker-#{tracker_id} status-#{status_id} #{priority.try(:css_classes)}" |
|
1104 | 1104 | s << ' closed' if closed? |
|
1105 | 1105 | s << ' overdue' if overdue? |
|
1106 | 1106 | s << ' child' if child? |
|
1107 | 1107 | s << ' parent' unless leaf? |
|
1108 | 1108 | s << ' private' if is_private? |
|
1109 | 1109 | if user.logged? |
|
1110 | 1110 | s << ' created-by-me' if author_id == user.id |
|
1111 | 1111 | s << ' assigned-to-me' if assigned_to_id == user.id |
|
1112 | 1112 | s << ' assigned-to-my-group' if user.groups.any? {|g| g.id == assigned_to_id} |
|
1113 | 1113 | end |
|
1114 | 1114 | s |
|
1115 | 1115 | end |
|
1116 | 1116 | |
|
1117 | 1117 | # Unassigns issues from +version+ if it's no longer shared with issue's project |
|
1118 | 1118 | def self.update_versions_from_sharing_change(version) |
|
1119 | 1119 | # Update issues assigned to the version |
|
1120 | 1120 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) |
|
1121 | 1121 | end |
|
1122 | 1122 | |
|
1123 | 1123 | # Unassigns issues from versions that are no longer shared |
|
1124 | 1124 | # after +project+ was moved |
|
1125 | 1125 | def self.update_versions_from_hierarchy_change(project) |
|
1126 | 1126 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) |
|
1127 | 1127 | # Update issues of the moved projects and issues assigned to a version of a moved project |
|
1128 | 1128 | Issue.update_versions( |
|
1129 | 1129 | ["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", |
|
1130 | 1130 | moved_project_ids, moved_project_ids] |
|
1131 | 1131 | ) |
|
1132 | 1132 | end |
|
1133 | 1133 | |
|
1134 | 1134 | def parent_issue_id=(arg) |
|
1135 | 1135 | s = arg.to_s.strip.presence |
|
1136 | 1136 | if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1])) |
|
1137 | 1137 | @invalid_parent_issue_id = nil |
|
1138 | 1138 | elsif s.blank? |
|
1139 | 1139 | @parent_issue = nil |
|
1140 | 1140 | @invalid_parent_issue_id = nil |
|
1141 | 1141 | else |
|
1142 | 1142 | @parent_issue = nil |
|
1143 | 1143 | @invalid_parent_issue_id = arg |
|
1144 | 1144 | end |
|
1145 | 1145 | end |
|
1146 | 1146 | |
|
1147 | 1147 | def parent_issue_id |
|
1148 | 1148 | if @invalid_parent_issue_id |
|
1149 | 1149 | @invalid_parent_issue_id |
|
1150 | 1150 | elsif instance_variable_defined? :@parent_issue |
|
1151 | 1151 | @parent_issue.nil? ? nil : @parent_issue.id |
|
1152 | 1152 | else |
|
1153 | 1153 | parent_id |
|
1154 | 1154 | end |
|
1155 | 1155 | end |
|
1156 | 1156 | |
|
1157 | 1157 | # Returns true if issue's project is a valid |
|
1158 | 1158 | # parent issue project |
|
1159 | 1159 | def valid_parent_project?(issue=parent) |
|
1160 | 1160 | return true if issue.nil? || issue.project_id == project_id |
|
1161 | 1161 | |
|
1162 | 1162 | case Setting.cross_project_subtasks |
|
1163 | 1163 | when 'system' |
|
1164 | 1164 | true |
|
1165 | 1165 | when 'tree' |
|
1166 | 1166 | issue.project.root == project.root |
|
1167 | 1167 | when 'hierarchy' |
|
1168 | 1168 | issue.project.is_or_is_ancestor_of?(project) || issue.project.is_descendant_of?(project) |
|
1169 | 1169 | when 'descendants' |
|
1170 | 1170 | issue.project.is_or_is_ancestor_of?(project) |
|
1171 | 1171 | else |
|
1172 | 1172 | false |
|
1173 | 1173 | end |
|
1174 | 1174 | end |
|
1175 | 1175 | |
|
1176 | 1176 | # Returns an issue scope based on project and scope |
|
1177 | 1177 | def self.cross_project_scope(project, scope=nil) |
|
1178 | 1178 | if project.nil? |
|
1179 | 1179 | return Issue |
|
1180 | 1180 | end |
|
1181 | 1181 | case scope |
|
1182 | 1182 | when 'all', 'system' |
|
1183 | 1183 | Issue |
|
1184 | 1184 | when 'tree' |
|
1185 | 1185 | Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)", |
|
1186 | 1186 | :lft => project.root.lft, :rgt => project.root.rgt) |
|
1187 | 1187 | when 'hierarchy' |
|
1188 | 1188 | Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt) OR (#{Project.table_name}.lft < :lft AND #{Project.table_name}.rgt > :rgt)", |
|
1189 | 1189 | :lft => project.lft, :rgt => project.rgt) |
|
1190 | 1190 | when 'descendants' |
|
1191 | 1191 | Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)", |
|
1192 | 1192 | :lft => project.lft, :rgt => project.rgt) |
|
1193 | 1193 | else |
|
1194 | 1194 | Issue.where(:project_id => project.id) |
|
1195 | 1195 | end |
|
1196 | 1196 | end |
|
1197 | 1197 | |
|
1198 | 1198 | def self.by_tracker(project) |
|
1199 | 1199 | count_and_group_by(:project => project, :association => :tracker) |
|
1200 | 1200 | end |
|
1201 | 1201 | |
|
1202 | 1202 | def self.by_version(project) |
|
1203 | 1203 | count_and_group_by(:project => project, :association => :fixed_version) |
|
1204 | 1204 | end |
|
1205 | 1205 | |
|
1206 | 1206 | def self.by_priority(project) |
|
1207 | 1207 | count_and_group_by(:project => project, :association => :priority) |
|
1208 | 1208 | end |
|
1209 | 1209 | |
|
1210 | 1210 | def self.by_category(project) |
|
1211 | 1211 | count_and_group_by(:project => project, :association => :category) |
|
1212 | 1212 | end |
|
1213 | 1213 | |
|
1214 | 1214 | def self.by_assigned_to(project) |
|
1215 | 1215 | count_and_group_by(:project => project, :association => :assigned_to) |
|
1216 | 1216 | end |
|
1217 | 1217 | |
|
1218 | 1218 | def self.by_author(project) |
|
1219 | 1219 | count_and_group_by(:project => project, :association => :author) |
|
1220 | 1220 | end |
|
1221 | 1221 | |
|
1222 | 1222 | def self.by_subproject(project) |
|
1223 | 1223 | r = count_and_group_by(:project => project, :with_subprojects => true, :association => :project) |
|
1224 | 1224 | r.reject {|r| r["project_id"] == project.id.to_s} |
|
1225 | 1225 | end |
|
1226 | 1226 | |
|
1227 | 1227 | # Query generator for selecting groups of issue counts for a project |
|
1228 | 1228 | # based on specific criteria |
|
1229 | 1229 | # |
|
1230 | 1230 | # Options |
|
1231 | 1231 | # * project - Project to search in. |
|
1232 | 1232 | # * with_subprojects - Includes subprojects issues if set to true. |
|
1233 | 1233 | # * association - Symbol. Association for grouping. |
|
1234 | 1234 | def self.count_and_group_by(options) |
|
1235 | 1235 | assoc = reflect_on_association(options[:association]) |
|
1236 | 1236 | select_field = assoc.foreign_key |
|
1237 | 1237 | |
|
1238 | 1238 | Issue. |
|
1239 | 1239 | visible(User.current, :project => options[:project], :with_subprojects => options[:with_subprojects]). |
|
1240 | 1240 | joins(:status, assoc.name). |
|
1241 | 1241 | group(:status_id, :is_closed, select_field). |
|
1242 | 1242 | count. |
|
1243 | 1243 | map do |columns, total| |
|
1244 | 1244 | status_id, is_closed, field_value = columns |
|
1245 | 1245 | is_closed = ['t', 'true', '1'].include?(is_closed.to_s) |
|
1246 | 1246 | { |
|
1247 | 1247 | "status_id" => status_id.to_s, |
|
1248 | 1248 | "closed" => is_closed, |
|
1249 | 1249 | select_field => field_value.to_s, |
|
1250 | 1250 | "total" => total.to_s |
|
1251 | 1251 | } |
|
1252 | 1252 | end |
|
1253 | 1253 | end |
|
1254 | 1254 | |
|
1255 | 1255 | # Returns a scope of projects that user can assign the issue to |
|
1256 | 1256 | def allowed_target_projects(user=User.current) |
|
1257 | 1257 | if new_record? |
|
1258 | 1258 | Project.where(Project.allowed_to_condition(user, :add_issues)) |
|
1259 | 1259 | else |
|
1260 | 1260 | self.class.allowed_target_projects_on_move(user) |
|
1261 | 1261 | end |
|
1262 | 1262 | end |
|
1263 | 1263 | |
|
1264 | 1264 | # Returns a scope of projects that user can move issues to |
|
1265 | 1265 | def self.allowed_target_projects_on_move(user=User.current) |
|
1266 | 1266 | Project.where(Project.allowed_to_condition(user, :move_issues)) |
|
1267 | 1267 | end |
|
1268 | 1268 | |
|
1269 | 1269 | private |
|
1270 | 1270 | |
|
1271 | 1271 | def after_project_change |
|
1272 | 1272 | # Update project_id on related time entries |
|
1273 | 1273 | TimeEntry.where({:issue_id => id}).update_all(["project_id = ?", project_id]) |
|
1274 | 1274 | |
|
1275 | 1275 | # Delete issue relations |
|
1276 | 1276 | unless Setting.cross_project_issue_relations? |
|
1277 | 1277 | relations_from.clear |
|
1278 | 1278 | relations_to.clear |
|
1279 | 1279 | end |
|
1280 | 1280 | |
|
1281 | 1281 | # Move subtasks that were in the same project |
|
1282 | 1282 | children.each do |child| |
|
1283 | 1283 | next unless child.project_id == project_id_was |
|
1284 | 1284 | # Change project and keep project |
|
1285 | 1285 | child.send :project=, project, true |
|
1286 | 1286 | unless child.save |
|
1287 | 1287 | raise ActiveRecord::Rollback |
|
1288 | 1288 | end |
|
1289 | 1289 | end |
|
1290 | 1290 | end |
|
1291 | 1291 | |
|
1292 | 1292 | # Callback for after the creation of an issue by copy |
|
1293 | 1293 | # * adds a "copied to" relation with the copied issue |
|
1294 | 1294 | # * copies subtasks from the copied issue |
|
1295 | 1295 | def after_create_from_copy |
|
1296 | 1296 | return unless copy? && !@after_create_from_copy_handled |
|
1297 | 1297 | |
|
1298 | 1298 | if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false |
|
1299 | 1299 | relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO) |
|
1300 | 1300 | unless relation.save |
|
1301 | 1301 | logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger |
|
1302 | 1302 | end |
|
1303 | 1303 | end |
|
1304 | 1304 | |
|
1305 | 1305 | unless @copied_from.leaf? || @copy_options[:subtasks] == false |
|
1306 | 1306 | copy_options = (@copy_options || {}).merge(:subtasks => false) |
|
1307 | 1307 | copied_issue_ids = {@copied_from.id => self.id} |
|
1308 | 1308 | @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child| |
|
1309 | 1309 | # Do not copy self when copying an issue as a descendant of the copied issue |
|
1310 | 1310 | next if child == self |
|
1311 | 1311 | # Do not copy subtasks of issues that were not copied |
|
1312 | 1312 | next unless copied_issue_ids[child.parent_id] |
|
1313 | 1313 | # Do not copy subtasks that are not visible to avoid potential disclosure of private data |
|
1314 | 1314 | unless child.visible? |
|
1315 | 1315 | logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger |
|
1316 | 1316 | next |
|
1317 | 1317 | end |
|
1318 | 1318 | copy = Issue.new.copy_from(child, copy_options) |
|
1319 | 1319 | copy.author = author |
|
1320 | 1320 | copy.project = project |
|
1321 | 1321 | copy.parent_issue_id = copied_issue_ids[child.parent_id] |
|
1322 | 1322 | unless copy.save |
|
1323 | 1323 | logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger |
|
1324 | 1324 | next |
|
1325 | 1325 | end |
|
1326 | 1326 | copied_issue_ids[child.id] = copy.id |
|
1327 | 1327 | end |
|
1328 | 1328 | end |
|
1329 | 1329 | @after_create_from_copy_handled = true |
|
1330 | 1330 | end |
|
1331 | 1331 | |
|
1332 | 1332 | def update_nested_set_attributes |
|
1333 | 1333 | if root_id.nil? |
|
1334 | 1334 | # issue was just created |
|
1335 | 1335 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
1336 | 1336 | Issue.where(["id = ?", id]).update_all(["root_id = ?", root_id]) |
|
1337 | 1337 | if @parent_issue |
|
1338 | 1338 | move_to_child_of(@parent_issue) |
|
1339 | 1339 | end |
|
1340 | 1340 | elsif parent_issue_id != parent_id |
|
1341 | 1341 | update_nested_set_attributes_on_parent_change |
|
1342 | 1342 | end |
|
1343 | 1343 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) |
|
1344 | 1344 | end |
|
1345 | 1345 | |
|
1346 | 1346 | # Updates the nested set for when an existing issue is moved |
|
1347 | 1347 | def update_nested_set_attributes_on_parent_change |
|
1348 | 1348 | former_parent_id = parent_id |
|
1349 | 1349 | # moving an existing issue |
|
1350 | 1350 | if @parent_issue && @parent_issue.root_id == root_id |
|
1351 | 1351 | # inside the same tree |
|
1352 | 1352 | move_to_child_of(@parent_issue) |
|
1353 | 1353 | else |
|
1354 | 1354 | # to another tree |
|
1355 | 1355 | unless root? |
|
1356 | 1356 | move_to_right_of(root) |
|
1357 | 1357 | end |
|
1358 | 1358 | old_root_id = root_id |
|
1359 | 1359 | in_tenacious_transaction do |
|
1360 | 1360 | @parent_issue.reload_nested_set if @parent_issue |
|
1361 | 1361 | self.reload_nested_set |
|
1362 | 1362 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
1363 | 1363 | cond = ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt] |
|
1364 | 1364 | self.class.base_class.select('id').lock(true).where(cond) |
|
1365 | 1365 | offset = rdm_right_most_bound + 1 - lft |
|
1366 | 1366 | Issue.where(cond). |
|
1367 | 1367 | update_all(["root_id = ?, lft = lft + ?, rgt = rgt + ?", root_id, offset, offset]) |
|
1368 | 1368 | end |
|
1369 | 1369 | if @parent_issue |
|
1370 | 1370 | move_to_child_of(@parent_issue) |
|
1371 | 1371 | end |
|
1372 | 1372 | end |
|
1373 | 1373 | # delete invalid relations of all descendants |
|
1374 | 1374 | self_and_descendants.each do |issue| |
|
1375 | 1375 | issue.relations.each do |relation| |
|
1376 | 1376 | relation.destroy unless relation.valid? |
|
1377 | 1377 | end |
|
1378 | 1378 | end |
|
1379 | 1379 | # update former parent |
|
1380 | 1380 | recalculate_attributes_for(former_parent_id) if former_parent_id |
|
1381 | 1381 | end |
|
1382 | 1382 | |
|
1383 | 1383 | def rdm_right_most_bound |
|
1384 | 1384 | right_most_node = |
|
1385 | 1385 | self.class.base_class.unscoped. |
|
1386 | 1386 | order("#{quoted_right_column_full_name} desc").limit(1).lock(true).first |
|
1387 | 1387 | right_most_node ? (right_most_node[right_column_name] || 0 ) : 0 |
|
1388 | 1388 | end |
|
1389 | 1389 | private :rdm_right_most_bound |
|
1390 | 1390 | |
|
1391 | 1391 | def update_parent_attributes |
|
1392 | 1392 | recalculate_attributes_for(parent_id) if parent_id |
|
1393 | 1393 | end |
|
1394 | 1394 | |
|
1395 | 1395 | def recalculate_attributes_for(issue_id) |
|
1396 | 1396 | if issue_id && p = Issue.find_by_id(issue_id) |
|
1397 | 1397 | # priority = highest priority of children |
|
1398 | 1398 | if priority_position = p.children.joins(:priority).maximum("#{IssuePriority.table_name}.position") |
|
1399 | 1399 | p.priority = IssuePriority.find_by_position(priority_position) |
|
1400 | 1400 | end |
|
1401 | 1401 | |
|
1402 | 1402 | # start/due dates = lowest/highest dates of children |
|
1403 | 1403 | p.start_date = p.children.minimum(:start_date) |
|
1404 | 1404 | p.due_date = p.children.maximum(:due_date) |
|
1405 | 1405 | if p.start_date && p.due_date && p.due_date < p.start_date |
|
1406 | 1406 | p.start_date, p.due_date = p.due_date, p.start_date |
|
1407 | 1407 | end |
|
1408 | 1408 | |
|
1409 | 1409 | # done ratio = weighted average ratio of leaves |
|
1410 | 1410 | unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio |
|
1411 | 1411 | leaves_count = p.leaves.count |
|
1412 | 1412 | if leaves_count > 0 |
|
1413 | 1413 | average = p.leaves.where("estimated_hours > 0").average(:estimated_hours).to_f |
|
1414 | 1414 | if average == 0 |
|
1415 | 1415 | average = 1 |
|
1416 | 1416 | end |
|
1417 | 1417 | done = p.leaves.joins(:status). |
|
1418 | 1418 | sum("COALESCE(CASE WHEN estimated_hours > 0 THEN estimated_hours ELSE NULL END, #{average}) " + |
|
1419 | 1419 | "* (CASE WHEN is_closed = #{self.class.connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)").to_f |
|
1420 | 1420 | progress = done / (average * leaves_count) |
|
1421 | 1421 | p.done_ratio = progress.round |
|
1422 | 1422 | end |
|
1423 | 1423 | end |
|
1424 | 1424 | |
|
1425 | 1425 | # estimate = sum of leaves estimates |
|
1426 | 1426 | p.estimated_hours = p.leaves.sum(:estimated_hours).to_f |
|
1427 | 1427 | p.estimated_hours = nil if p.estimated_hours == 0.0 |
|
1428 | 1428 | |
|
1429 | 1429 | # ancestors will be recursively updated |
|
1430 | 1430 | p.save(:validate => false) |
|
1431 | 1431 | end |
|
1432 | 1432 | end |
|
1433 | 1433 | |
|
1434 | 1434 | # Update issues so their versions are not pointing to a |
|
1435 | 1435 | # fixed_version that is not shared with the issue's project |
|
1436 | 1436 | def self.update_versions(conditions=nil) |
|
1437 | 1437 | # Only need to update issues with a fixed_version from |
|
1438 | 1438 | # a different project and that is not systemwide shared |
|
1439 | 1439 | Issue.joins(:project, :fixed_version). |
|
1440 | 1440 | where("#{Issue.table_name}.fixed_version_id IS NOT NULL" + |
|
1441 | 1441 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + |
|
1442 | 1442 | " AND #{Version.table_name}.sharing <> 'system'"). |
|
1443 | 1443 | where(conditions).each do |issue| |
|
1444 | 1444 | next if issue.project.nil? || issue.fixed_version.nil? |
|
1445 | 1445 | unless issue.project.shared_versions.include?(issue.fixed_version) |
|
1446 | 1446 | issue.init_journal(User.current) |
|
1447 | 1447 | issue.fixed_version = nil |
|
1448 | 1448 | issue.save |
|
1449 | 1449 | end |
|
1450 | 1450 | end |
|
1451 | 1451 | end |
|
1452 | 1452 | |
|
1453 | 1453 | # Callback on file attachment |
|
1454 | 1454 | def attachment_added(obj) |
|
1455 | 1455 | if @current_journal && !obj.new_record? |
|
1456 | 1456 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename) |
|
1457 | 1457 | end |
|
1458 | 1458 | end |
|
1459 | 1459 | |
|
1460 | 1460 | # Callback on attachment deletion |
|
1461 | 1461 | def attachment_removed(obj) |
|
1462 | 1462 | if @current_journal && !obj.new_record? |
|
1463 | 1463 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename) |
|
1464 | 1464 | @current_journal.save |
|
1465 | 1465 | end |
|
1466 | 1466 | end |
|
1467 | 1467 | |
|
1468 | 1468 | # Default assignment based on category |
|
1469 | 1469 | def default_assign |
|
1470 | 1470 | if assigned_to.nil? && category && category.assigned_to |
|
1471 | 1471 | self.assigned_to = category.assigned_to |
|
1472 | 1472 | end |
|
1473 | 1473 | end |
|
1474 | 1474 | |
|
1475 | 1475 | # Updates start/due dates of following issues |
|
1476 | 1476 | def reschedule_following_issues |
|
1477 | 1477 | if start_date_changed? || due_date_changed? |
|
1478 | 1478 | relations_from.each do |relation| |
|
1479 | 1479 | relation.set_issue_to_dates |
|
1480 | 1480 | end |
|
1481 | 1481 | end |
|
1482 | 1482 | end |
|
1483 | 1483 | |
|
1484 | 1484 | # Closes duplicates if the issue is being closed |
|
1485 | 1485 | def close_duplicates |
|
1486 | 1486 | if closing? |
|
1487 | 1487 | duplicates.each do |duplicate| |
|
1488 | 1488 | # Reload is needed in case the duplicate was updated by a previous duplicate |
|
1489 | 1489 | duplicate.reload |
|
1490 | 1490 | # Don't re-close it if it's already closed |
|
1491 | 1491 | next if duplicate.closed? |
|
1492 | 1492 | # Same user and notes |
|
1493 | 1493 | if @current_journal |
|
1494 | 1494 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
1495 | 1495 | end |
|
1496 | 1496 | duplicate.update_attribute :status, self.status |
|
1497 | 1497 | end |
|
1498 | 1498 | end |
|
1499 | 1499 | end |
|
1500 | 1500 | |
|
1501 | 1501 | # Make sure updated_on is updated when adding a note and set updated_on now |
|
1502 | 1502 | # so we can set closed_on with the same value on closing |
|
1503 | 1503 | def force_updated_on_change |
|
1504 | 1504 | if @current_journal || changed? |
|
1505 | 1505 | self.updated_on = current_time_from_proper_timezone |
|
1506 | 1506 | if new_record? |
|
1507 | 1507 | self.created_on = updated_on |
|
1508 | 1508 | end |
|
1509 | 1509 | end |
|
1510 | 1510 | end |
|
1511 | 1511 | |
|
1512 | 1512 | # Callback for setting closed_on when the issue is closed. |
|
1513 | 1513 | # The closed_on attribute stores the time of the last closing |
|
1514 | 1514 | # and is preserved when the issue is reopened. |
|
1515 | 1515 | def update_closed_on |
|
1516 | 1516 | if closing? |
|
1517 | 1517 | self.closed_on = updated_on |
|
1518 | 1518 | end |
|
1519 | 1519 | end |
|
1520 | 1520 | |
|
1521 | 1521 | # Saves the changes in a Journal |
|
1522 | 1522 | # Called after_save |
|
1523 | 1523 | def create_journal |
|
1524 | 1524 | if @current_journal |
|
1525 | 1525 | # attributes changes |
|
1526 | 1526 | if @attributes_before_change |
|
1527 | 1527 | (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on closed_on)).each {|c| |
|
1528 | 1528 | before = @attributes_before_change[c] |
|
1529 | 1529 | after = send(c) |
|
1530 | 1530 | next if before == after || (before.blank? && after.blank?) |
|
1531 | 1531 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
1532 | 1532 | :prop_key => c, |
|
1533 | 1533 | :old_value => before, |
|
1534 | 1534 | :value => after) |
|
1535 | 1535 | } |
|
1536 | 1536 | end |
|
1537 | 1537 | if @custom_values_before_change |
|
1538 | 1538 | # custom fields changes |
|
1539 | 1539 | custom_field_values.each {|c| |
|
1540 | 1540 | before = @custom_values_before_change[c.custom_field_id] |
|
1541 | 1541 | after = c.value |
|
1542 | 1542 | next if before == after || (before.blank? && after.blank?) |
|
1543 | 1543 | |
|
1544 | 1544 | if before.is_a?(Array) || after.is_a?(Array) |
|
1545 | 1545 | before = [before] unless before.is_a?(Array) |
|
1546 | 1546 | after = [after] unless after.is_a?(Array) |
|
1547 | 1547 | |
|
1548 | 1548 | # values removed |
|
1549 | 1549 | (before - after).reject(&:blank?).each do |value| |
|
1550 | 1550 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1551 | 1551 | :prop_key => c.custom_field_id, |
|
1552 | 1552 | :old_value => value, |
|
1553 | 1553 | :value => nil) |
|
1554 | 1554 | end |
|
1555 | 1555 | # values added |
|
1556 | 1556 | (after - before).reject(&:blank?).each do |value| |
|
1557 | 1557 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1558 | 1558 | :prop_key => c.custom_field_id, |
|
1559 | 1559 | :old_value => nil, |
|
1560 | 1560 | :value => value) |
|
1561 | 1561 | end |
|
1562 | 1562 | else |
|
1563 | 1563 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1564 | 1564 | :prop_key => c.custom_field_id, |
|
1565 | 1565 | :old_value => before, |
|
1566 | 1566 | :value => after) |
|
1567 | 1567 | end |
|
1568 | 1568 | } |
|
1569 | 1569 | end |
|
1570 | 1570 | @current_journal.save |
|
1571 | 1571 | # reset current journal |
|
1572 | 1572 | init_journal @current_journal.user, @current_journal.notes |
|
1573 | 1573 | end |
|
1574 | 1574 | end |
|
1575 | 1575 | |
|
1576 | 1576 | def send_notification |
|
1577 | 1577 | if Setting.notified_events.include?('issue_added') |
|
1578 | 1578 | Mailer.deliver_issue_add(self) |
|
1579 | 1579 | end |
|
1580 | 1580 | end |
|
1581 | 1581 | |
|
1582 | 1582 | # Stores the previous assignee so we can still have access |
|
1583 | 1583 | # to it during after_save callbacks (assigned_to_id_was is reset) |
|
1584 | 1584 | def set_assigned_to_was |
|
1585 | 1585 | @previous_assigned_to_id = assigned_to_id_was |
|
1586 | 1586 | end |
|
1587 | 1587 | |
|
1588 | 1588 | # Clears the previous assignee at the end of after_save callbacks |
|
1589 | 1589 | def clear_assigned_to_was |
|
1590 | 1590 | @assigned_to_was = nil |
|
1591 | 1591 | @previous_assigned_to_id = nil |
|
1592 | 1592 | end |
|
1593 | 1593 | end |
@@ -1,1068 +1,1070 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 Project < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | |
|
21 | 21 | # Project statuses |
|
22 | 22 | STATUS_ACTIVE = 1 |
|
23 | 23 | STATUS_CLOSED = 5 |
|
24 | 24 | STATUS_ARCHIVED = 9 |
|
25 | 25 | |
|
26 | 26 | # Maximum length for project identifiers |
|
27 | 27 | IDENTIFIER_MAX_LENGTH = 100 |
|
28 | 28 | |
|
29 | 29 | # Specific overridden Activities |
|
30 | 30 | has_many :time_entry_activities |
|
31 | 31 | has_many :members, |
|
32 | 32 | lambda { joins(:principal, :roles). |
|
33 | 33 | where("#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}") } |
|
34 | 34 | has_many :memberships, :class_name => 'Member' |
|
35 | 35 | has_many :member_principals, |
|
36 | 36 | lambda { joins(:principal). |
|
37 | 37 | where("#{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}")}, |
|
38 | 38 | :class_name => 'Member' |
|
39 | 39 | has_many :enabled_modules, :dependent => :delete_all |
|
40 | 40 | has_and_belongs_to_many :trackers, lambda {order("#{Tracker.table_name}.position")} |
|
41 | 41 | has_many :issues, :dependent => :destroy |
|
42 | 42 | has_many :issue_changes, :through => :issues, :source => :journals |
|
43 | 43 | has_many :versions, lambda {order("#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC")}, :dependent => :destroy |
|
44 | 44 | has_many :time_entries, :dependent => :destroy |
|
45 | 45 | has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all |
|
46 | 46 | has_many :documents, :dependent => :destroy |
|
47 | 47 | has_many :news, lambda {includes(:author)}, :dependent => :destroy |
|
48 | 48 | has_many :issue_categories, lambda {order("#{IssueCategory.table_name}.name")}, :dependent => :delete_all |
|
49 | 49 | has_many :boards, lambda {order("position ASC")}, :dependent => :destroy |
|
50 | 50 | has_one :repository, lambda {where(["is_default = ?", true])} |
|
51 | 51 | has_many :repositories, :dependent => :destroy |
|
52 | 52 | has_many :changesets, :through => :repository |
|
53 | 53 | has_one :wiki, :dependent => :destroy |
|
54 | 54 | # Custom field for the project issues |
|
55 | 55 | has_and_belongs_to_many :issue_custom_fields, |
|
56 | 56 | lambda {order("#{CustomField.table_name}.position")}, |
|
57 | 57 | :class_name => 'IssueCustomField', |
|
58 | 58 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", |
|
59 | 59 | :association_foreign_key => 'custom_field_id' |
|
60 | 60 | |
|
61 | 61 | acts_as_nested_set :dependent => :destroy |
|
62 | 62 | acts_as_attachable :view_permission => :view_files, |
|
63 | 63 | :delete_permission => :manage_files |
|
64 | 64 | |
|
65 | 65 | acts_as_customizable |
|
66 | 66 | acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil |
|
67 | 67 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, |
|
68 | 68 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}}, |
|
69 | 69 | :author => nil |
|
70 | 70 | |
|
71 | 71 | attr_protected :status |
|
72 | 72 | |
|
73 | 73 | validates_presence_of :name, :identifier |
|
74 | 74 | validates_uniqueness_of :identifier |
|
75 | 75 | validates_associated :repository, :wiki |
|
76 | 76 | validates_length_of :name, :maximum => 255 |
|
77 | 77 | validates_length_of :homepage, :maximum => 255 |
|
78 | 78 | validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH |
|
79 | 79 | # downcase letters, digits, dashes but not digits only |
|
80 | 80 | validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :if => Proc.new { |p| p.identifier_changed? } |
|
81 | 81 | # reserved words |
|
82 | 82 | validates_exclusion_of :identifier, :in => %w( new ) |
|
83 | 83 | |
|
84 | 84 | after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?} |
|
85 | 85 | after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?} |
|
86 | 86 | before_destroy :delete_all_members |
|
87 | 87 | |
|
88 | 88 | scope :has_module, lambda {|mod| |
|
89 | 89 | where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s) |
|
90 | 90 | } |
|
91 | 91 | scope :active, lambda { where(:status => STATUS_ACTIVE) } |
|
92 | 92 | scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } |
|
93 | 93 | scope :all_public, lambda { where(:is_public => true) } |
|
94 | 94 | scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) } |
|
95 | 95 | scope :allowed_to, lambda {|*args| |
|
96 | 96 | user = User.current |
|
97 | 97 | permission = nil |
|
98 | 98 | if args.first.is_a?(Symbol) |
|
99 | 99 | permission = args.shift |
|
100 | 100 | else |
|
101 | 101 | user = args.shift |
|
102 | 102 | permission = args.shift |
|
103 | 103 | end |
|
104 | 104 | where(Project.allowed_to_condition(user, permission, *args)) |
|
105 | 105 | } |
|
106 | 106 | scope :like, lambda {|arg| |
|
107 | 107 | if arg.blank? |
|
108 | 108 | where(nil) |
|
109 | 109 | else |
|
110 | 110 | pattern = "%#{arg.to_s.strip.downcase}%" |
|
111 | 111 | where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern) |
|
112 | 112 | end |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | def initialize(attributes=nil, *args) |
|
116 | 116 | super |
|
117 | 117 | |
|
118 | 118 | initialized = (attributes || {}).stringify_keys |
|
119 | 119 | if !initialized.key?('identifier') && Setting.sequential_project_identifiers? |
|
120 | 120 | self.identifier = Project.next_identifier |
|
121 | 121 | end |
|
122 | 122 | if !initialized.key?('is_public') |
|
123 | 123 | self.is_public = Setting.default_projects_public? |
|
124 | 124 | end |
|
125 | 125 | if !initialized.key?('enabled_module_names') |
|
126 | 126 | self.enabled_module_names = Setting.default_projects_modules |
|
127 | 127 | end |
|
128 | 128 | if !initialized.key?('trackers') && !initialized.key?('tracker_ids') |
|
129 | 129 | default = Setting.default_projects_tracker_ids |
|
130 | 130 | if default.is_a?(Array) |
|
131 | 131 | self.trackers = Tracker.where(:id => default.map(&:to_i)).sorted.to_a |
|
132 | 132 | else |
|
133 | 133 | self.trackers = Tracker.sorted.to_a |
|
134 | 134 | end |
|
135 | 135 | end |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def identifier=(identifier) |
|
139 | 139 | super unless identifier_frozen? |
|
140 | 140 | end |
|
141 | 141 | |
|
142 | 142 | def identifier_frozen? |
|
143 | 143 | errors[:identifier].blank? && !(new_record? || identifier.blank?) |
|
144 | 144 | end |
|
145 | 145 | |
|
146 | 146 | # returns latest created projects |
|
147 | 147 | # non public projects will be returned only if user is a member of those |
|
148 | 148 | def self.latest(user=nil, count=5) |
|
149 | 149 | visible(user).limit(count).order("created_on DESC").to_a |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | # Returns true if the project is visible to +user+ or to the current user. |
|
153 | 153 | def visible?(user=User.current) |
|
154 | 154 | user.allowed_to?(:view_project, self) |
|
155 | 155 | end |
|
156 | 156 | |
|
157 | 157 | # Returns a SQL conditions string used to find all projects visible by the specified user. |
|
158 | 158 | # |
|
159 | 159 | # Examples: |
|
160 | 160 | # Project.visible_condition(admin) => "projects.status = 1" |
|
161 | 161 | # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))" |
|
162 | 162 | # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))" |
|
163 | 163 | def self.visible_condition(user, options={}) |
|
164 | 164 | allowed_to_condition(user, :view_project, options) |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+ |
|
168 | 168 | # |
|
169 | 169 | # Valid options: |
|
170 | 170 | # * :project => limit the condition to project |
|
171 | 171 | # * :with_subprojects => limit the condition to project and its subprojects |
|
172 | 172 | # * :member => limit the condition to the user projects |
|
173 | 173 | def self.allowed_to_condition(user, permission, options={}) |
|
174 | 174 | perm = Redmine::AccessControl.permission(permission) |
|
175 | 175 | base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}") |
|
176 | 176 | if perm && perm.project_module |
|
177 | 177 | # If the permission belongs to a project module, make sure the module is enabled |
|
178 | 178 | base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" |
|
179 | 179 | end |
|
180 | 180 | if project = options[:project] |
|
181 | 181 | project_statement = project.project_condition(options[:with_subprojects]) |
|
182 | 182 | base_statement = "(#{project_statement}) AND (#{base_statement})" |
|
183 | 183 | end |
|
184 | 184 | |
|
185 | 185 | if user.admin? |
|
186 | 186 | base_statement |
|
187 | 187 | else |
|
188 | 188 | statement_by_role = {} |
|
189 | 189 | unless options[:member] |
|
190 | 190 | role = user.builtin_role |
|
191 | 191 | if role.allowed_to?(permission) |
|
192 | 192 | statement_by_role[role] = "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
193 | 193 | end |
|
194 | 194 | end |
|
195 | 195 | user.projects_by_role.each do |role, projects| |
|
196 | 196 | if role.allowed_to?(permission) && projects.any? |
|
197 | 197 | statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})" |
|
198 | 198 | end |
|
199 | 199 | end |
|
200 | 200 | if statement_by_role.empty? |
|
201 | 201 | "1=0" |
|
202 | 202 | else |
|
203 | 203 | if block_given? |
|
204 | 204 | statement_by_role.each do |role, statement| |
|
205 | 205 | if s = yield(role, user) |
|
206 | 206 | statement_by_role[role] = "(#{statement} AND (#{s}))" |
|
207 | 207 | end |
|
208 | 208 | end |
|
209 | 209 | end |
|
210 | 210 | "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))" |
|
211 | 211 | end |
|
212 | 212 | end |
|
213 | 213 | end |
|
214 | 214 | |
|
215 | 215 | def override_roles(role) |
|
216 | 216 | group_class = role.anonymous? ? GroupAnonymous : GroupNonMember |
|
217 | 217 | member = member_principals.where("#{Principal.table_name}.type = ?", group_class.name).first |
|
218 | 218 | member ? member.roles.to_a : [role] |
|
219 | 219 | end |
|
220 | 220 | |
|
221 | 221 | def principals |
|
222 | 222 | @principals ||= Principal.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq |
|
223 | 223 | end |
|
224 | 224 | |
|
225 | 225 | def users |
|
226 | 226 | @users ||= User.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq |
|
227 | 227 | end |
|
228 | 228 | |
|
229 | 229 | # Returns the Systemwide and project specific activities |
|
230 | 230 | def activities(include_inactive=false) |
|
231 | 231 | if include_inactive |
|
232 | 232 | return all_activities |
|
233 | 233 | else |
|
234 | 234 | return active_activities |
|
235 | 235 | end |
|
236 | 236 | end |
|
237 | 237 | |
|
238 | 238 | # Will create a new Project specific Activity or update an existing one |
|
239 | 239 | # |
|
240 | 240 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity |
|
241 | 241 | # does not successfully save. |
|
242 | 242 | def update_or_create_time_entry_activity(id, activity_hash) |
|
243 | 243 | if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id') |
|
244 | 244 | self.create_time_entry_activity_if_needed(activity_hash) |
|
245 | 245 | else |
|
246 | 246 | activity = project.time_entry_activities.find_by_id(id.to_i) |
|
247 | 247 | activity.update_attributes(activity_hash) if activity |
|
248 | 248 | end |
|
249 | 249 | end |
|
250 | 250 | |
|
251 | 251 | # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity |
|
252 | 252 | # |
|
253 | 253 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity |
|
254 | 254 | # does not successfully save. |
|
255 | 255 | def create_time_entry_activity_if_needed(activity) |
|
256 | 256 | if activity['parent_id'] |
|
257 | 257 | parent_activity = TimeEntryActivity.find(activity['parent_id']) |
|
258 | 258 | activity['name'] = parent_activity.name |
|
259 | 259 | activity['position'] = parent_activity.position |
|
260 | 260 | if Enumeration.overriding_change?(activity, parent_activity) |
|
261 | 261 | project_activity = self.time_entry_activities.create(activity) |
|
262 | 262 | if project_activity.new_record? |
|
263 | 263 | raise ActiveRecord::Rollback, "Overriding TimeEntryActivity was not successfully saved" |
|
264 | 264 | else |
|
265 | 265 | self.time_entries. |
|
266 | 266 | where(["activity_id = ?", parent_activity.id]). |
|
267 | 267 | update_all("activity_id = #{project_activity.id}") |
|
268 | 268 | end |
|
269 | 269 | end |
|
270 | 270 | end |
|
271 | 271 | end |
|
272 | 272 | |
|
273 | 273 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. |
|
274 | 274 | # |
|
275 | 275 | # Examples: |
|
276 | 276 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" |
|
277 | 277 | # project.project_condition(false) => "projects.id = 1" |
|
278 | 278 | def project_condition(with_subprojects) |
|
279 | 279 | cond = "#{Project.table_name}.id = #{id}" |
|
280 | 280 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects |
|
281 | 281 | cond |
|
282 | 282 | end |
|
283 | 283 | |
|
284 | 284 | def self.find(*args) |
|
285 | 285 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) |
|
286 | 286 | project = find_by_identifier(*args) |
|
287 | 287 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? |
|
288 | 288 | project |
|
289 | 289 | else |
|
290 | 290 | super |
|
291 | 291 | end |
|
292 | 292 | end |
|
293 | 293 | |
|
294 | 294 | def self.find_by_param(*args) |
|
295 | 295 | self.find(*args) |
|
296 | 296 | end |
|
297 | 297 | |
|
298 | 298 | alias :base_reload :reload |
|
299 | 299 | def reload(*args) |
|
300 | 300 | @principals = nil |
|
301 | 301 | @users = nil |
|
302 | 302 | @shared_versions = nil |
|
303 | 303 | @rolled_up_versions = nil |
|
304 | 304 | @rolled_up_trackers = nil |
|
305 | 305 | @all_issue_custom_fields = nil |
|
306 | 306 | @all_time_entry_custom_fields = nil |
|
307 | 307 | @to_param = nil |
|
308 | 308 | @allowed_parents = nil |
|
309 | 309 | @allowed_permissions = nil |
|
310 | 310 | @actions_allowed = nil |
|
311 | 311 | @start_date = nil |
|
312 | 312 | @due_date = nil |
|
313 | 313 | @override_members = nil |
|
314 | 314 | base_reload(*args) |
|
315 | 315 | end |
|
316 | 316 | |
|
317 | 317 | def to_param |
|
318 | 318 | # id is used for projects with a numeric identifier (compatibility) |
|
319 | 319 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier) |
|
320 | 320 | end |
|
321 | 321 | |
|
322 | 322 | def active? |
|
323 | 323 | self.status == STATUS_ACTIVE |
|
324 | 324 | end |
|
325 | 325 | |
|
326 | 326 | def archived? |
|
327 | 327 | self.status == STATUS_ARCHIVED |
|
328 | 328 | end |
|
329 | 329 | |
|
330 | 330 | # Archives the project and its descendants |
|
331 | 331 | def archive |
|
332 | 332 | # Check that there is no issue of a non descendant project that is assigned |
|
333 | 333 | # to one of the project or descendant versions |
|
334 | 334 | v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten |
|
335 | 335 | if v_ids.any? && |
|
336 | 336 | Issue. |
|
337 | 337 | includes(:project). |
|
338 | 338 | where("#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?", lft, rgt). |
|
339 | 339 | where("#{Issue.table_name}.fixed_version_id IN (?)", v_ids). |
|
340 | 340 | exists? |
|
341 | 341 | return false |
|
342 | 342 | end |
|
343 | 343 | Project.transaction do |
|
344 | 344 | archive! |
|
345 | 345 | end |
|
346 | 346 | true |
|
347 | 347 | end |
|
348 | 348 | |
|
349 | 349 | # Unarchives the project |
|
350 | 350 | # All its ancestors must be active |
|
351 | 351 | def unarchive |
|
352 | 352 | return false if ancestors.detect {|a| !a.active?} |
|
353 | 353 | update_attribute :status, STATUS_ACTIVE |
|
354 | 354 | end |
|
355 | 355 | |
|
356 | 356 | def close |
|
357 | 357 | self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED |
|
358 | 358 | end |
|
359 | 359 | |
|
360 | 360 | def reopen |
|
361 | 361 | self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | # Returns an array of projects the project can be moved to |
|
365 | 365 | # by the current user |
|
366 | 366 | def allowed_parents |
|
367 | 367 | return @allowed_parents if @allowed_parents |
|
368 | 368 | @allowed_parents = Project.where(Project.allowed_to_condition(User.current, :add_subprojects)).to_a |
|
369 | 369 | @allowed_parents = @allowed_parents - self_and_descendants |
|
370 | 370 | if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?) |
|
371 | 371 | @allowed_parents << nil |
|
372 | 372 | end |
|
373 | 373 | unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent) |
|
374 | 374 | @allowed_parents << parent |
|
375 | 375 | end |
|
376 | 376 | @allowed_parents |
|
377 | 377 | end |
|
378 | 378 | |
|
379 | 379 | # Sets the parent of the project with authorization check |
|
380 | 380 | def set_allowed_parent!(p) |
|
381 | 381 | unless p.nil? || p.is_a?(Project) |
|
382 | 382 | if p.to_s.blank? |
|
383 | 383 | p = nil |
|
384 | 384 | else |
|
385 | 385 | p = Project.find_by_id(p) |
|
386 | 386 | return false unless p |
|
387 | 387 | end |
|
388 | 388 | end |
|
389 | 389 | if p.nil? |
|
390 | 390 | if !new_record? && allowed_parents.empty? |
|
391 | 391 | return false |
|
392 | 392 | end |
|
393 | 393 | elsif !allowed_parents.include?(p) |
|
394 | 394 | return false |
|
395 | 395 | end |
|
396 | 396 | set_parent!(p) |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | # Sets the parent of the project |
|
400 | 400 | # Argument can be either a Project, a String, a Fixnum or nil |
|
401 | 401 | def set_parent!(p) |
|
402 | 402 | unless p.nil? || p.is_a?(Project) |
|
403 | 403 | if p.to_s.blank? |
|
404 | 404 | p = nil |
|
405 | 405 | else |
|
406 | 406 | p = Project.find_by_id(p) |
|
407 | 407 | return false unless p |
|
408 | 408 | end |
|
409 | 409 | end |
|
410 | 410 | if p == parent && !p.nil? |
|
411 | 411 | # Nothing to do |
|
412 | 412 | true |
|
413 | 413 | elsif p.nil? || (p.active? && move_possible?(p)) |
|
414 | 414 | set_or_update_position_under(p) |
|
415 | 415 | Issue.update_versions_from_hierarchy_change(self) |
|
416 | 416 | true |
|
417 | 417 | else |
|
418 | 418 | # Can not move to the given target |
|
419 | 419 | false |
|
420 | 420 | end |
|
421 | 421 | end |
|
422 | 422 | |
|
423 | 423 | # Recalculates all lft and rgt values based on project names |
|
424 | 424 | # Unlike Project.rebuild!, these values are recalculated even if the tree "looks" valid |
|
425 | 425 | # Used in BuildProjectsTree migration |
|
426 | 426 | def self.rebuild_tree! |
|
427 | 427 | transaction do |
|
428 | 428 | update_all "lft = NULL, rgt = NULL" |
|
429 | 429 | rebuild!(false) |
|
430 | 430 | all.each { |p| p.set_or_update_position_under(p.parent) } |
|
431 | 431 | end |
|
432 | 432 | end |
|
433 | 433 | |
|
434 | 434 | # Returns an array of the trackers used by the project and its active sub projects |
|
435 | 435 | def rolled_up_trackers |
|
436 | 436 | @rolled_up_trackers ||= |
|
437 | 437 | Tracker. |
|
438 | 438 | joins(:projects). |
|
439 | 439 | joins("JOIN #{EnabledModule.table_name} ON #{EnabledModule.table_name}.project_id = #{Project.table_name}.id AND #{EnabledModule.table_name}.name = 'issue_tracking'"). |
|
440 | 440 | select("DISTINCT #{Tracker.table_name}.*"). |
|
441 | 441 | where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> #{STATUS_ARCHIVED}", lft, rgt). |
|
442 | 442 | sorted. |
|
443 | 443 | to_a |
|
444 | 444 | end |
|
445 | 445 | |
|
446 | 446 | # Closes open and locked project versions that are completed |
|
447 | 447 | def close_completed_versions |
|
448 | 448 | Version.transaction do |
|
449 | 449 | versions.where(:status => %w(open locked)).each do |version| |
|
450 | 450 | if version.completed? |
|
451 | 451 | version.update_attribute(:status, 'closed') |
|
452 | 452 | end |
|
453 | 453 | end |
|
454 | 454 | end |
|
455 | 455 | end |
|
456 | 456 | |
|
457 | 457 | # Returns a scope of the Versions on subprojects |
|
458 | 458 | def rolled_up_versions |
|
459 | 459 | @rolled_up_versions ||= |
|
460 | 460 | Version. |
|
461 | 461 | joins(:project). |
|
462 | 462 | where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED) |
|
463 | 463 | end |
|
464 | 464 | |
|
465 | 465 | # Returns a scope of the Versions used by the project |
|
466 | 466 | def shared_versions |
|
467 | 467 | if new_record? |
|
468 | 468 | Version. |
|
469 | 469 | joins(:project). |
|
470 | 470 | preload(:project). |
|
471 | 471 | where("#{Project.table_name}.status <> ? AND #{Version.table_name}.sharing = 'system'", STATUS_ARCHIVED) |
|
472 | 472 | else |
|
473 | 473 | @shared_versions ||= begin |
|
474 | 474 | r = root? ? self : root |
|
475 | 475 | Version. |
|
476 | 476 | joins(:project). |
|
477 | 477 | preload(:project). |
|
478 | 478 | where("#{Project.table_name}.id = #{id}" + |
|
479 | 479 | " OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" + |
|
480 | 480 | " #{Version.table_name}.sharing = 'system'" + |
|
481 | 481 | " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" + |
|
482 | 482 | " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" + |
|
483 | 483 | " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" + |
|
484 | 484 | "))") |
|
485 | 485 | end |
|
486 | 486 | end |
|
487 | 487 | end |
|
488 | 488 | |
|
489 | 489 | # Returns a hash of project users grouped by role |
|
490 | 490 | def users_by_role |
|
491 | 491 | members.includes(:user, :roles).inject({}) do |h, m| |
|
492 | 492 | m.roles.each do |r| |
|
493 | 493 | h[r] ||= [] |
|
494 | 494 | h[r] << m.user |
|
495 | 495 | end |
|
496 | 496 | h |
|
497 | 497 | end |
|
498 | 498 | end |
|
499 | 499 | |
|
500 | 500 | # Deletes all project's members |
|
501 | 501 | def delete_all_members |
|
502 | 502 | me, mr = Member.table_name, MemberRole.table_name |
|
503 | 503 | self.class.connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") |
|
504 | 504 | Member.delete_all(['project_id = ?', id]) |
|
505 | 505 | end |
|
506 | 506 | |
|
507 |
# |
|
|
507 | # Return a Principal scope of users/groups issues can be assigned to | |
|
508 | 508 | def assignable_users |
|
509 | 509 | types = ['User'] |
|
510 | 510 | types << 'Group' if Setting.issue_group_assignment? |
|
511 | 511 | |
|
512 | member_principals. | |
|
513 | select {|m| types.include?(m.principal.type) && m.roles.detect(&:assignable?)}. | |
|
514 | map(&:principal). | |
|
515 | sort | |
|
512 | @assignable_users ||= Principal. | |
|
513 | active. | |
|
514 | joins(:members => :roles). | |
|
515 | where(:type => types, :members => {:project_id => id}, :roles => {:assignable => true}). | |
|
516 | uniq. | |
|
517 | sorted | |
|
516 | 518 | end |
|
517 | 519 | |
|
518 | 520 | # Returns the mail addresses of users that should be always notified on project events |
|
519 | 521 | def recipients |
|
520 | 522 | notified_users.collect {|user| user.mail} |
|
521 | 523 | end |
|
522 | 524 | |
|
523 | 525 | # Returns the users that should be notified on project events |
|
524 | 526 | def notified_users |
|
525 | 527 | # TODO: User part should be extracted to User#notify_about? |
|
526 | 528 | members.select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal} |
|
527 | 529 | end |
|
528 | 530 | |
|
529 | 531 | # Returns a scope of all custom fields enabled for project issues |
|
530 | 532 | # (explicitly associated custom fields and custom fields enabled for all projects) |
|
531 | 533 | def all_issue_custom_fields |
|
532 | 534 | @all_issue_custom_fields ||= IssueCustomField. |
|
533 | 535 | sorted. |
|
534 | 536 | where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" + |
|
535 | 537 | " FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" + |
|
536 | 538 | " WHERE cfp.project_id = ?)", true, id) |
|
537 | 539 | end |
|
538 | 540 | |
|
539 | 541 | # Returns an array of all custom fields enabled for project time entries |
|
540 | 542 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
541 | 543 | def all_time_entry_custom_fields |
|
542 | 544 | @all_time_entry_custom_fields ||= (TimeEntryCustomField.for_all + time_entry_custom_fields).uniq.sort |
|
543 | 545 | end |
|
544 | 546 | |
|
545 | 547 | def project |
|
546 | 548 | self |
|
547 | 549 | end |
|
548 | 550 | |
|
549 | 551 | def <=>(project) |
|
550 | 552 | name.downcase <=> project.name.downcase |
|
551 | 553 | end |
|
552 | 554 | |
|
553 | 555 | def to_s |
|
554 | 556 | name |
|
555 | 557 | end |
|
556 | 558 | |
|
557 | 559 | # Returns a short description of the projects (first lines) |
|
558 | 560 | def short_description(length = 255) |
|
559 | 561 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description |
|
560 | 562 | end |
|
561 | 563 | |
|
562 | 564 | def css_classes |
|
563 | 565 | s = 'project' |
|
564 | 566 | s << ' root' if root? |
|
565 | 567 | s << ' child' if child? |
|
566 | 568 | s << (leaf? ? ' leaf' : ' parent') |
|
567 | 569 | unless active? |
|
568 | 570 | if archived? |
|
569 | 571 | s << ' archived' |
|
570 | 572 | else |
|
571 | 573 | s << ' closed' |
|
572 | 574 | end |
|
573 | 575 | end |
|
574 | 576 | s |
|
575 | 577 | end |
|
576 | 578 | |
|
577 | 579 | # The earliest start date of a project, based on it's issues and versions |
|
578 | 580 | def start_date |
|
579 | 581 | @start_date ||= [ |
|
580 | 582 | issues.minimum('start_date'), |
|
581 | 583 | shared_versions.minimum('effective_date'), |
|
582 | 584 | Issue.fixed_version(shared_versions).minimum('start_date') |
|
583 | 585 | ].compact.min |
|
584 | 586 | end |
|
585 | 587 | |
|
586 | 588 | # The latest due date of an issue or version |
|
587 | 589 | def due_date |
|
588 | 590 | @due_date ||= [ |
|
589 | 591 | issues.maximum('due_date'), |
|
590 | 592 | shared_versions.maximum('effective_date'), |
|
591 | 593 | Issue.fixed_version(shared_versions).maximum('due_date') |
|
592 | 594 | ].compact.max |
|
593 | 595 | end |
|
594 | 596 | |
|
595 | 597 | def overdue? |
|
596 | 598 | active? && !due_date.nil? && (due_date < Date.today) |
|
597 | 599 | end |
|
598 | 600 | |
|
599 | 601 | # Returns the percent completed for this project, based on the |
|
600 | 602 | # progress on it's versions. |
|
601 | 603 | def completed_percent(options={:include_subprojects => false}) |
|
602 | 604 | if options.delete(:include_subprojects) |
|
603 | 605 | total = self_and_descendants.collect(&:completed_percent).sum |
|
604 | 606 | |
|
605 | 607 | total / self_and_descendants.count |
|
606 | 608 | else |
|
607 | 609 | if versions.count > 0 |
|
608 | 610 | total = versions.collect(&:completed_percent).sum |
|
609 | 611 | |
|
610 | 612 | total / versions.count |
|
611 | 613 | else |
|
612 | 614 | 100 |
|
613 | 615 | end |
|
614 | 616 | end |
|
615 | 617 | end |
|
616 | 618 | |
|
617 | 619 | # Return true if this project allows to do the specified action. |
|
618 | 620 | # action can be: |
|
619 | 621 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
620 | 622 | # * a permission Symbol (eg. :edit_project) |
|
621 | 623 | def allows_to?(action) |
|
622 | 624 | if archived? |
|
623 | 625 | # No action allowed on archived projects |
|
624 | 626 | return false |
|
625 | 627 | end |
|
626 | 628 | unless active? || Redmine::AccessControl.read_action?(action) |
|
627 | 629 | # No write action allowed on closed projects |
|
628 | 630 | return false |
|
629 | 631 | end |
|
630 | 632 | # No action allowed on disabled modules |
|
631 | 633 | if action.is_a? Hash |
|
632 | 634 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
633 | 635 | else |
|
634 | 636 | allowed_permissions.include? action |
|
635 | 637 | end |
|
636 | 638 | end |
|
637 | 639 | |
|
638 | 640 | # Return the enabled module with the given name |
|
639 | 641 | # or nil if the module is not enabled for the project |
|
640 | 642 | def enabled_module(name) |
|
641 | 643 | name = name.to_s |
|
642 | 644 | enabled_modules.detect {|m| m.name == name} |
|
643 | 645 | end |
|
644 | 646 | |
|
645 | 647 | # Return true if the module with the given name is enabled |
|
646 | 648 | def module_enabled?(name) |
|
647 | 649 | enabled_module(name).present? |
|
648 | 650 | end |
|
649 | 651 | |
|
650 | 652 | def enabled_module_names=(module_names) |
|
651 | 653 | if module_names && module_names.is_a?(Array) |
|
652 | 654 | module_names = module_names.collect(&:to_s).reject(&:blank?) |
|
653 | 655 | self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)} |
|
654 | 656 | else |
|
655 | 657 | enabled_modules.clear |
|
656 | 658 | end |
|
657 | 659 | end |
|
658 | 660 | |
|
659 | 661 | # Returns an array of the enabled modules names |
|
660 | 662 | def enabled_module_names |
|
661 | 663 | enabled_modules.collect(&:name) |
|
662 | 664 | end |
|
663 | 665 | |
|
664 | 666 | # Enable a specific module |
|
665 | 667 | # |
|
666 | 668 | # Examples: |
|
667 | 669 | # project.enable_module!(:issue_tracking) |
|
668 | 670 | # project.enable_module!("issue_tracking") |
|
669 | 671 | def enable_module!(name) |
|
670 | 672 | enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name) |
|
671 | 673 | end |
|
672 | 674 | |
|
673 | 675 | # Disable a module if it exists |
|
674 | 676 | # |
|
675 | 677 | # Examples: |
|
676 | 678 | # project.disable_module!(:issue_tracking) |
|
677 | 679 | # project.disable_module!("issue_tracking") |
|
678 | 680 | # project.disable_module!(project.enabled_modules.first) |
|
679 | 681 | def disable_module!(target) |
|
680 | 682 | target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target) |
|
681 | 683 | target.destroy unless target.blank? |
|
682 | 684 | end |
|
683 | 685 | |
|
684 | 686 | safe_attributes 'name', |
|
685 | 687 | 'description', |
|
686 | 688 | 'homepage', |
|
687 | 689 | 'is_public', |
|
688 | 690 | 'identifier', |
|
689 | 691 | 'custom_field_values', |
|
690 | 692 | 'custom_fields', |
|
691 | 693 | 'tracker_ids', |
|
692 | 694 | 'issue_custom_field_ids' |
|
693 | 695 | |
|
694 | 696 | safe_attributes 'enabled_module_names', |
|
695 | 697 | :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) } |
|
696 | 698 | |
|
697 | 699 | safe_attributes 'inherit_members', |
|
698 | 700 | :if => lambda {|project, user| project.parent.nil? || project.parent.visible?(user)} |
|
699 | 701 | |
|
700 | 702 | # Returns an array of projects that are in this project's hierarchy |
|
701 | 703 | # |
|
702 | 704 | # Example: parents, children, siblings |
|
703 | 705 | def hierarchy |
|
704 | 706 | parents = project.self_and_ancestors || [] |
|
705 | 707 | descendants = project.descendants || [] |
|
706 | 708 | project_hierarchy = parents | descendants # Set union |
|
707 | 709 | end |
|
708 | 710 | |
|
709 | 711 | # Returns an auto-generated project identifier based on the last identifier used |
|
710 | 712 | def self.next_identifier |
|
711 | 713 | p = Project.order('id DESC').first |
|
712 | 714 | p.nil? ? nil : p.identifier.to_s.succ |
|
713 | 715 | end |
|
714 | 716 | |
|
715 | 717 | # Copies and saves the Project instance based on the +project+. |
|
716 | 718 | # Duplicates the source project's: |
|
717 | 719 | # * Wiki |
|
718 | 720 | # * Versions |
|
719 | 721 | # * Categories |
|
720 | 722 | # * Issues |
|
721 | 723 | # * Members |
|
722 | 724 | # * Queries |
|
723 | 725 | # |
|
724 | 726 | # Accepts an +options+ argument to specify what to copy |
|
725 | 727 | # |
|
726 | 728 | # Examples: |
|
727 | 729 | # project.copy(1) # => copies everything |
|
728 | 730 | # project.copy(1, :only => 'members') # => copies members only |
|
729 | 731 | # project.copy(1, :only => ['members', 'versions']) # => copies members and versions |
|
730 | 732 | def copy(project, options={}) |
|
731 | 733 | project = project.is_a?(Project) ? project : Project.find(project) |
|
732 | 734 | |
|
733 | 735 | to_be_copied = %w(wiki versions issue_categories issues members queries boards) |
|
734 | 736 | to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil? |
|
735 | 737 | |
|
736 | 738 | Project.transaction do |
|
737 | 739 | if save |
|
738 | 740 | reload |
|
739 | 741 | to_be_copied.each do |name| |
|
740 | 742 | send "copy_#{name}", project |
|
741 | 743 | end |
|
742 | 744 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) |
|
743 | 745 | save |
|
744 | 746 | end |
|
745 | 747 | end |
|
746 | 748 | true |
|
747 | 749 | end |
|
748 | 750 | |
|
749 | 751 | # Returns a new unsaved Project instance with attributes copied from +project+ |
|
750 | 752 | def self.copy_from(project) |
|
751 | 753 | project = project.is_a?(Project) ? project : Project.find(project) |
|
752 | 754 | # clear unique attributes |
|
753 | 755 | attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt') |
|
754 | 756 | copy = Project.new(attributes) |
|
755 | 757 | copy.enabled_modules = project.enabled_modules |
|
756 | 758 | copy.trackers = project.trackers |
|
757 | 759 | copy.custom_values = project.custom_values.collect {|v| v.clone} |
|
758 | 760 | copy.issue_custom_fields = project.issue_custom_fields |
|
759 | 761 | copy |
|
760 | 762 | end |
|
761 | 763 | |
|
762 | 764 | # Yields the given block for each project with its level in the tree |
|
763 | 765 | def self.project_tree(projects, &block) |
|
764 | 766 | ancestors = [] |
|
765 | 767 | projects.sort_by(&:lft).each do |project| |
|
766 | 768 | while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) |
|
767 | 769 | ancestors.pop |
|
768 | 770 | end |
|
769 | 771 | yield project, ancestors.size |
|
770 | 772 | ancestors << project |
|
771 | 773 | end |
|
772 | 774 | end |
|
773 | 775 | |
|
774 | 776 | private |
|
775 | 777 | |
|
776 | 778 | def after_parent_changed(parent_was) |
|
777 | 779 | remove_inherited_member_roles |
|
778 | 780 | add_inherited_member_roles |
|
779 | 781 | end |
|
780 | 782 | |
|
781 | 783 | def update_inherited_members |
|
782 | 784 | if parent |
|
783 | 785 | if inherit_members? && !inherit_members_was |
|
784 | 786 | remove_inherited_member_roles |
|
785 | 787 | add_inherited_member_roles |
|
786 | 788 | elsif !inherit_members? && inherit_members_was |
|
787 | 789 | remove_inherited_member_roles |
|
788 | 790 | end |
|
789 | 791 | end |
|
790 | 792 | end |
|
791 | 793 | |
|
792 | 794 | def remove_inherited_member_roles |
|
793 | 795 | member_roles = memberships.map(&:member_roles).flatten |
|
794 | 796 | member_role_ids = member_roles.map(&:id) |
|
795 | 797 | member_roles.each do |member_role| |
|
796 | 798 | if member_role.inherited_from && !member_role_ids.include?(member_role.inherited_from) |
|
797 | 799 | member_role.destroy |
|
798 | 800 | end |
|
799 | 801 | end |
|
800 | 802 | end |
|
801 | 803 | |
|
802 | 804 | def add_inherited_member_roles |
|
803 | 805 | if inherit_members? && parent |
|
804 | 806 | parent.memberships.each do |parent_member| |
|
805 | 807 | member = Member.find_or_new(self.id, parent_member.user_id) |
|
806 | 808 | parent_member.member_roles.each do |parent_member_role| |
|
807 | 809 | member.member_roles << MemberRole.new(:role => parent_member_role.role, :inherited_from => parent_member_role.id) |
|
808 | 810 | end |
|
809 | 811 | member.save! |
|
810 | 812 | end |
|
811 | 813 | end |
|
812 | 814 | end |
|
813 | 815 | |
|
814 | 816 | # Copies wiki from +project+ |
|
815 | 817 | def copy_wiki(project) |
|
816 | 818 | # Check that the source project has a wiki first |
|
817 | 819 | unless project.wiki.nil? |
|
818 | 820 | wiki = self.wiki || Wiki.new |
|
819 | 821 | wiki.attributes = project.wiki.attributes.dup.except("id", "project_id") |
|
820 | 822 | wiki_pages_map = {} |
|
821 | 823 | project.wiki.pages.each do |page| |
|
822 | 824 | # Skip pages without content |
|
823 | 825 | next if page.content.nil? |
|
824 | 826 | new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on")) |
|
825 | 827 | new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id")) |
|
826 | 828 | new_wiki_page.content = new_wiki_content |
|
827 | 829 | wiki.pages << new_wiki_page |
|
828 | 830 | wiki_pages_map[page.id] = new_wiki_page |
|
829 | 831 | end |
|
830 | 832 | |
|
831 | 833 | self.wiki = wiki |
|
832 | 834 | wiki.save |
|
833 | 835 | # Reproduce page hierarchy |
|
834 | 836 | project.wiki.pages.each do |page| |
|
835 | 837 | if page.parent_id && wiki_pages_map[page.id] |
|
836 | 838 | wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id] |
|
837 | 839 | wiki_pages_map[page.id].save |
|
838 | 840 | end |
|
839 | 841 | end |
|
840 | 842 | end |
|
841 | 843 | end |
|
842 | 844 | |
|
843 | 845 | # Copies versions from +project+ |
|
844 | 846 | def copy_versions(project) |
|
845 | 847 | project.versions.each do |version| |
|
846 | 848 | new_version = Version.new |
|
847 | 849 | new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on") |
|
848 | 850 | self.versions << new_version |
|
849 | 851 | end |
|
850 | 852 | end |
|
851 | 853 | |
|
852 | 854 | # Copies issue categories from +project+ |
|
853 | 855 | def copy_issue_categories(project) |
|
854 | 856 | project.issue_categories.each do |issue_category| |
|
855 | 857 | new_issue_category = IssueCategory.new |
|
856 | 858 | new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id") |
|
857 | 859 | self.issue_categories << new_issue_category |
|
858 | 860 | end |
|
859 | 861 | end |
|
860 | 862 | |
|
861 | 863 | # Copies issues from +project+ |
|
862 | 864 | def copy_issues(project) |
|
863 | 865 | # Stores the source issue id as a key and the copied issues as the |
|
864 | 866 | # value. Used to map the two together for issue relations. |
|
865 | 867 | issues_map = {} |
|
866 | 868 | |
|
867 | 869 | # Store status and reopen locked/closed versions |
|
868 | 870 | version_statuses = versions.reject(&:open?).map {|version| [version, version.status]} |
|
869 | 871 | version_statuses.each do |version, status| |
|
870 | 872 | version.update_attribute :status, 'open' |
|
871 | 873 | end |
|
872 | 874 | |
|
873 | 875 | # Get issues sorted by root_id, lft so that parent issues |
|
874 | 876 | # get copied before their children |
|
875 | 877 | project.issues.reorder('root_id, lft').each do |issue| |
|
876 | 878 | new_issue = Issue.new |
|
877 | 879 | new_issue.copy_from(issue, :subtasks => false, :link => false) |
|
878 | 880 | new_issue.project = self |
|
879 | 881 | # Changing project resets the custom field values |
|
880 | 882 | # TODO: handle this in Issue#project= |
|
881 | 883 | new_issue.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
882 | 884 | # Reassign fixed_versions by name, since names are unique per project |
|
883 | 885 | if issue.fixed_version && issue.fixed_version.project == project |
|
884 | 886 | new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name} |
|
885 | 887 | end |
|
886 | 888 | # Reassign the category by name, since names are unique per project |
|
887 | 889 | if issue.category |
|
888 | 890 | new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name} |
|
889 | 891 | end |
|
890 | 892 | # Parent issue |
|
891 | 893 | if issue.parent_id |
|
892 | 894 | if copied_parent = issues_map[issue.parent_id] |
|
893 | 895 | new_issue.parent_issue_id = copied_parent.id |
|
894 | 896 | end |
|
895 | 897 | end |
|
896 | 898 | |
|
897 | 899 | self.issues << new_issue |
|
898 | 900 | if new_issue.new_record? |
|
899 | 901 | logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info |
|
900 | 902 | else |
|
901 | 903 | issues_map[issue.id] = new_issue unless new_issue.new_record? |
|
902 | 904 | end |
|
903 | 905 | end |
|
904 | 906 | |
|
905 | 907 | # Restore locked/closed version statuses |
|
906 | 908 | version_statuses.each do |version, status| |
|
907 | 909 | version.update_attribute :status, status |
|
908 | 910 | end |
|
909 | 911 | |
|
910 | 912 | # Relations after in case issues related each other |
|
911 | 913 | project.issues.each do |issue| |
|
912 | 914 | new_issue = issues_map[issue.id] |
|
913 | 915 | unless new_issue |
|
914 | 916 | # Issue was not copied |
|
915 | 917 | next |
|
916 | 918 | end |
|
917 | 919 | |
|
918 | 920 | # Relations |
|
919 | 921 | issue.relations_from.each do |source_relation| |
|
920 | 922 | new_issue_relation = IssueRelation.new |
|
921 | 923 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") |
|
922 | 924 | new_issue_relation.issue_to = issues_map[source_relation.issue_to_id] |
|
923 | 925 | if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations? |
|
924 | 926 | new_issue_relation.issue_to = source_relation.issue_to |
|
925 | 927 | end |
|
926 | 928 | new_issue.relations_from << new_issue_relation |
|
927 | 929 | end |
|
928 | 930 | |
|
929 | 931 | issue.relations_to.each do |source_relation| |
|
930 | 932 | new_issue_relation = IssueRelation.new |
|
931 | 933 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") |
|
932 | 934 | new_issue_relation.issue_from = issues_map[source_relation.issue_from_id] |
|
933 | 935 | if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations? |
|
934 | 936 | new_issue_relation.issue_from = source_relation.issue_from |
|
935 | 937 | end |
|
936 | 938 | new_issue.relations_to << new_issue_relation |
|
937 | 939 | end |
|
938 | 940 | end |
|
939 | 941 | end |
|
940 | 942 | |
|
941 | 943 | # Copies members from +project+ |
|
942 | 944 | def copy_members(project) |
|
943 | 945 | # Copy users first, then groups to handle members with inherited and given roles |
|
944 | 946 | members_to_copy = [] |
|
945 | 947 | members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)} |
|
946 | 948 | members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)} |
|
947 | 949 | |
|
948 | 950 | members_to_copy.each do |member| |
|
949 | 951 | new_member = Member.new |
|
950 | 952 | new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on") |
|
951 | 953 | # only copy non inherited roles |
|
952 | 954 | # inherited roles will be added when copying the group membership |
|
953 | 955 | role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id) |
|
954 | 956 | next if role_ids.empty? |
|
955 | 957 | new_member.role_ids = role_ids |
|
956 | 958 | new_member.project = self |
|
957 | 959 | self.members << new_member |
|
958 | 960 | end |
|
959 | 961 | end |
|
960 | 962 | |
|
961 | 963 | # Copies queries from +project+ |
|
962 | 964 | def copy_queries(project) |
|
963 | 965 | project.queries.each do |query| |
|
964 | 966 | new_query = IssueQuery.new |
|
965 | 967 | new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria", "user_id", "type") |
|
966 | 968 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria |
|
967 | 969 | new_query.project = self |
|
968 | 970 | new_query.user_id = query.user_id |
|
969 | 971 | new_query.role_ids = query.role_ids if query.visibility == IssueQuery::VISIBILITY_ROLES |
|
970 | 972 | self.queries << new_query |
|
971 | 973 | end |
|
972 | 974 | end |
|
973 | 975 | |
|
974 | 976 | # Copies boards from +project+ |
|
975 | 977 | def copy_boards(project) |
|
976 | 978 | project.boards.each do |board| |
|
977 | 979 | new_board = Board.new |
|
978 | 980 | new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id") |
|
979 | 981 | new_board.project = self |
|
980 | 982 | self.boards << new_board |
|
981 | 983 | end |
|
982 | 984 | end |
|
983 | 985 | |
|
984 | 986 | def allowed_permissions |
|
985 | 987 | @allowed_permissions ||= begin |
|
986 | 988 | module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name) |
|
987 | 989 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
988 | 990 | end |
|
989 | 991 | end |
|
990 | 992 | |
|
991 | 993 | def allowed_actions |
|
992 | 994 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
993 | 995 | end |
|
994 | 996 | |
|
995 | 997 | # Returns all the active Systemwide and project specific activities |
|
996 | 998 | def active_activities |
|
997 | 999 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) |
|
998 | 1000 | |
|
999 | 1001 | if overridden_activity_ids.empty? |
|
1000 | 1002 | return TimeEntryActivity.shared.active |
|
1001 | 1003 | else |
|
1002 | 1004 | return system_activities_and_project_overrides |
|
1003 | 1005 | end |
|
1004 | 1006 | end |
|
1005 | 1007 | |
|
1006 | 1008 | # Returns all the Systemwide and project specific activities |
|
1007 | 1009 | # (inactive and active) |
|
1008 | 1010 | def all_activities |
|
1009 | 1011 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) |
|
1010 | 1012 | |
|
1011 | 1013 | if overridden_activity_ids.empty? |
|
1012 | 1014 | return TimeEntryActivity.shared |
|
1013 | 1015 | else |
|
1014 | 1016 | return system_activities_and_project_overrides(true) |
|
1015 | 1017 | end |
|
1016 | 1018 | end |
|
1017 | 1019 | |
|
1018 | 1020 | # Returns the systemwide active activities merged with the project specific overrides |
|
1019 | 1021 | def system_activities_and_project_overrides(include_inactive=false) |
|
1020 | 1022 | t = TimeEntryActivity.table_name |
|
1021 | 1023 | scope = TimeEntryActivity.where( |
|
1022 | 1024 | "(#{t}.project_id IS NULL AND #{t}.id NOT IN (?)) OR (#{t}.project_id = ?)", |
|
1023 | 1025 | time_entry_activities.map(&:parent_id), id |
|
1024 | 1026 | ) |
|
1025 | 1027 | unless include_inactive |
|
1026 | 1028 | scope = scope.active |
|
1027 | 1029 | end |
|
1028 | 1030 | scope |
|
1029 | 1031 | end |
|
1030 | 1032 | |
|
1031 | 1033 | # Archives subprojects recursively |
|
1032 | 1034 | def archive! |
|
1033 | 1035 | children.each do |subproject| |
|
1034 | 1036 | subproject.send :archive! |
|
1035 | 1037 | end |
|
1036 | 1038 | update_attribute :status, STATUS_ARCHIVED |
|
1037 | 1039 | end |
|
1038 | 1040 | |
|
1039 | 1041 | def update_position_under_parent |
|
1040 | 1042 | set_or_update_position_under(parent) |
|
1041 | 1043 | end |
|
1042 | 1044 | |
|
1043 | 1045 | public |
|
1044 | 1046 | |
|
1045 | 1047 | # Inserts/moves the project so that target's children or root projects stay alphabetically sorted |
|
1046 | 1048 | def set_or_update_position_under(target_parent) |
|
1047 | 1049 | parent_was = parent |
|
1048 | 1050 | sibs = (target_parent.nil? ? self.class.roots : target_parent.children) |
|
1049 | 1051 | to_be_inserted_before = sibs.sort_by {|c| c.name.to_s.downcase}.detect {|c| c.name.to_s.downcase > name.to_s.downcase } |
|
1050 | 1052 | |
|
1051 | 1053 | if to_be_inserted_before |
|
1052 | 1054 | move_to_left_of(to_be_inserted_before) |
|
1053 | 1055 | elsif target_parent.nil? |
|
1054 | 1056 | if sibs.empty? |
|
1055 | 1057 | # move_to_root adds the project in first (ie. left) position |
|
1056 | 1058 | move_to_root |
|
1057 | 1059 | else |
|
1058 | 1060 | move_to_right_of(sibs.last) unless self == sibs.last |
|
1059 | 1061 | end |
|
1060 | 1062 | else |
|
1061 | 1063 | # move_to_child_of adds the project in last (ie.right) position |
|
1062 | 1064 | move_to_child_of(target_parent) |
|
1063 | 1065 | end |
|
1064 | 1066 | if parent_was != target_parent |
|
1065 | 1067 | after_parent_changed(parent_was) |
|
1066 | 1068 | end |
|
1067 | 1069 | end |
|
1068 | 1070 | end |
General Comments 0
You need to be logged in to leave comments.
Login now