module BeTaskable::Taskable::InstanceMethods

Public Instance Methods

complete_task_for(action, assignee) click to toggle source

@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
complete_task_for_action(action) click to toggle source

@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
complete_tasks_for_action(action) click to toggle source

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
create_or_refresh_task_for_action(action) click to toggle source

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_task_for_action(action) click to toggle source

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
current_task_assignment_for(action, assignee) click to toggle source
# 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
current_tasks_for_action(action) click to toggle source
# File lib/be_taskable/taskable.rb, line 72
def current_tasks_for_action(action)
        tasks.where(action: action).current
end
expire_tasks_for_action(action) click to toggle source

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
last_current_task_for_action(action) click to toggle source

@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
last_task_for_action(action) click to toggle source

@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
task_assignment_for(action, assignee) click to toggle source

@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
task_assignments() click to toggle source

@return {Array} All current assignments for this taskable

# File lib/be_taskable/taskable.rb, line 88
def task_assignments
        tasks.map(&:assignments).flatten
end
task_assignments_for_action(action) click to toggle source

@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
task_resolver_for_action(action) click to toggle source

@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
taskable?() click to toggle source

hook for testing e.g. expect(instance).to be_taskable

# File lib/be_taskable/taskable.rb, line 22
def taskable?
        true
end
tasks_for_action(action) click to toggle source

@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