Class: Task

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
UserSystem
Includes:
UserSystem
Defined in:
app/models/task.rb

Constant Summary collapse

COMPLETED =
'COMPLETED'
POSTPONED =
'POSTPONED'
MOVED =
'MOVED'
ABORTED =
'ABORTED'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_openObject



45
46
47
# File 'app/models/task.rb', line 45

def self.find_open
  find(:all, :conditions => 'finished_at IS NULL', :order => 'description')
end

.find_startedObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/task.rb', line 49

def self.find_started
  if current_user
    user_clause = " OR user_id = #{current_user.id}"
  end
  conditions = "completed_at IS NULL AND (user_id IS NULL#{user_clause})"
  Work.find(:all, :conditions => conditions).map {|work| work.task}.sort do |t1, t2|
    if (backlog_sort = t1.backlog.name <=> t2.backlog.name) != 0
      backlog_sort
    elsif (period_sort = t1.period.end_on <=> t2.period.end_on) != 0
      period_sort
    else
      t1.position <=> t2.position
    end
  end
end

.recent_conditionsObject



65
66
67
# File 'app/models/task.rb', line 65

def self.recent_conditions
  return "finished_at >= '#{1.week.ago.iso8601}'"
end

Instance Method Details

#abort(user) ⇒ Object



333
334
335
# File 'app/models/task.rb', line 333

def abort(user)
  finish(Task::ABORTED, false)
end

#active?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'app/models/task.rb', line 203

def active?
  finished_at.nil? || work_started? || active_children?
end

#active_children?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'app/models/task.rb', line 207

def active_children?
  children.detect {|child| child.active?}
end

#check_finished(subtask_finsihed_at, resolution, save_work) ⇒ Object



195
196
197
198
199
200
201
# File 'app/models/task.rb', line 195

def check_finished(subtask_finsihed_at, resolution, save_work)
  return if self.finished_at
  children.each do |child_task|
    return if child_task.active?
  end
  finish(resolution, save_work)
end

#completed?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'app/models/task.rb', line 211

def completed?
  finished_at || completed_children?
end

#completed_children?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'app/models/task.rb', line 215

def completed_children?
  children.detect {|child| child.completed?}
end

#depthObject



270
271
272
273
274
275
276
277
278
# File 'app/models/task.rb', line 270

def depth
  root_task = self 
  depth = 0
  while !root_task.root?
    root_task = root_task.parent
    depth += 1
  end
  depth
end

#doneObject



97
98
99
# File 'app/models/task.rb', line 97

def done
  nil
end

#done=(hours_done) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'app/models/task.rb', line 101

def done=(hours_done)
  return unless hours_done && hours_done != '' && BigDecimal(hours_done) != 0
  return if end_work(hours_done)
  new_work = Work.new
  new_work.hours = hours_done
  new_work.task_id = self.id
  new_work.completed_at = DateTime.now
  new_work.save!
  works << new_work
end

#enable_customer?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'app/models/task.rb', line 257

def enable_customer?
  root_task.backlog.enable_customer?
end

#enable_invoicing?Boolean

Returns:

  • (Boolean)


261
262
263
# File 'app/models/task.rb', line 261

def enable_invoicing?
  root_task.backlog.enable_invoicing?
end

#enable_subtasks?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'app/models/task.rb', line 249

def enable_subtasks?
  root_task.backlog.enable_subtasks?
end

#enable_users?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'app/models/task.rb', line 253

def enable_users?
  root_task.backlog.enable_users?
end

#estimate(new_todo) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/task.rb', line 73

def estimate(new_todo)
  return unless new_todo && new_todo != ''
  previous_estimate = estimates.last
  new_estimate = Estimate.new
  if previous_estimate
    return if new_todo == previous_estimate.todo
  else
    return if new_todo == initial_estimate
  end
  now = Time.now
  if previous_estimate && (now < previous_estimate.created_at || now.strftime('%Y-%m-%DT%H:%M:%S') == previous_estimate.created_at.strftime('%Y-%m-%DT%H:%M:%S'))
    previous_estimate.todo = new_todo
    previous_estimate.user = current_user
    previous_estimate.save!
  else
    new_estimate.task_id = self.id
    new_estimate.todo = new_todo
    new_estimate.created_at = now
    new_estimate.user = current_user
    new_estimate.save!
    estimates << new_estimate
  end
end

#estimate_data(date, actual = false) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'app/models/task.rb', line 280

def estimate_data(date, actual=false)
  if children.empty?
    return 0 if actual && (resolution == ABORTED || resolution == POSTPONED)
    return 0 if finished_at && (date >= finished_at.to_date)
    estimate = Estimate.find(:first, :conditions => "task_id = #{id} AND created_at < '#{(date+1).to_s}'", :order => 'created_at DESC,id DESC')
    if estimate
      estimate.todo
    elsif actual || created_at < (date+1).to_time
      initial_estimate
    else
      0
    end
  else
    total = BigDecimal('0')
    children.each {|child_task| total += child_task.estimate_data(date, actual)}
    total
  end
end

#finish(resolution, save_work) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/task.rb', line 183

def finish(resolution, save_work)
  unless finished_at || work_started?
    self.finished_at = Time.now
    self.resolution = resolution
    remove_from_list
    self.position = nil
    save!
    estimate(0) if save_work
    parent.check_finished(self.finished_at, resolution, save_work) if parent
  end
