Module: Bumbleworks::User

Defined in:
lib/bumbleworks/user.rb

Defined Under Namespace

Classes: NoClaimTokenMethodDefined, NoRoleIdentifiersMethodDefined, UnauthorizedClaimAttempt, UnauthorizedReleaseAttempt

Instance Method Summary collapse

Instance Method Details

#authorized_tasksObject

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



85
86
87
# File 'lib/bumbleworks/user.rb', line 85

def authorized_tasks
  Bumbleworks::Task.for_roles(role_identifiers)
end

#available_tasksObject

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



91
92
93
# File 'lib/bumbleworks/user.rb', line 91

def available_tasks
  authorized_tasks.available
end

#claim(task, force = false) ⇒ Object

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.



52
53
54
55
56
# 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) ⇒ Object

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.



63
64
65
# File 'lib/bumbleworks/user.rb', line 63

def claim!(task)
  claim(task, true)
end

#claim_tokenObject

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).



26
27
28
29
30
31
32
# 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_tasksObject

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



97
98
99
# File 'lib/bumbleworks/user.rb', line 97

def claimed_tasks
  Bumbleworks::Task.for_claimant(claim_token)
end

#has_role?(role_name) ⇒ Boolean

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.

Returns:

  • (Boolean)


45
46
47
# File 'lib/bumbleworks/user.rb', line 45

def has_role?(role_name)
  role_identifiers.include? role_name
end

#release(task, force = false) ⇒ Object

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.



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

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.



80
81
82
# File 'lib/bumbleworks/user.rb', line 80

def release!(task)
  release(task, true)
end

#role_identifiersObject

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.



37
38
39
40
# 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