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

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Method Details

.keysObject


22
23
24
# File 'app/models/role.rb', line 22

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>


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

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

#destroy_if_emptyObject


26
27
28
# File 'app/models/role.rb', line 26

def destroy_if_empty
  destroy if users.empty?
end

#touch_authorizableObject


30
31
32
# File 'app/models/role.rb', line 30

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