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 => 'Enumeration', :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_attachable :after_remove => :attachment_removed
|
36
|
acts_as_customizable
|
37
|
acts_as_watchable
|
38
|
acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
|
39
|
:include => [:project, :journals],
|
40
|
# sort by id so that limited eager loading doesn't break with postgresql
|
41
|
:order_column => "#{table_name}.id"
|
42
|
acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
|
43
|
:url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
|
44
|
:type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
|
45
|
|
46
|
acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
|
47
|
:author_key => :author_id
|
48
|
|
49
|
validates_presence_of :subject, :priority, :category, :project, :tracker, :author, :status
|
50
|
validates_length_of :subject, :maximum => 255
|
51
|
validates_inclusion_of :done_ratio, :in => 0..100
|
52
|
validates_numericality_of :estimated_hours, :allow_nil => true
|
53
|
|
54
|
named_scope :visible, lambda {|*args| { :include => :project,
|
55
|
:conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
|
56
|
|
57
|
named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
|
58
|
|
59
|
after_save :create_journal
|
60
|
|
61
|
# Returns true if usr or current user is allowed to view the issue
|
62
|
def visible?(usr=nil)
|
63
|
(usr || User.current).allowed_to?(:view_issues, self.project)
|
64
|
end
|
65
|
|
66
|
def after_initialize
|
67
|
if new_record?
|
68
|
# set default values for new records only
|
69
|
self.status ||= IssueStatus.default
|
70
|
self.priority ||= Enumeration.priorities.default
|
71
|
end
|
72
|
end
|
73
|
|
74
|
# Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
|
75
|
def available_custom_fields
|
76
|
(project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
|
77
|
end
|
78
|
|
79
|
def copy_from(arg)
|
80
|
issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
|
81
|
self.attributes = issue.attributes.dup
|
82
|
self.custom_values = issue.custom_values.collect {|v| v.clone}
|
83
|
self
|
84
|
end
|
85
|
|
86
|
# Moves/copies an issue to a new project and tracker
|
87
|
# Returns the moved/copied issue on success, false on failure
|
88
|
def move_to(new_project, new_tracker = nil, options = {})
|
89
|
options ||= {}
|
90
|
issue = options[:copy] ? self.clone : self
|
91
|
transaction do
|
92
|
if new_project && issue.project_id != new_project.id
|
93
|
# delete issue relations
|
94
|
unless Setting.cross_project_issue_relations?
|
95
|
issue.relations_from.clear
|
96
|
issue.relations_to.clear
|
97
|
end
|
98
|
# issue is moved to another project
|
99
|
# reassign to the category with same name if any
|
100
|
new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
|
101
|
issue.category = new_category
|
102
|
issue.fixed_version = nil
|
103
|
issue.project = new_project
|
104
|
end
|
105
|
if new_tracker
|
106
|
issue.tracker = new_tracker
|
107
|
end
|
108
|
if options[:copy]
|
109
|
issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
|
110
|
issue.status = self.status
|
111
|
end
|
112
|
if issue.save
|
113
|
unless options[:copy]
|
114
|
# Manually update project_id on related time entries
|
115
|
TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
|
116
|
end
|
117
|
else
|
118
|
Issue.connection.rollback_db_transaction
|
119
|
return false
|
120
|
end
|
121
|
end
|
122
|
return issue
|
123
|
end
|
124
|
|
125
|
def priority_id=(pid)
|
126
|
self.priority = nil
|
127
|
write_attribute(:priority_id, pid)
|
128
|
end
|
129
|
|
130
|
def estimated_hours=(h)
|
131
|
write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
|
132
|
end
|
133
|
|
134
|
def validate
|
135
|
if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
|
136
|
errors.add :due_date, :not_a_date
|
137
|
end
|
138
|
|
139
|
if self.due_date and self.start_date and self.due_date < self.start_date
|
140
|
errors.add :due_date, :greater_than_start_date
|
141
|
end
|
142
|
|
143
|
if start_date && soonest_start && start_date < soonest_start
|
144
|
errors.add :start_date, :invalid
|
145
|
end
|
146
|
end
|
147
|
|
148
|
def validate_on_create
|
149
|
errors.add :tracker_id, :invalid unless project.trackers.include?(tracker)
|
150
|
end
|
151
|
|
152
|
def before_create
|
153
|
# default assignment based on category
|
154
|
if assigned_to.nil? && category && category.assigned_to
|
155
|
self.assigned_to = category.assigned_to
|
156
|
end
|
157
|
end
|
158
|
|
159
|
def after_save
|
160
|
# Reload is needed in order to get the right status
|
161
|
reload
|
162
|
|
163
|
# Update start/due dates of following issues
|
164
|
relations_from.each(&:set_issue_to_dates)
|
165
|
|
166
|
# Close duplicates if the issue was closed
|
167
|
if @issue_before_change && !@issue_before_change.closed? && self.closed?
|
168
|
duplicates.each do |duplicate|
|
169
|
# Reload is need in case the duplicate was updated by a previous duplicate
|
170
|
duplicate.reload
|
171
|
# Don't re-close it if it's already closed
|
172
|
next if duplicate.closed?
|
173
|
# Same user and notes
|
174
|
duplicate.init_journal(@current_journal.user, @current_journal.notes)
|
175
|
duplicate.update_attribute :status, self.status
|
176
|
end
|
177
|
end
|
178
|
end
|
179
|
|
180
|
def init_journal(user, notes = "")
|
181
|
@current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
|
182
|
@issue_before_change = self.clone
|
183
|
@issue_before_change.status = self.status
|
184
|
@custom_values_before_change = {}
|
185
|
self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
|
186
|
# Make sure updated_on is updated when adding a note.
|
187
|
updated_on_will_change!
|
188
|
@current_journal
|
189
|
end
|
190
|
|
191
|
# Return true if the issue is closed, otherwise false
|
192
|
def closed?
|
193
|
self.status.is_closed?
|
194
|
end
|
195
|
|
196
|
# Returns true if the issue is overdue
|
197
|
def overdue?
|
198
|
!due_date.nil? && (due_date < Date.today) && !status.is_closed?
|
199
|
end
|
200
|
|
201
|
# Users the issue can be assigned to
|
202
|
def assignable_users
|
203
|
project.assignable_users
|
204
|
end
|
205
|
|
206
|
# Returns an array of status that user is able to apply
|
207
|
def new_statuses_allowed_to(user)
|
208
|
statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
|
209
|
statuses << status unless statuses.empty?
|
210
|
statuses.uniq.sort
|
211
|
end
|
212
|
|
213
|
# Returns the mail adresses of users that should be notified for the issue
|
214
|
def recipients
|
215
|
recipients = project.recipients
|
216
|
# Author and assignee are always notified unless they have been locked
|
217
|
recipients << author.mail if author && author.active?
|
218
|
recipients << assigned_to.mail if assigned_to && assigned_to.active?
|
219
|
recipients.compact.uniq
|
220
|
end
|
221
|
|
222
|
# Returns the total number of hours spent on this issue.
|
223
|
#
|
224
|
# Example:
|
225
|
# spent_hours => 0
|
226
|
# spent_hours => 50
|
227
|
def spent_hours
|
228
|
@spent_hours ||= time_entries.sum(:hours) || 0
|
229
|
end
|
230
|
|
231
|
def relations
|
232
|
(relations_from + relations_to).sort
|
233
|
end
|
234
|
|
235
|
def all_dependent_issues
|
236
|
dependencies = []
|
237
|
relations_from.each do |relation|
|
238
|
dependencies << relation.issue_to
|
239
|
dependencies += relation.issue_to.all_dependent_issues
|
240
|
end
|
241
|
dependencies
|
242
|
end
|
243
|
|
244
|
# Returns an array of issues that duplicate this one
|
245
|
def duplicates
|
246
|
relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
|
247
|
end
|
248
|
|
249
|
# Returns the due date or the target due date if any
|
250
|
# Used on gantt chart
|
251
|
def due_before
|
252
|
due_date || (fixed_version ? fixed_version.effective_date : nil)
|
253
|
end
|
254
|
|
255
|
# Returns the time scheduled for this issue.
|
256
|
#
|
257
|
# Example:
|
258
|
# Start Date: 2/26/09, End Date: 3/04/09
|
259
|
# duration => 6
|
260
|
def duration
|
261
|
(start_date && due_date) ? due_date - start_date : 0
|
262
|
end
|
263
|
|
264
|
def soonest_start
|
265
|
@soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
|
266
|
end
|
267
|
|
268
|
def to_s
|
269
|
"#{tracker} ##{id}: #{subject}"
|
270
|
end
|
271
|
|
272
|
private
|
273
|
|
274
|
# Callback on attachment deletion
|
275
|
def attachment_removed(obj)
|
276
|
journal = init_journal(User.current)
|
277
|
journal.details << JournalDetail.new(:property => 'attachment',
|
278
|
:prop_key => obj.id,
|
279
|
:old_value => obj.filename)
|
280
|
journal.save
|
281
|
end
|
282
|
|
283
|
# Saves the changes in a Journal
|
284
|
# Called after_save
|
285
|
def create_journal
|
286
|
if @current_journal
|
287
|
# attributes changes
|
288
|
(Issue.column_names - %w(id description lock_version created_on updated_on)).each {|c|
|
289
|
@current_journal.details << JournalDetail.new(:property => 'attr',
|
290
|
:prop_key => c,
|
291
|
:old_value => @issue_before_change.send(c),
|
292
|
:value => send(c)) unless send(c)==@issue_before_change.send(c)
|
293
|
}
|
294
|
# custom fields changes
|
295
|
custom_values.each {|c|
|
296
|
next if (@custom_values_before_change[c.custom_field_id]==c.value ||
|
297
|
(@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
|
298
|
@current_journal.details << JournalDetail.new(:property => 'cf',
|
299
|
:prop_key => c.custom_field_id,
|
300
|
:old_value => @custom_values_before_change[c.custom_field_id],
|
301
|
:value => c.value)
|
302
|
}
|
303
|
@current_journal.save
|
304
|
end
|
305
|
end
|
306
|
end
|