Class: TaskManager::Task

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activeObject



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

def active
  where("status = ? OR status = ?", :new, :in_process)
end

.autocompletableObject



81
82
83
# File 'app/models/task_manager/task.rb', line 81

def autocompletable
  where(autocompletable: true)
end

.just_expiredObject



73
74
75
# File 'app/models/task_manager/task.rb', line 73

def just_expired
  active.where("deadline <= ?", Time.now)
end

.remindableObject



77
78
79
# File 'app/models/task_manager/task.rb', line 77

def remindable
  active.where("reminding_at <= ?", Time.now)
end

Instance Method Details

#assigneeObject



21
22
23
# File 'app/models/task_manager/task.rb', line 21

def assignee
  assignable.assignee
end

#callbacksObject



25
26
27
# File 'app/models/task_manager/task.rb', line 25

def callbacks
  callables.collect(&:callback)
end

#expireObject



54
55
56
57
58
59
60
61
62
# File 'app/models/task_manager/task.rb', line 54

def expire
  Task.transaction do
    callbacks.each do |c|
      c.call(self) if c.respond_to?(:call)
    end

    update_attributes!(status: :expired)
  end
end

#finishObject



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

def finish
  update_attributes(finished_at: Time.now, status: :finished)
end

#next_reminding_atObject



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

def next_reminding_at
  if remindable?
    seconds = (deadline - reminding_at) / 60

    reminding_at.since(seconds)
  end
end

#remindObject

提醒任务执行者,任务即将到期,并且修改任务下次提醒时间。 如果‘next_reminding_at`返回`nil`,则表示该任务不再需要提醒。



46
47
48
49
50
51
52
# File 'app/models/task_manager/task.rb', line 46

def remind
  Task.transaction do
    assignee.remind_of_expiring_task(self) if assignee.respond_to?(:remind_of_expiring_task)

    update_attributes!(reminding_at: next_reminding_at)
  end
end

#remindable?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'app/models/task_manager/task.rb', line 29

def remindable?
  return false if reminding_at.nil?

  (status == :new || status == :in_process) &&
    (deadline - reminding_at) / 60 >= 2 * 24 * 60 * 60
end