Class: Coyote::OrganizationUser
- Inherits:
-
Object
- Object
- Coyote::OrganizationUser
- 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
-
#admin? ⇒ Boolean
readonly
Is the user at least an admin for this organization?.
-
#author? ⇒ Boolean
readonly
Is the user at least an author for this organization?.
-
#editor? ⇒ Boolean
readonly
Is the user at least an editor for this organization?.
-
#guest? ⇒ Boolean
readonly
Is the user at least a guest for this organization?.
-
#id ⇒ Integer
readonly
Database ID of the underlying User object.
-
#owner? ⇒ Boolean
readonly
Is the user at least an owner of this organization?.
-
#staff? ⇒ Boolean
readonly
Whether or not the user is a Coyote staff member with cross-organizational authority.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#viewer? ⇒ Boolean
readonly
Is the user at least a viewer for this organization?.
Instance Method Summary collapse
-
#initialize(user, organization) ⇒ OrganizationUser
constructor
A new instance of OrganizationUser.
-
#membership ⇒ Membership
The membership we are modeling.
-
#role ⇒ Symbol
The name of the role held by this user in the current organization.
- #role_rank ⇒ Object
Constructor Details
#initialize(user, organization) ⇒ OrganizationUser
Returns a new instance of OrganizationUser
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?
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?
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?
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?
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 |
#id ⇒ Integer (readonly)
Returns 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?
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
13 |
# File 'lib/coyote/organization_user.rb', line 13 delegate :id, :staff?, :first_name, :last_name, :email, :==, to: :user |
#user ⇒ Object (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)
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?
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
#membership ⇒ Membership
Returns the membership we are modeling
51 52 53 |
# File 'lib/coyote/organization_user.rb', line 51 def membership @membership ||= user.memberships.find_by(organization: organization) end |
#role ⇒ Symbol
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.
56 57 58 |
# File 'lib/coyote/organization_user.rb', line 56 def role (membership&.role || :none).to_sym end |
#role_rank ⇒ Object
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 |