module BeTaskable::Taskable::InstanceMethods
Public Instance Methods
@param {String} action Name of the action @param {Object} assignee Completes a task assignment
# File lib/be_taskable/taskable.rb, line 136 def complete_task_for(action, assignee) task = last_task_for_action(action) return false unless task task.complete_by(assignee) end
@param {String} action Name of the action Completes the last task for an action (and all the assignments)
# File lib/be_taskable/taskable.rb, line 115 def complete_task_for_action(action) task = last_task_for_action(action) return false unless task task.complete! end
Completes all task for this action Only for uncompleted tasks
# File lib/be_taskable/taskable.rb, line 123 def complete_tasks_for_action(action) ts = current_tasks_for_action(action) return false unless ts.any? ts.each do |task| task.complete! end true end
Finds or Create a task and run it @param {String} action Name of the action @return {BeTaskable::Task} A task object
# File lib/be_taskable/taskable.rb, line 53 def create_or_refresh_task_for_action(action) # if already created use that task task = last_current_task_for_action(action) if !task task = create_task_for_action(action) else task.refresh end task end
Create a task and run it @param {String} action Name of the action @return {BeTaskable::Task} A task object
# File lib/be_taskable/taskable.rb, line 35 def create_task_for_action(action) raise(ActiveRecord::RecordNotSaved, "Taskable must be persisted") unless self.persisted? task = tasks.create(action: action) if task.persisted? task.on_creation task.refresh else raise "Couldn't create task #{task.errors.full_messages}" end task end
# File lib/be_taskable/taskable.rb, line 108 def current_task_assignment_for(action, assignee) task = last_current_task_for_action(action) task.assignment_for(assignee) if task end
# File lib/be_taskable/taskable.rb, line 72 def current_tasks_for_action(action) tasks.where(action: action).current end
Expire all task for this action Only for uncompleted tasks
# File lib/be_taskable/taskable.rb, line 144 def expire_tasks_for_action(action) ts = current_tasks_for_action(action) return false unless ts.any? ts.each do |task| task.expire end true end
@return {BeTaskable::Task} Last current task for the given action
# File lib/be_taskable/taskable.rb, line 83 def last_current_task_for_action(action) current_tasks_for_action(action).last end
@param {String} action Name of the action @return {BeTaskable::Task} Last task for the given action
# File lib/be_taskable/taskable.rb, line 78 def last_task_for_action(action) tasks_for_action(action).last end
@param {String} action @param {Object} assignee @return {TaskAssignment} Return nil if it cannot find the task
# File lib/be_taskable/taskable.rb, line 103 def task_assignment_for(action, assignee) task = last_task_for_action(action) task.assignment_for(assignee) if task end
@return {Array} All current assignments for this taskable
# File lib/be_taskable/taskable.rb, line 88 def task_assignments tasks.map(&:assignments).flatten end
@return {Array} All current assignments for this action returns an empty array if it cannot find the task
# File lib/be_taskable/taskable.rb, line 94 def task_assignments_for_action(action) tasks_for_action(action).map(&:assignments).flatten end
@param {String} action Name of the action @return {Object} Resolver instance for the given action
# File lib/be_taskable/taskable.rb, line 28 def task_resolver_for_action(action) self.class._task_resolver_for_action(action) end
hook for testing e.g. expect(instance).to be_taskable
# File lib/be_taskable/taskable.rb, line 22 def taskable? true end
@param {String} action Name of the action @return {ActiveRecord::Relation}
# File lib/be_taskable/taskable.rb, line 68 def tasks_for_action(action) tasks.where(action: action) end