Module: BeTaskable::Taskable::InstanceMethods

Defined in:
lib/be_taskable/taskable.rb

Instance Method Summary collapse

Instance Method Details

#complete_task_for(action, assignee) ⇒ Object

Completes a task assignment

Parameters:

  • action (String)

    Name of the action

  • assignee (Object)


136
137
138
139
140
# 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) ⇒ Object

Completes the last task for an action (and all the assignments)

Parameters:

  • action (String)

    Name of the action



115
116
117
118
119
# 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) ⇒ Object

Completes all task for this action Only for uncompleted tasks



123
124
125
126
127
128
129
130
131
# 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) ⇒ BeTaskable::Task

Finds or Create a task and run it

Parameters:

  • action (String)

    Name of the action

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ BeTaskable::Task

Create a task and run it

Parameters:

  • action (String)

    Name of the action

Returns:

Raises:

  • (ActiveRecord::RecordNotSaved)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



108
109
110
111
# 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) ⇒ Object



72
73
74
# 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) ⇒ Object

Expire all task for this action Only for uncompleted tasks



144
145
146
147
148
149
150
151
152
# 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) ⇒ BeTaskable::Task

Returns Last current task for the given action.

Returns:



83
84
85
# 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) ⇒ BeTaskable::Task

Returns Last task for the given action.

Parameters:

  • action (String)

    Name of the action

Returns:



78
79
80
# 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) ⇒ TaskAssignment

Return nil if it cannot find the task

Parameters:

  • action (String)
  • assignee (Object)

Returns:



103
104
105
106
# 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_assignmentsArray

Returns All current assignments for this taskable.

Returns:

  • (Array)

    All current assignments for this taskable



88
89
90
# File 'lib/be_taskable/taskable.rb', line 88

def task_assignments
	tasks.map(&:assignments).flatten
end

#task_assignments_for_action(action) ⇒ Array

returns an empty array if it cannot find the task

Returns:

  • (Array)

    All current assignments for this action



94
95
96
# 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) ⇒ Object

Returns Resolver instance for the given action.

Parameters:

  • action (String)

    Name of the action

Returns:

  • (Object)

    Resolver instance for the given action



28
29
30
# File 'lib/be_taskable/taskable.rb', line 28

def task_resolver_for_action(action)
	self.class._task_resolver_for_action(action)
end

#taskable?Boolean

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/be_taskable/taskable.rb', line 22

def taskable?
	true
end

#tasks_for_action(action) ⇒ ActiveRecord::Relation

Parameters:

  • action (String)

    Name of the action

Returns:

  • (ActiveRecord::Relation)


68
69
70
# File 'lib/be_taskable/taskable.rb', line 68

def tasks_for_action(action)
	tasks.where(action: action)
end