Class: Role

Inherits:
ApplicationRecord show all
Defined in:
app/models/role.rb

Overview

Defines named roles for users that may be applied to objects in a polymorphic fashion. For example, you could create a role “moderator” for an instance of a model (i.e., an object), a model class, or without any specification at all.

Defined Under Namespace

Modules: Authorized, UserRoleHelper Classes: UserRole

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.keysObject



20
21
22
# File 'app/models/role.rb', line 20

def self.keys
  distinct.pluck(:name)
end

Instance Method Details

#authorizes?(check) ⇒ <Type>

Returns the true if the role authorizes check. If check is a class, returns true is the authorizable is that class, otherwise checks the equality.

Parameters:

Returns:

  • (<Type>)

    <description>



41
42
43
44
45
46
47
48
49
50
# File 'app/models/role.rb', line 41

def authorizes?(check)
  case check
  when nil
    true
  when Class
    authorizable.is_a?(check)
  else
    check == authorizable
  end
end

#destroy_if_emptyObject



24
25
26
# File 'app/models/role.rb', line 24

def destroy_if_empty
  destroy if users.empty?
end

#touch_authorizableObject



28
29
30
# File 'app/models/role.rb', line 28

def touch_authorizable
  authorizable&.touch # rubocop:disable Rails/SkipsModelValidations
end