1
|
# redMine - project management software
|
2
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
3
|
#
|
4
|
# This program is free software; you can redistribute it and/or
|
5
|
# modify it under the terms of the GNU General Public License
|
6
|
# as published by the Free Software Foundation; either version 2
|
7
|
# of the License, or (at your option) any later version.
|
8
|
#
|
9
|
# This program is distributed in the hope that it will be useful,
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
# GNU General Public License for more details.
|
13
|
#
|
14
|
# You should have received a copy of the GNU General Public License
|
15
|
# along with this program; if not, write to the Free Software
|
16
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
17
|
|
18
|
class Issue < ActiveRecord::Base
|
19
|
belongs_to :project
|
20
|
belongs_to :tracker
|
21
|
belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
|
22
|
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
|
23
|
belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
|
24
|
belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
|
25
|
belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
|
26
|
belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
|
27
|
|
28
|
has_many :journals, :as => :journalized, :dependent => :destroy
|
29
|
has_many :time_entries, :dependent => :delete_all
|
30
|
has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
|
31
|
|
32
|
has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
|
33
|
has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
|
34
|
|
35
|
acts_as_nested_set :scope => 'root_id'
|
36
|
acts_as_attachable :after_remove => :attachment_removed
|
37
|
acts_as_customizable
|
38
|
acts_as_watchable
|
39
|
acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
|
40
|
:include => [:project, :journals],
|
41
|
# sort by id so that limited eager loading doesn't break with postgresql
|
42
|
:order_column => "#{table_name}.id"
|
43
|
acts_as_event :title => Proc.new { |o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}" },
|
44
|
:url => Proc.new { |o| {:controller => 'issues', :action => 'show', :id => o.id} },
|
45
|
:type => Proc.new { |o| 'issue' + (o.closed? ? ' closed' : '') }
|
46
|
|
47
|
acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
|
48
|
:author_key => :author_id
|
49
|
|
50
|
DONE_RATIO_OPTIONS = %w(issue_field issue_status)
|
51
|
|
52
|
attr_reader :current_journal
|
53
|
|
54
|
validates_presence_of :subject, :priority, :project, :tracker, :author, :status
|
55
|
|
56
|
validates_length_of :subject, :maximum => 255
|
57
|
validates_inclusion_of :done_ratio, :in => 0..100
|
58
|
validates_numericality_of :estimated_hours, :allow_nil => true
|
59
|
|
60
|
named_scope :visible, lambda { |*args| {:include => :project,
|
61
|
:conditions => Project.allowed_to_condition(args.first || User.current, :view_issues)} }
|
62
|
|
63
|
named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
|
64
|
|
65
|
named_scope :recently_updated, :order => "#{self.table_name}.updated_on DESC"
|
66
|
named_scope :with_limit, lambda { |limit| {:limit => limit} }
|
67
|
named_scope :on_active_project, :include => [:status, :project, :tracker],
|
68
|
:conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"]
|
69
|
|
70
|
before_create :default_assign
|
71
|
before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status
|
72
|
after_save :update_nested_set_attributes, :update_parent_attributes, :create_journal
|
73
|
after_destroy :destroy_children
|
74
|
after_destroy :update_parent_attributes
|
75
|
|
76
|
# Returns true if usr or current user is allowed to view the issue
|
77
|
def visible?(usr=nil)
|
78
|
(usr || User.current).allowed_to?(:view_issues, self.project)
|
79
|
end
|
80
|
|
81
|
def after_initialize
|
82
|
if new_record?
|
83
|
# set default values for new records only
|
84
|
self.status ||= IssueStatus.default
|
85
|
self.priority ||= IssuePriority.default
|
86
|
end
|
87
|
end
|
88
|
|
89
|
# Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
|
90
|
def available_custom_fields
|
91
|
(project && tracker) ? project.all_issue_custom_fields.select { |c| tracker.custom_fields.include? c } : []
|
92
|
end
|
93
|
|
94
|
def copy_from(arg)
|
95
|
issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
|
96
|
self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
|
97
|
self.custom_field_values = issue.custom_field_values.inject({}) { |h, v| h[v.custom_field_id] = v.value; h }
|
98
|
self.status = issue.status
|
99
|
self
|
100
|
|
101
|
end
|
102
|
|
103
|
def logger
|
104
|
RAILS_DEFAULT_LOGGER
|
105
|
end
|
106
|
|
107
|
# Moves/copies an issue to a new project and tracker
|
108
|
# Returns the moved/copied issue on success, false on failure
|
109
|
def move_to_project(*args)
|
110
|
ret = Issue.transaction do
|
111
|
move_to_project_without_transaction(*args) || raise(ActiveRecord::Rollback)
|
112
|
end || false
|
113
|
end
|
114
|
|
115
|
def move_to_project_without_transaction(new_project, new_tracker = nil, options = {})
|
116
|
options ||= {}
|
117
|
issue = options[:copy] ? self.class.new.copy_from(self) : self
|
118
|
|
119
|
logger.info "starting move_to_project_witouth_trans"
|
120
|
logger.info self.id
|
121
|
|
122
|
if new_project && issue.project_id != new_project.id
|
123
|
# delete issue relations
|
124
|
unless Setting.cross_project_issue_relations?
|
125
|
issue.relations_from.clear
|
126
|
issue.relations_to.clear
|
127
|
end
|
128
|
# issue is moved to another project
|
129
|
# reassign to the category with same name if any
|
130
|
new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
|
131
|
issue.category = new_category
|
132
|
# Keep the fixed_version if it's still valid in the new_project
|
133
|
unless new_project.shared_versions.include?(issue.fixed_version)
|
134
|
issue.fixed_version = nil
|
135
|
end
|
136
|
issue.project = new_project
|
137
|
if issue.parent && issue.parent.project_id != issue.project_id
|
138
|
issue.parent_issue_id = nil
|
139
|
end
|
140
|
end
|
141
|
if new_tracker
|
142
|
issue.tracker = new_tracker
|
143
|
issue.reset_custom_values!
|
144
|
end
|
145
|
if options[:copy]
|
146
|
issue.custom_field_values = self.custom_field_values.inject({}) { |h, v| h[v.custom_field_id] = v.value; h }
|
147
|
issue.status = if options[:attributes] && options[:attributes][:status_id]
|
148
|
IssueStatus.find_by_id(options[:attributes][:status_id])
|
149
|
else
|
150
|
self.status
|
151
|
end
|
152
|
end
|
153
|
# Allow bulk setting of attributes on the issue
|
154
|
if options[:attributes]
|
155
|
issue.attributes = options[:attributes]
|
156
|
end
|
157
|
if issue.save
|
158
|
unless options[:copy]
|
159
|
# Manually update project_id on related time entries
|
160
|
TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
|
161
|
|
162
|
issue.children.each do |child|
|
163
|
unless child.move_to_project_without_transaction(new_project)
|
164
|
# Move failed and transaction was rollback'd
|
165
|
return false
|
166
|
end
|
167
|
end
|
168
|
end
|
169
|
#Copy Function Changes to support copying subtasks with parent tasks. (Call the copy_children Function)
|
170
|
if options[:copy]
|
171
|
copy_children(self, issue.id, new_project)
|
172
|
end
|
173
|
#End Changes
|
174
|
else
|
175
|
return false
|
176
|
end
|
177
|
|
178
|
issue
|
179
|
end
|
180
|
#Copy_Children Function to support copying subtasks with parent tasks.
|
181
|
def copy_children(issue, parent_issue_id, new_project)
|
182
|
issue.children.each do |childissue|
|
183
|
child_issue = Issue.new
|
184
|
child_issue.copy_from(childissue)
|
185
|
child_issue.parent_issue_id = parent_issue_id
|
186
|
|
187
|
child_issue.project = new_project
|
188
|
if child_issue.save
|
189
|
unless childissue.copy_children(childissue, child_issue.id,new_project)
|
190
|
# Move failed and transaction was rollback'd
|
191
|
return false
|
192
|
end
|
193
|
else
|
194
|
logger.error "failed to save child issue"
|
195
|
end
|
196
|
end
|
197
|
end
|
198
|
#End New Copy Function
|
199
|
|
200
|
def status_id=(sid)
|
201
|
self.status = nil
|
202
|
write_attribute(:status_id, sid)
|
203
|
end
|
204
|
|
205
|
def priority_id=(pid)
|
206
|
self.priority = nil
|
207
|
write_attribute(:priority_id, pid)
|
208
|
end
|
209
|
|
210
|
def tracker_id=(tid)
|
211
|
self.tracker = nil
|
212
|
result = write_attribute(:tracker_id, tid)
|
213
|
@custom_field_values = nil
|
214
|
result
|
215
|
end
|
216
|
|
217
|
# Overrides attributes= so that tracker_id gets assigned first
|
218
|
def attributes_with_tracker_first=(new_attributes, *args)
|
219
|
return if new_attributes.nil?
|
220
|
new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id]
|
221
|
if new_tracker_id
|
222
|
self.tracker_id = new_tracker_id
|
223
|
end
|
224
|
send :attributes_without_tracker_first=, new_attributes, *args
|
225
|
end
|
226
|
|
227
|
# Do not redefine alias chain on reload (see #4838)
|
228
|
alias_method_chain(:attributes=, :tracker_first) unless method_defined?(:attributes_without_tracker_first=)
|
229
|
|
230
|
def estimated_hours=(h)
|
231
|
write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
|
232
|
end
|
233
|
|
234
|
SAFE_ATTRIBUTES = %w(
|
235
|
tracker_id
|
236
|
status_id
|
237
|
parent_issue_id
|
238
|
category_id
|
239
|
assigned_to_id
|
240
|
priority_id
|
241
|
fixed_version_id
|
242
|
subject
|
243
|
description
|
244
|
start_date
|
245
|
due_date
|
246
|
done_ratio
|
247
|
estimated_hours
|
248
|
custom_field_values
|
249
|
lock_version
|
250
|
) unless const_defined?(:SAFE_ATTRIBUTES)
|
251
|
|
252
|
# Safely sets attributes
|
253
|
# Should be called from controllers instead of #attributes=
|
254
|
# attr_accessible is too rough because we still want things like
|
255
|
# Issue.new(:project => foo) to work
|
256
|
# TODO: move workflow/permission checks from controllers to here
|
257
|
def safe_attributes=(attrs, user=User.current)
|
258
|
return if attrs.nil?
|
259
|
attrs = attrs.reject { |k, v| !SAFE_ATTRIBUTES.include?(k) }
|
260
|
if attrs['status_id']
|
261
|
unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i)
|
262
|
attrs.delete('status_id')
|
263
|
end
|
264
|
end
|
265
|
|
266
|
unless leaf?
|
267
|
attrs.reject! { |k, v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k) }
|
268
|
end
|
269
|
|
270
|
if attrs.has_key?('parent_issue_id')
|
271
|
if !user.allowed_to?(:manage_subtasks, project)
|
272
|
attrs.delete('parent_issue_id')
|
273
|
elsif !attrs['parent_issue_id'].blank?
|
274
|
attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'])
|
275
|
end
|
276
|
end
|
277
|
|
278
|
self.attributes = attrs
|
279
|
end
|
280
|
|
281
|
def done_ratio
|
282
|
if Issue.use_status_for_done_ratio? && status && status.default_done_ratio?
|
283
|
status.default_done_ratio
|
284
|
else
|
285
|
read_attribute(:done_ratio)
|
286
|
end
|
287
|
end
|
288
|
|
289
|
def self.use_status_for_done_ratio?
|
290
|
Setting.issue_done_ratio == 'issue_status'
|
291
|
end
|
292
|
|
293
|
def self.use_field_for_done_ratio?
|
294
|
Setting.issue_done_ratio == 'issue_field'
|
295
|
end
|
296
|
|
297
|
def validate
|
298
|
if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
|
299
|
errors.add :due_date, :not_a_date
|
300
|
end
|
301
|
|
302
|
if self.due_date and self.start_date and self.due_date < self.start_date
|
303
|
errors.add :due_date, :greater_than_start_date
|
304
|
end
|
305
|
|
306
|
if start_date && soonest_start && start_date < soonest_start
|
307
|
errors.add :start_date, :invalid
|
308
|
end
|
309
|
|
310
|
if fixed_version
|
311
|
if !assignable_versions.include?(fixed_version)
|
312
|
errors.add :fixed_version_id, :inclusion
|
313
|
elsif reopened? && fixed_version.closed?
|
314
|
errors.add_to_base I18n.t(:error_can_not_reopen_issue_on_closed_version)
|
315
|
end
|
316
|
end
|
317
|
|
318
|
# Checks that the issue can not be added/moved to a disabled tracker
|
319
|
if project && (tracker_id_changed? || project_id_changed?)
|
320
|
unless project.trackers.include?(tracker)
|
321
|
errors.add :tracker_id, :inclusion
|
322
|
end
|
323
|
end
|
324
|
|
325
|
# Checks parent issue assignment
|
326
|
if @parent_issue
|
327
|
if @parent_issue.project_id != project_id
|
328
|
errors.add :parent_issue_id, :not_same_project
|
329
|
elsif !new_record?
|
330
|
# moving an existing issue
|
331
|
if @parent_issue.root_id != root_id
|
332
|
# we can always move to another tree
|
333
|
elsif move_possible?(@parent_issue)
|
334
|
# move accepted inside tree
|
335
|
else
|
336
|
errors.add :parent_issue_id, :not_a_valid_parent
|
337
|
end
|
338
|
end
|
339
|
end
|
340
|
end
|
341
|
|
342
|
# Set the done_ratio using the status if that setting is set. This will keep the done_ratios
|
343
|
# even if the user turns off the setting later
|
344
|
def update_done_ratio_from_issue_status
|
345
|
if Issue.use_status_for_done_ratio? && status && status.default_done_ratio?
|
346
|
self.done_ratio = status.default_done_ratio
|
347
|
end
|
348
|
end
|
349
|
|
350
|
def init_journal(user, notes = "")
|
351
|
@current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
|
352
|
@issue_before_change = self.clone
|
353
|
@issue_before_change.status = self.status
|
354
|
@custom_values_before_change = {}
|
355
|
self.custom_values.each { |c| @custom_values_before_change.store c.custom_field_id, c.value }
|
356
|
# Make sure updated_on is updated when adding a note.
|
357
|
updated_on_will_change!
|
358
|
@current_journal
|
359
|
end
|
360
|
|
361
|
# Return true if the issue is closed, otherwise false
|
362
|
def closed?
|
363
|
self.status.is_closed?
|
364
|
end
|
365
|
|
366
|
# Return true if the issue is being reopened
|
367
|
def reopened?
|
368
|
if !new_record? && status_id_changed?
|
369
|
status_was = IssueStatus.find_by_id(status_id_was)
|
370
|
status_new = IssueStatus.find_by_id(status_id)
|
371
|
if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
|
372
|
return true
|
373
|
end
|
374
|
end
|
375
|
false
|
376
|
end
|
377
|
|
378
|
# Return true if the issue is being closed
|
379
|
def closing?
|
380
|
if !new_record? && status_id_changed?
|
381
|
status_was = IssueStatus.find_by_id(status_id_was)
|
382
|
status_new = IssueStatus.find_by_id(status_id)
|
383
|
if status_was && status_new && !status_was.is_closed? && status_new.is_closed?
|
384
|
return true
|
385
|
end
|
386
|
end
|
387
|
false
|
388
|
end
|
389
|
|
390
|
# Returns true if the issue is overdue
|
391
|
def overdue?
|
392
|
!due_date.nil? && (due_date < Date.today) && !status.is_closed?
|
393
|
end
|
394
|
|
395
|
# Users the issue can be assigned to
|
396
|
def assignable_users
|
397
|
project.assignable_users
|
398
|
end
|
399
|
|
400
|
# Versions that the issue can be assigned to
|
401
|
def assignable_versions
|
402
|
@assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
|
403
|
end
|
404
|
|
405
|
# Returns true if this issue is blocked by another issue that is still open
|
406
|
def blocked?
|
407
|
!relations_to.detect { |ir| ir.relation_type == 'blocks' && !ir.issue_from.closed? }.nil?
|
408
|
end
|
409
|
|
410
|
# Returns an array of status that user is able to apply
|
411
|
def new_statuses_allowed_to(user, include_default=false)
|
412
|
statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker)
|
413
|
statuses << status unless statuses.empty?
|
414
|
statuses << IssueStatus.default if include_default
|
415
|
statuses = statuses.uniq.sort
|
416
|
blocked? ? statuses.reject { |s| s.is_closed? } : statuses
|
417
|
end
|
418
|
|
419
|
# Returns the mail adresses of users that should be notified
|
420
|
def recipients
|
421
|
notified = project.notified_users
|
422
|
# Author and assignee are always notified unless they have been locked
|
423
|
notified << author if author && author.active?
|
424
|
notified << assigned_to if assigned_to && assigned_to.active?
|
425
|
notified.uniq!
|
426
|
# Remove users that can not view the issue
|
427
|
notified.reject! { |user| !visible?(user) }
|
428
|
notified.collect(&:mail)
|
429
|
end
|
430
|
|
431
|
# Returns the total number of hours spent on this issue and its descendants
|
432
|
#
|
433
|
# Example:
|
434
|
# spent_hours => 0.0
|
435
|
# spent_hours => 50.2
|
436
|
def spent_hours
|
437
|
@spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", :include => :time_entries).to_f || 0.0
|
438
|
end
|
439
|
|
440
|
def relations
|
441
|
(relations_from + relations_to).sort
|
442
|
end
|
443
|
|
444
|
def all_dependent_issues
|
445
|
dependencies = []
|
446
|
relations_from.each do |relation|
|
447
|
dependencies << relation.issue_to
|
448
|
dependencies += relation.issue_to.all_dependent_issues
|
449
|
end
|
450
|
dependencies
|
451
|
end
|
452
|
|
453
|
# Returns an array of issues that duplicate this one
|
454
|
def duplicates
|
455
|
relations_to.select { |r| r.relation_type == IssueRelation::TYPE_DUPLICATES }.collect { |r| r.issue_from }
|
456
|
end
|
457
|
|
458
|
# Returns the due date or the target due date if any
|
459
|
# Used on gantt chart
|
460
|
def due_before
|
461
|
due_date || (fixed_version ? fixed_version.effective_date : nil)
|
462
|
end
|
463
|
|
464
|
# Returns the time scheduled for this issue.
|
465
|
#
|
466
|
# Example:
|
467
|
# Start Date: 2/26/09, End Date: 3/04/09
|
468
|
# duration => 6
|
469
|
def duration
|
470
|
(start_date && due_date) ? due_date - start_date : 0
|
471
|
end
|
472
|
|
473
|
def soonest_start
|
474
|
@soonest_start ||= (
|
475
|
relations_to.collect { |relation| relation.successor_soonest_start } +
|
476
|
ancestors.collect(&:soonest_start)
|
477
|
).compact.max
|
478
|
end
|
479
|
|
480
|
def reschedule_after(date)
|
481
|
return if date.nil?
|
482
|
if leaf?
|
483
|
if start_date.nil? || start_date < date
|
484
|
self.start_date, self.due_date = date, date + duration
|
485
|
save
|
486
|
end
|
487
|
else
|
488
|
leaves.each do |leaf|
|
489
|
leaf.reschedule_after(date)
|
490
|
end
|
491
|
end
|
492
|
end
|
493
|
|
494
|
def <=>(issue)
|
495
|
if issue.nil?
|
496
|
-1
|
497
|
elsif root_id != issue.root_id
|
498
|
(root_id || 0) <=> (issue.root_id || 0)
|
499
|
else
|
500
|
(lft || 0) <=> (issue.lft || 0)
|
501
|
end
|
502
|
end
|
503
|
|
504
|
def to_s
|
505
|
"#{tracker} ##{id}: #{subject}"
|
506
|
end
|
507
|
|
508
|
# Returns a string of css classes that apply to the issue
|
509
|
def css_classes
|
510
|
s = "issue status-#{status.position} priority-#{priority.position}"
|
511
|
s << ' closed' if closed?
|
512
|
s << ' overdue' if overdue?
|
513
|
s << ' created-by-me' if User.current.logged? && author_id == User.current.id
|
514
|
s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
|
515
|
s
|
516
|
end
|
517
|
|
518
|
# Saves an issue, time_entry, attachments, and a journal from the parameters
|
519
|
# Returns false if save fails
|
520
|
def save_issue_with_child_records(params, existing_time_entry=nil)
|
521
|
Issue.transaction do
|
522
|
if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
|
523
|
@time_entry = existing_time_entry || TimeEntry.new
|
524
|
@time_entry.project = project
|
525
|
@time_entry.issue = self
|
526
|
@time_entry.user = User.current
|
527
|
@time_entry.spent_on = Date.today
|
528
|
@time_entry.attributes = params[:time_entry]
|
529
|
self.time_entries << @time_entry
|
530
|
end
|
531
|
|
532
|
if valid?
|
533
|
attachments = Attachment.attach_files(self, params[:attachments])
|
534
|
|
535
|
attachments[:files].each { |a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename) }
|
536
|
# TODO: Rename hook
|
537
|
Redmine::Hook.call_hook(:controller_issues_edit_before_save, {:params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
|
538
|
begin
|
539
|
if save
|
540
|
# TODO: Rename hook
|
541
|
Redmine::Hook.call_hook(:controller_issues_edit_after_save, {:params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
|
542
|
else
|
543
|
raise ActiveRecord::Rollback
|
544
|
end
|
545
|
rescue ActiveRecord::StaleObjectError
|
546
|
attachments[:files].each(&:destroy)
|
547
|
errors.add_to_base l(:notice_locking_conflict)
|
548
|
raise ActiveRecord::Rollback
|
549
|
end
|
550
|
end
|
551
|
end
|
552
|
end
|
553
|
|
554
|
# Unassigns issues from +version+ if it's no longer shared with issue's project
|
555
|
def self.update_versions_from_sharing_change(version)
|
556
|
# Update issues assigned to the version
|
557
|
update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
|
558
|
end
|
559
|
|
560
|
# Unassigns issues from versions that are no longer shared
|
561
|
# after +project+ was moved
|
562
|
def self.update_versions_from_hierarchy_change(project)
|
563
|
moved_project_ids = project.self_and_descendants.reload.collect(&:id)
|
564
|
# Update issues of the moved projects and issues assigned to a version of a moved project
|
565
|
Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
|
566
|
end
|
567
|
|
568
|
def parent_issue_id=(arg)
|
569
|
parent_issue_id = arg.blank? ? nil : arg.to_i
|
570
|
if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id)
|
571
|
@parent_issue.id
|
572
|
else
|
573
|
@parent_issue = nil
|
574
|
nil
|
575
|
end
|
576
|
end
|
577
|
|
578
|
def parent_issue_id
|
579
|
if instance_variable_defined? :@parent_issue
|
580
|
@parent_issue.nil? ? nil : @parent_issue.id
|
581
|
else
|
582
|
parent_id
|
583
|
end
|
584
|
end
|
585
|
|
586
|
# Extracted from the ReportsController.
|
587
|
def self.by_tracker(project)
|
588
|
count_and_group_by(:project => project,
|
589
|
:field => 'tracker_id',
|
590
|
:joins => Tracker.table_name)
|
591
|
end
|
592
|
|
593
|
def self.by_version(project)
|
594
|
count_and_group_by(:project => project,
|
595
|
:field => 'fixed_version_id',
|
596
|
:joins => Version.table_name)
|
597
|
end
|
598
|
|
599
|
def self.by_priority(project)
|
600
|
count_and_group_by(:project => project,
|
601
|
:field => 'priority_id',
|
602
|
:joins => IssuePriority.table_name)
|
603
|
end
|
604
|
|
605
|
def self.by_category(project)
|
606
|
count_and_group_by(:project => project,
|
607
|
:field => 'category_id',
|
608
|
:joins => IssueCategory.table_name)
|
609
|
end
|
610
|
|
611
|
def self.by_assigned_to(project)
|
612
|
count_and_group_by(:project => project,
|
613
|
:field => 'assigned_to_id',
|
614
|
:joins => User.table_name)
|
615
|
end
|
616
|
|
617
|
def self.by_author(project)
|
618
|
count_and_group_by(:project => project,
|
619
|
:field => 'author_id',
|
620
|
:joins => User.table_name)
|
621
|
end
|
622
|
|
623
|
def self.by_subproject(project)
|
624
|
ActiveRecord::Base.connection.select_all("select s.id as status_id,
|
625
|
s.is_closed as closed,
|
626
|
i.project_id as project_id,
|
627
|
count(i.id) as total
|
628
|
from
|
629
|
#{Issue.table_name} i, #{IssueStatus.table_name} s
|
630
|
where
|
631
|
i.status_id=s.id
|
632
|
and i.project_id IN (#{project.descendants.active.collect { |p| p.id }.join(',')})
|
633
|
group by s.id, s.is_closed, i.project_id") if project.descendants.active.any?
|
634
|
end
|
635
|
|
636
|
# End ReportsController extraction
|
637
|
|
638
|
# Returns an array of projects that current user can move issues to
|
639
|
def self.allowed_target_projects_on_move
|
640
|
projects = []
|
641
|
if User.current.admin?
|
642
|
# admin is allowed to move issues to any active (visible) project
|
643
|
projects = Project.visible.all
|
644
|
elsif User.current.logged?
|
645
|
if Role.non_member.allowed_to?(:move_issues)
|
646
|
projects = Project.visible.all
|
647
|
else
|
648
|
User.current.memberships.each { |m| projects << m.project if m.roles.detect { |r| r.allowed_to?(:move_issues) } }
|
649
|
end
|
650
|
end
|
651
|
projects
|
652
|
end
|
653
|
|
654
|
private
|
655
|
|
656
|
def update_nested_set_attributes
|
657
|
if root_id.nil?
|
658
|
# issue was just created
|
659
|
self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
|
660
|
set_default_left_and_right
|
661
|
Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
|
662
|
if @parent_issue
|
663
|
move_to_child_of(@parent_issue)
|
664
|
end
|
665
|
reload
|
666
|
elsif parent_issue_id != parent_id
|
667
|
former_parent_id = parent_id
|
668
|
# moving an existing issue
|
669
|
if @parent_issue && @parent_issue.root_id == root_id
|
670
|
# inside the same tree
|
671
|
move_to_child_of(@parent_issue)
|
672
|
else
|
673
|
# to another tree
|
674
|
unless root?
|
675
|
move_to_right_of(root)
|
676
|
reload
|
677
|
end
|
678
|
old_root_id = root_id
|
679
|
self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
|
680
|
target_maxright = nested_set_scope.maximum(right_column_name) || 0
|
681
|
offset = target_maxright + 1 - lft
|
682
|
Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
|
683
|
["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
|
684
|
self[left_column_name] = lft + offset
|
685
|
self[right_column_name] = rgt + offset
|
686
|
if @parent_issue
|
687
|
move_to_child_of(@parent_issue)
|
688
|
end
|
689
|
end
|
690
|
reload
|
691
|
# delete invalid relations of all descendants
|
692
|
self_and_descendants.each do |issue|
|
693
|
issue.relations.each do |relation|
|
694
|
relation.destroy unless relation.valid?
|
695
|
end
|
696
|
end
|
697
|
# update former parent
|
698
|
recalculate_attributes_for(former_parent_id) if former_parent_id
|
699
|
end
|
700
|
remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
|
701
|
end
|
702
|
|
703
|
def update_parent_attributes
|
704
|
recalculate_attributes_for(parent_id) if parent_id
|
705
|
end
|
706
|
|
707
|
def recalculate_attributes_for(issue_id)
|
708
|
if issue_id && p = Issue.find_by_id(issue_id)
|
709
|
# priority = highest priority of children
|
710
|
if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :include => :priority)
|
711
|
p.priority = IssuePriority.find_by_position(priority_position)
|
712
|
end
|
713
|
|
714
|
# start/due dates = lowest/highest dates of children
|
715
|
p.start_date = p.children.minimum(:start_date)
|
716
|
p.due_date = p.children.maximum(:due_date)
|
717
|
if p.start_date && p.due_date && p.due_date < p.start_date
|
718
|
p.start_date, p.due_date = p.due_date, p.start_date
|
719
|
end
|
720
|
|
721
|
# done ratio = weighted average ratio of leaves
|
722
|
unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio?
|
723
|
leaves_count = p.leaves.count
|
724
|
if leaves_count > 0
|
725
|
average = p.leaves.average(:estimated_hours).to_f
|
726
|
if average == 0
|
727
|
average = 1
|
728
|
end
|
729
|
done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :include => :status).to_f
|
730
|
progress = done / (average * leaves_count)
|
731
|
p.done_ratio = progress.round
|
732
|
end
|
733
|
end
|
734
|
|
735
|
# estimate = sum of leaves estimates
|
736
|
p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
|
737
|
p.estimated_hours = nil if p.estimated_hours == 0.0
|
738
|
|
739
|
# ancestors will be recursively updated
|
740
|
p.save(false)
|
741
|
end
|
742
|
end
|
743
|
|
744
|
def destroy_children
|
745
|
unless leaf?
|
746
|
children.each do |child|
|
747
|
child.destroy
|
748
|
end
|
749
|
end
|
750
|
end
|
751
|
|
752
|
# Update issues so their versions are not pointing to a
|
753
|
# fixed_version that is not shared with the issue's project
|
754
|
def self.update_versions(conditions=nil)
|
755
|
# Only need to update issues with a fixed_version from
|
756
|
# a different project and that is not systemwide shared
|
757
|
Issue.all(:conditions => merge_conditions("#{Issue.table_name}.fixed_version_id IS NOT NULL" +
|
758
|
" AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
|
759
|
" AND #{Version.table_name}.sharing <> 'system'",
|
760
|
conditions),
|
761
|
:include => [:project, :fixed_version]
|
762
|
).each do |issue|
|
763
|
next if issue.project.nil? || issue.fixed_version.nil?
|
764
|
unless issue.project.shared_versions.include?(issue.fixed_version)
|
765
|
issue.init_journal(User.current)
|
766
|
issue.fixed_version = nil
|
767
|
issue.save
|
768
|
end
|
769
|
end
|
770
|
end
|
771
|
|
772
|
# Callback on attachment deletion
|
773
|
def attachment_removed(obj)
|
774
|
journal = init_journal(User.current)
|
775
|
journal.details << JournalDetail.new(:property => 'attachment',
|
776
|
:prop_key => obj.id,
|
777
|
:old_value => obj.filename)
|
778
|
journal.save
|
779
|
end
|
780
|
|
781
|
# Default assignment based on category
|
782
|
def default_assign
|
783
|
if assigned_to.nil? && category && category.assigned_to
|
784
|
self.assigned_to = category.assigned_to
|
785
|
end
|
786
|
end
|
787
|
|
788
|
# Updates start/due dates of following issues
|
789
|
def reschedule_following_issues
|
790
|
if start_date_changed? || due_date_changed?
|
791
|
relations_from.each do |relation|
|
792
|
relation.set_issue_to_dates
|
793
|
end
|
794
|
end
|
795
|
end
|
796
|
|
797
|
# Closes duplicates if the issue is being closed
|
798
|
def close_duplicates
|
799
|
if closing?
|
800
|
duplicates.each do |duplicate|
|
801
|
# Reload is need in case the duplicate was updated by a previous duplicate
|
802
|
duplicate.reload
|
803
|
# Don't re-close it if it's already closed
|
804
|
next if duplicate.closed?
|
805
|
# Same user and notes
|
806
|
if @current_journal
|
807
|
duplicate.init_journal(@current_journal.user, @current_journal.notes)
|
808
|
end
|
809
|
duplicate.update_attribute :status, self.status
|
810
|
end
|
811
|
end
|
812
|
end
|
813
|
|
814
|
# Saves the changes in a Journal
|
815
|
# Called after_save
|
816
|
def create_journal
|
817
|
if @current_journal
|
818
|
# attributes changes
|
819
|
(Issue.column_names - %w(id description root_id lft rgt lock_version created_on updated_on)).each { |c|
|
820
|
@current_journal.details << JournalDetail.new(:property => 'attr',
|
821
|
:prop_key => c,
|
822
|
:old_value => @issue_before_change.send(c),
|
823
|
:value => send(c)) unless send(c)==@issue_before_change.send(c)
|
824
|
}
|
825
|
# custom fields changes
|
826
|
custom_values.each { |c|
|
827
|
next if (@custom_values_before_change[c.custom_field_id]==c.value ||
|
828
|
(@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
|
829
|
@current_journal.details << JournalDetail.new(:property => 'cf',
|
830
|
:prop_key => c.custom_field_id,
|
831
|
:old_value => @custom_values_before_change[c.custom_field_id],
|
832
|
:value => c.value)
|
833
|
}
|
834
|
@current_journal.save
|
835
|
# reset current journal
|
836
|
init_journal @current_journal.user, @current_journal.notes
|
837
|
end
|
838
|
end
|
839
|
|
840
|
# Query generator for selecting groups of issue counts for a project
|
841
|
# based on specific criteria
|
842
|
#
|
843
|
# Options
|
844
|
# * project - Project to search in.
|
845
|
# * field - String. Issue field to key off of in the grouping.
|
846
|
# * joins - String. The table name to join against.
|
847
|
def self.count_and_group_by(options)
|
848
|
project = options.delete(:project)
|
849
|
select_field = options.delete(:field)
|
850
|
joins = options.delete(:joins)
|
851
|
|
852
|
where = "i.#{select_field}=j.id"
|
853
|
|
854
|
ActiveRecord::Base.connection.select_all("select s.id as status_id,
|
855
|
s.is_closed as closed,
|
856
|
j.id as #{select_field},
|
857
|
count(i.id) as total
|
858
|
from
|
859
|
#{Issue.table_name} i, #{IssueStatus.table_name} s, #{joins} as j
|
860
|
where
|
861
|
i.status_id=s.id
|
862
|
and #{where}
|
863
|
and i.project_id=#{project.id}
|
864
|
group by s.id, s.is_closed, j.id")
|
865
|
end
|
866
|
|
867
|
|
868
|
end
|