Class: Task
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Task
- 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
- .find_descendant(period_id, ancestor_task_id) ⇒ Object
- .find_open ⇒ Object
- .find_started ⇒ Object
- .recent_conditions ⇒ Object
Instance Method Summary collapse
- #abort ⇒ Object
- #active? ⇒ Boolean
- #active_children? ⇒ Boolean
- #ancestor_id ⇒ Object
- #backlog ⇒ Object
- #check_finished(subtask_finsihed_at, resolution, save_work) ⇒ Object
- #completed? ⇒ Boolean
- #completed_children? ⇒ Boolean
- #depth ⇒ Object
- #description_with_id ⇒ Object
- #description_with_parents ⇒ Object
- #done ⇒ Object
- #done=(hours_done) ⇒ Object
- #enable_customer? ⇒ Boolean
- #enable_invoicing? ⇒ Boolean
- #enable_subtasks? ⇒ Boolean
- #enable_users? ⇒ Boolean
- #estimate(new_todo) ⇒ Object
- #estimate_data(date, actual = false) ⇒ Object
- #finish(resolution, save_work) ⇒ Object
- #finished? ⇒ Boolean
- #grab ⇒ Object
- #invoicing? ⇒ Boolean
- #leaf? ⇒ Boolean
- #loggable? ⇒ Boolean
- #move_to_period(new_period) ⇒ Object
- #old_backlog ⇒ Object
- #old_period ⇒ Object
- #old_work_account ⇒ Object
- #open ⇒ Object
- #period ⇒ Object
- #release ⇒ Object
- #reopen ⇒ Object
- #root? ⇒ Boolean
- #root_task ⇒ Object
- #self_with_children ⇒ Object
- #start_work ⇒ Object
- #started_work ⇒ Object
- #todo ⇒ Object
- #total_done ⇒ Object
- #track_done? ⇒ Boolean
- #track_times? ⇒ Boolean
- #track_todo? ⇒ Boolean
- #validate ⇒ Object
- #work_account ⇒ Object
- #work_data(date) ⇒ Object
- #work_started? ⇒ Boolean
- #works_with_children ⇒ Object
Class Method Details
.find_descendant(period_id, ancestor_task_id) ⇒ Object
73 74 75 76 77 78 79 |
# File 'app/models/task.rb', line 73 def self.find_descendant(period_id, ancestor_task_id) candidate_tasks = Task.find :all, :conditions => ["id = ? OR previous_task_id = ?", ancestor_task_id, ancestor_task_id] candidate_tasks = candidate_tasks.select {|t| t.root_task.period_id == period_id} logger.error "Multiple descendants in period: #{period_id} #{ancestor_task_id}" if candidate_tasks.size > 1 candidate_tasks[0] end |
.find_open ⇒ Object
55 56 57 |
# File 'app/models/task.rb', line 55 def self.find_open find(:all, :conditions => 'finished_at IS NULL', :order => 'description') end |
.find_started ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'app/models/task.rb', line 59 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}.compact.sort_by do |t| [t.root_task.backlog.name, t.root_task.period ? t.root_task.period.end_on : 0, t.position || 0] end end |
.recent_conditions ⇒ Object
69 70 71 |
# File 'app/models/task.rb', line 69 def self.recent_conditions return "finished_at >= '#{1.week.ago.iso8601}'" end |
Instance Method Details
#abort ⇒ Object
398 399 400 |
# File 'app/models/task.rb', line 398 def abort finish(Task::ABORTED, false) end |
#active? ⇒ Boolean
244 245 246 |
# File 'app/models/task.rb', line 244 def active? finished_at.nil? || work_started? || active_children? end |
#active_children? ⇒ Boolean
248 249 250 |
# File 'app/models/task.rb', line 248 def active_children? children.detect {|child| child.active?} end |
#ancestor_id ⇒ Object
427 428 429 |
# File 'app/models/task.rb', line 427 def ancestor_id previous_task_id || id end |
#backlog ⇒ Object
320 321 322 |
# File 'app/models/task.rb', line 320 def backlog old_backlog || root_task.old_backlog end |
#check_finished(subtask_finsihed_at, resolution, save_work) ⇒ Object
236 237 238 239 240 241 242 |
# File 'app/models/task.rb', line 236 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
256 257 258 |
# File 'app/models/task.rb', line 256 def completed? finished_at || completed_children? end |
#completed_children? ⇒ Boolean
260 261 262 |
# File 'app/models/task.rb', line 260 def completed_children? children.detect {|child| child.completed?} end |
#depth ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'app/models/task.rb', line 329 def depth root_task = self depth = 0 while !root_task.root? root_task = root_task.parent depth += 1 end depth end |
#description_with_id ⇒ Object
415 416 417 |
# File 'app/models/task.rb', line 415 def description_with_id "##{id}: #{description}" end |
#description_with_parents ⇒ Object
419 420 421 422 423 424 425 |
# File 'app/models/task.rb', line 419 def description_with_parents if parent.nil? "#{backlog.name}: #{description}" else "#{parent.description_with_parents} - #{description}" end end |
#done ⇒ Object
117 118 119 |
# File 'app/models/task.rb', line 117 def done nil end |
#done=(hours_done) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/task.rb', line 121 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
306 307 308 |
# File 'app/models/task.rb', line 306 def enable_customer? root_task.backlog.enable_customer? end |
#enable_invoicing? ⇒ Boolean
310 311 312 |
# File 'app/models/task.rb', line 310 def enable_invoicing? root_task.backlog.enable_invoicing? end |
#enable_subtasks? ⇒ Boolean
298 299 300 |
# File 'app/models/task.rb', line 298 def enable_subtasks? root_task.backlog.enable_subtasks? end |
#enable_users? ⇒ Boolean
302 303 304 |
# File 'app/models/task.rb', line 302 def enable_users? root_task.backlog.enable_users? end |
#estimate(new_todo) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/task.rb', line 89 def estimate(new_todo) return unless new_todo && new_todo != '' new_todo = new_todo.to_i if new_todo == 0 finish(Task::COMPLETED, false) end 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
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'app/models/task.rb', line 339 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
224 225 226 227 228 229 230 231 232 233 234 |
# File 'app/models/task.rb', line 224 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 |
#finished? ⇒ Boolean
252 253 254 |
# File 'app/models/task.rb', line 252 def finished? completed? end |
#grab ⇒ Object
216 217 218 |
# File 'app/models/task.rb', line 216 def grab self.users << current_user end |
#invoicing? ⇒ Boolean
282 283 284 |
# File 'app/models/task.rb', line 282 def invoicing? root_task.backlog.enable_invoicing? end |
#leaf? ⇒ Boolean
268 269 270 |
# File 'app/models/task.rb', line 268 def leaf? children.size == 0 end |
#loggable? ⇒ Boolean
264 265 266 |
# File 'app/models/task.rb', line 264 def loggable? active? && leaf? end |
#move_to_period(new_period) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'app/models/task.rb', line 160 def move_to_period(new_period) raise "Cannot move finished tasks" unless active? return self if new_period == period self.remove_from_list self.position = nil if self.period old_todo = self.todo if parent parent.move_to_period new_period end if leaf? self.finish((new_period.nil? || (new_period.party == self.period.party)) ? Task::POSTPONED : Task::MOVED, true) end existing_task = Task.find_descendant(new_period && new_period.id, ancestor_id) if existing_task ||= Task.find_by_period_id_and_previous_task_id(new_period && 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.users = self.users existing_task.save! existing_task.estimate(old_todo) return existing_task else new_task = Task.new new_task.previous_task_id = ancestor_id if parent new_parent = Task.find_descendant(new_period.id, parent.ancestor_id) new_task.parent = new_parent else new_task.backlog = root_task.backlog new_task.period = new_period end new_task.description = self.description new_task.initial_estimate = self.initial_estimate new_task.position = nil new_task.users = self.users new_task.save! new_task.insert_at 1 new_task.estimate(old_todo) return new_task end else self.period_id = new_period && new_period.id self.period = new_period self.insert_at(1) self.position = 1 self.move_to_bottom self.save! return self end end |
#old_backlog ⇒ Object
319 |
# File 'app/models/task.rb', line 319 alias_method :old_backlog, :backlog |
#old_period ⇒ Object
314 |
# File 'app/models/task.rb', line 314 alias_method :old_period, :period |
#old_work_account ⇒ Object
324 |
# File 'app/models/task.rb', line 324 alias_method :old_work_account, :work_account |
#open ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'app/models/task.rb', line 138 def open if finished_at self.finished_at = nil self.resolution = nil self.position = nil insert_at 1 estimate(initial_estimate) parent.open if parent end end |
#period ⇒ Object
315 316 317 |
# File 'app/models/task.rb', line 315 def period old_period || root_task.old_period end |
#release ⇒ Object
220 221 222 |
# File 'app/models/task.rb', line 220 def release self.users.delete current_user end |
#reopen ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'app/models/task.rb', line 149 def reopen if period && period.passed? raise "You cannot reopen a task in a period that is passed." else open save! children.each {|child_task| child_task.reopen} end self end |
#root? ⇒ Boolean
272 273 274 |
# File 'app/models/task.rb', line 272 def root? parent.nil? end |
#root_task ⇒ Object
276 277 278 279 280 |
# File 'app/models/task.rb', line 276 def root_task root_task = self root_task = root_task.parent while root_task.parent root_task end |
#self_with_children ⇒ Object
81 82 83 |
# File 'app/models/task.rb', line 81 def self_with_children [self] << children.map {|t| t.self_with_children}.flatten end |
#start_work ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'app/models/task.rb', line 368 def start_work return if work_started? open grab if current_user new_work = works.new # TODO (uwe): Only needed for rails 1.2.x branch. Remove when moving to 1.3 or 2.0. new_work.task = self new_work.started_on = Time.previous_quarter.to_date new_work.start_time = Time.previous_quarter.time_of_day if current_user last_work = current_user.works.select {|w| w.completed_at}.last else last_work = Work.find(:first, :conditions => 'completed_at IS NOT NULL', :order => 'completed_at DESC') end if last_work && last_work.completed_at > new_work.started_on.at(new_work.start_time) new_work.started_at = last_work.completed_at end new_work.user = current_user new_work.work_account_id = work_account.id new_work.save! works << new_work end |
#started_work ⇒ Object
406 407 408 409 410 411 412 413 |
# File 'app/models/task.rb', line 406 def started_work started_works = works.select {|work| work.start_time && 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 |
#todo ⇒ Object
85 86 87 |
# File 'app/models/task.rb', line 85 def todo estimates.last ? estimates.last.todo : initial_estimate end |
#total_done ⇒ Object
132 133 134 135 136 |
# File 'app/models/task.rb', line 132 def total_done total = BigDecimal('0') works.each {|work| total += work.hours} total end |
#track_done? ⇒ Boolean
286 287 288 |
# File 'app/models/task.rb', line 286 def track_done? not root_task.backlog.work_account.nil? end |
#track_times? ⇒ Boolean
290 291 292 |
# File 'app/models/task.rb', line 290 def track_times? work_account && work_account.track_times? end |
#track_todo? ⇒ Boolean
294 295 296 |
# File 'app/models/task.rb', line 294 def track_todo? root_task.backlog.track_todo? end |
#validate ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'app/models/task.rb', line 46 def validate if self.parent_id && (self.period_id || self.backlog_id) errors.add :parent_id, "A subtask may 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_account ⇒ Object
325 326 327 |
# File 'app/models/task.rb', line 325 def work_account self.old_work_account || (parent ? parent.work_account : backlog.work_account) end |
#work_data(date) ⇒ Object
358 359 360 361 362 363 364 365 366 |
# File 'app/models/task.rb', line 358 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
402 403 404 |
# File 'app/models/task.rb', line 402 def work_started? !started_work.nil? end |
#works_with_children ⇒ Object
394 395 396 |
# File 'app/models/task.rb', line 394 def works_with_children works << children.map {|t| t.works_with_children}.flatten end |