end

#leaf?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'app/models/task.rb', line 223

def leaf?
  children.size == 0
end

#loggable?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'app/models/task.rb', line 219

def loggable?
  active? && leaf?
end

#move_to_period(new_period) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/task.rb', line 140

def move_to_period(new_period)
  raise "Period missing" unless new_period
  raise "Cannot move finished tasks" unless active?
  return if new_period == period
  
  self.remove_from_list
  self.position = nil
  
  if self.period
    old_todo = self.todo
    self.finish(new_period.party == self.period.party ? Task::POSTPONED : Task::MOVED, true)
    ancestor_id = self.previous_task_id || self.id
    existing_task =   Task.find_by_period_id_and_id(new_period.id, ancestor_id)
    if existing_task ||= Task.find_by_period_id_and_previous_task_id(new_period.id, ancestor_id)
      raise "mismatch" unless existing_task.backlog == root_task.backlog
      raise "Mismatch" unless existing_task.period == new_period
      existing_task.open
      existing_task.previous_task_id = self.previous_task_id || self.id
      existing_task.description = self.description
      existing_task.save!
      existing_task.estimate(old_todo)
    else
      new_task = Task.new
      new_task.previous_task_id = ancestor_id
      new_task.backlog = root_task.backlog
      new_task.period = new_period
      new_task.description = self.description
      new_task.initial_estimate = self.initial_estimate
      new_task.insert_at 1
      new_task.position = 1
      new_task.save!
      new_task.estimate(old_todo)
    end
  else
    self.period_id = new_period.id
    self.period = new_period
    self.insert_at(1)
    self.position = 1
    self.move_to_bottom
    self.save!
  end
end

#old_periodObject



265
# File 'app/models/task.rb', line 265

alias_method :old_period, :period

#openObject



118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/task.rb', line 118

def open
  if finished_at
    self.finished_at = nil
    self.resolution = nil
    insert_at 1
    self.position = 1
    self.save!
    estimate(initial_estimate)
    parent.open if parent
  end
end

#periodObject



266
267
268
# File 'app/models/task.rb', line 266

def period
  old_period || root_task.old_period
end

#reopenObject



130
131
132
133
134
135
136
137
138
# File 'app/models/task.rb', line 130

def reopen
  if period.passed?
    flash[:notice] = "You cannot reopen a task in a period that is passed."
  else
    open
    save!
    children.each {|child_task| child_task.reopen}
  end
end

#root?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'app/models/task.rb', line 227

def root?
  parent.nil?
end

#root_taskObject



231
232
233
234
235
# File 'app/models/task.rb', line 231

def root_task
  root_task = self 
  root_task = root_task.parent while root_task.parent
  root_task
end

#start_workObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'app/models/task.rb', line 309

def start_work
  return if work_started?
  open
  new_work = works.new
  new_work.started_at = Time.previous_quarter
  if works.size > 0
    if current_user
      last_work = works.select {|work| work.user == current_user}.last
    end
    unless last_work
      last_work = works.select {|work| work.user.nil?}.last
    end
    if last_work && last_work.completed_at > new_work.started_at
      new_work.started_at = last_work.completed_at
    end
  end
  new_work.user = current_user
  new_work.save!
end

#started_workObject



341
342
343
344
345
346
347
348
# File 'app/models/task.rb', line 341

def started_work
  started_works = works.select {|work| work.completed_at.nil?}
  if current_user
    started_by_user = started_works.select {|work| work.user == current_user}.last
    return started_by_user if started_by_user
  end
  started_works.select {|work| work.user.nil?}.last
end

#todoObject



69
70
71
# File 'app/models/task.rb', line 69

def todo
  estimates.last ? estimates.last.todo : initial_estimate
end

#total_doneObject



112
113
114
115
116
# File 'app/models/task.rb', line 112

def total_done
  total = BigDecimal('0')
  works.each {|work| total += work.hours}
  total
end

#track_done?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'app/models/task.rb', line 237

def track_done?
  root_task.backlog.track_done
end

#track_times?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'app/models/task.rb', line 241

def track_times?
  root_task.backlog.track_times?
end

#track_todo?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'app/models/task.rb', line 245

def track_todo?
  root_task.backlog.track_todo?
end

#validateObject



36
37
38
39
40
41
42
43
# File 'app/models/task.rb', line 36

def validate
  if self.parent_id && (self.period_id || self.backlog_id)
    errors.add :parent_id, "A subtask may not have neither period nor backlog set."
  end
  if new_record? && self.period && self.period.passed?
    errors.add :period_id, "You may not add a task to a past period."
  end
end

#work_data(date) ⇒ Object



299
300
301
302
303
304
305
306
307
# File 'app/models/task.rb', line 299

def work_data(date)
  return 0 if resolution == ABORTED
  relevant_works = Work.find(:all, :conditions => "task_id = #{id} AND completed_at < '#{(date+1).to_s}'")
  total = BigDecimal('0')
  relevant_works.each do |work|
    total += work.hours
  end
  total
end

#work_started?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'app/models/task.rb', line 337

def work_started?
  !started_work.nil? 
end

#works_with_childrenObject



329
330
331
# File 'app/models/task.rb', line 329

def works_with_children
  works << children.map {|t| t.works_with_children}.flatten
end