Module: Role::Authorized

Included in:
Study
Defined in:
app/models/role.rb

Overview

Include this module into your ActiveRecord model and get has_many roles and some utility named_scopes. You also get the ability to define role relations by name through the role_relation class method.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object


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

def self.included(base)
  base.extend(ClassMethods)
  base.instance_eval do
    has_many :roles, as: :authorizable
    has_many :users, through: :roles

    scope :with_related_users_included, -> { includes(roles: :users) }
    scope :with_related_owners_included, -> { includes(:owners) }
    scope :of_interest_to, ->(user) { joins(:users).where(users: { id: user }).distinct }
  end
end