module Bumbleworks::User

Public Instance Methods

authorized_tasks() click to toggle source

Returns Task::Finder instance filtered by roles assigned to this user.

# File lib/bumbleworks/user.rb, line 85
def authorized_tasks
  Bumbleworks::Task.for_roles(role_identifiers)
end
available_tasks() click to toggle source

Returns Task::Finder instance filtered by user roles and availability (unclaimed and completable).

# File lib/bumbleworks/user.rb, line 91
def available_tasks
  authorized_tasks.available
end
claim(task, force = false) click to toggle source

Attempts to set self as the claimant of the given task. If not authorized to claim the task, raises exception. Also bubbles exception from Task when task is already claimed by a different claimant.

# File lib/bumbleworks/user.rb, line 52
def claim(task, force = false)
  raise UnauthorizedClaimAttempt unless has_role?(task.role)
  release!(task) if force
  task.claim(claim_token)
end
claim!(task) click to toggle source

Same as claim, but first releases (by force) the task, to avoid an AlreadyClaimed exceptions. Note that this may still raise an UnauthorizedClaimAttempt exception - this method does not allow a user to claim a task they are not authorized for. Should only be made available to supervisory roles.

# File lib/bumbleworks/user.rb, line 63
def claim!(task)
  claim(task, true)
end
claim_token() click to toggle source

The return value from this method is used as the “claimant” token on tasks, both for claiming a task and for checking if the user is the current claimant.

By default, claim_token will first check for `username`, then `email`, and finally raise an exception if neither method exists. Including classes should override this method if using something other than username or email (or if both respond, but email should be preferred).

# File lib/bumbleworks/user.rb, line 26
def claim_token
  [:username, :email].each do |token_method|
    return send(token_method) if respond_to?(token_method)
  end
  raise NoClaimTokenMethodDefined,
    "If your user class does not respond to :username or :email, define a `claim_token` method"
end
claimed_tasks() click to toggle source

Returns Task::Finder instance filtered by claimant - only tasks this user has claimed (and not released or completed).

# File lib/bumbleworks/user.rb, line 97
def claimed_tasks
  Bumbleworks::Task.for_claimant(claim_token)
end
has_role?(role_name) click to toggle source

Returns true if the array returned by role_identifiers includes the given name. Can be used to determine authority to perform actions on a task, for example.

# File lib/bumbleworks/user.rb, line 45
def has_role?(role_name)
  role_identifiers.include? role_name
end
release(task, force = false) click to toggle source

If we are the current claimant of the given task, release the task. Does nothing if the task is not claimed, but raises exception if the task is currently claimed by someone else.

# File lib/bumbleworks/user.rb, line 70
def release(task, force = false)
  return unless task.claimed?
  raise UnauthorizedReleaseAttempt unless force || task.claimant == claim_token
  task.release
end
release!(task) click to toggle source

Same as release, but releases the task even if we're not the current claimant. Allows an administrator, for example, to wrench a task away from an employee who is lagging. Should only be made available to supervisory roles.

# File lib/bumbleworks/user.rb, line 80
def release!(task)
  release(task, true)
end
role_identifiers() click to toggle source

The return value from this method is used when determining which tasks in the queue this user should be authorized for. Must return an array of strings.

# File lib/bumbleworks/user.rb, line 37
def role_identifiers
  raise NoRoleIdentifiersMethodDefined,
    "Define a `role_identifiers` method that returns an array of role names"
end