Module: AuthClient::Permission::ClassMethods

Defined in:
lib/auth_client/permission.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_auth_client_permission(roles: roles) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/auth_client/permission.rb', line 12

def acts_as_auth_client_permission(roles: roles)
  define_singleton_method :available_roles do
    roles.map(&:to_s)
  end

  delegate :info_notify, :to => :user, :prefix => true, :allow_nil => true

  after_destroy :user_info_notify
  after_save :user_info_notify

  belongs_to :context, :polymorphic => true

  scope :for_role, ->(role) { where(:role => role)  }
  scope :for_context, ->(context) { where(:context_id => context.try(:id), :context_type => context.try(:class))  }

  validates_inclusion_of :role, :in => available_roles + available_roles.map(&:to_sym)
  validates_presence_of :role
end