Class: Coyote::OrganizationUser

Inherits:
Object
  • Object
show all
Defined in:
lib/coyote/organization_user.rb

Overview

Encapsulates a particular user acting within a particular organization, using a role defined via the Membership model. Used by Pundit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, organization) ⇒ OrganizationUser

Returns a new instance of OrganizationUser

Parameters:

  • user (User)

    the user who is acting

  • organization (Organization)

    the organization being acted upon/within



21
22
23
24
# File 'lib/coyote/organization_user.rb', line 21

def initialize(user, organization)
  @user = user
  @organization = organization
end

Instance Attribute Details

#admin?Boolean (readonly)

Returns is the user at least an admin for this organization?

Returns:

  • (Boolean)

    is the user at least an admin for this organization?



43
44
45
46
47
48
# File 'lib/coyote/organization_user.rb', line 43

Coyote::Membership.each_role do |_, test_role_name, test_role_rank|
  define_method :"#{test_role_name}?" do
    return true if staff? # staff members automatically have all admin owner powers regardless of whatever role they nominally hold in an organization
    role_rank >= test_role_rank
  end
end

#author?Boolean (readonly)

Returns is the user at least an author for this organization?

Returns:

  • (Boolean)

    is the user at least an author for this organization?



43
44
45
46
47
48
# File 'lib/coyote/organization_user.rb', line 43

Coyote::Membership.each_role do |_, test_role_name, test_role_rank|
  define_method :"#{test_role_name}?" do
    return true if staff? # staff members automatically have all admin owner powers regardless of whatever role they nominally hold in an organization
    role_rank >= test_role_rank
  end
end

#editor?Boolean (readonly)

Returns is the user at least an editor for this organization?

Returns:

  • (Boolean)

    is the user at least an editor for this organization?



43
44
45
46
47
48
# File 'lib/coyote/organization_user.rb', line 43

Coyote::Membership.each_role do |_, test_role_name, test_role_rank|
  define_method :"#{test_role_name}?" do
    return true if staff? # staff members automatically have all admin owner powers regardless of whatever role they nominally hold in an organization
    role_rank >= test_role_rank
  end
end

#guest?Boolean (readonly)

Returns is the user at least a guest for this organization?

Returns:

  • (Boolean)

    is the user at least a guest for this organization?



43
44
45
46
47
48
# File 'lib/coyote/organization_user.rb', line 43

Coyote::Membership.each_role do |_, test_role_name, test_role_rank|
  define_method :"#{test_role_name}?" do
    return true if staff? # staff members automatically have all admin owner powers regardless of whatever role they nominally hold in an organization
    role_rank >= test_role_rank
  end
end

#idInteger (readonly)

Returns database ID of the underlying User object

Returns:

  • (Integer)

    database ID of the underlying User object



13
# File 'lib/coyote/organization_user.rb', line 13

delegate :id, :staff?, :first_name, :last_name, :email, :==, to: :user

#owner?Boolean (readonly)

Returns is the user at least an owner of this organization?

Returns:

  • (Boolean)

    is the user at least an owner of this organization?



43
44
45
46
47
48
# File 'lib/coyote/organization_user.rb', line 43

Coyote::Membership.each_role do |_, test_role_name, test_role_rank|
  define_method :"#{test_role_name}?" do
    return true if staff? # staff members automatically have all admin owner powers regardless of whatever role they nominally hold in an organization
    role_rank >= test_role_rank
  end
end

#staff?Boolean (readonly)

Returns whether or not the user is a Coyote staff member with cross-organizational authority

Returns:

  • (Boolean)

    whether or not the user is a Coyote staff member with cross-organizational authority



13
# File 'lib/coyote/organization_user.rb', line 13

delegate :id, :staff?, :first_name, :last_name, :email, :==, to: :user

#userObject (readonly)

Returns the value of attribute user



17
18
19
# File 'lib/coyote/organization_user.rb', line 17

def user
  @user
end

#viewer?Boolean (readonly)

Note:

Each role in inherits the power of the roles below/before it in the hierarchy

Returns is the user at least a viewer for this organization?

Returns:

  • (Boolean)

    is the user at least a viewer for this organization?



43
44
45
46
47
48
# File 'lib/coyote/organization_user.rb', line 43

Coyote::Membership.each_role do |_, test_role_name, test_role_rank|
  define_method :"#{test_role_name}?" do
    return true if staff? # staff members automatically have all admin owner powers regardless of whatever role they nominally hold in an organization
    role_rank >= test_role_rank
  end
end

Instance Method Details

#membershipMembership

Returns the membership we are modeling

Returns:



51
52
53
# File 'lib/coyote/organization_user.rb', line 51

def membership
  @membership ||= user.memberships.find_by(organization: organization)
end

#roleSymbol

Returns the name of the role held by this user in the current organization. Will return :none if the user is not a member of the organization.

Returns:

  • (Symbol)

    the name of the role held by this user in the current organization. Will return :none if the user is not a member of the organization.



56
57
58
# File 'lib/coyote/organization_user.rb', line 56

def role
  (membership&.role || :none).to_sym
end

#role_rankObject



61
62
63
64
65
66
67
# File 'lib/coyote/organization_user.rb', line 61

def role_rank
  if staff?
    Coyote::Membership.role_rank(:staff)
  else
    Coyote::Membership.role_rank(role)
  end
end