Module: ActiveModel::PolicyContext

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_model/policy_context.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#can!(action, object) ⇒ Object



26
27
28
# File 'lib/active_model/policy_context.rb', line 26

def can! action, object
  can?(action, object) || raise(ActiveModel::ActionNotAllowed, action)
end

#can?(action, object) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/active_model/policy_context.rb', line 19

def can? action, object
  return false if object.nil?

  policy = object.respond_to?(:to_policy) && object.to_policy
  !policy || policy.can?(action, policy_context)
end

#cannot!(action, object) ⇒ Object



34
35
36
# File 'lib/active_model/policy_context.rb', line 34

def cannot! action, object
  cannot?(action, object) || raise(ActiveModel::ActionUnexpected, action)
end

#cannot?(*args) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/active_model/policy_context.rb', line 30

def cannot? *args
  !can?(*args)
end

#policy_contextObject



11
12
13
14
15
16
17
# File 'lib/active_model/policy_context.rb', line 11

def policy_context
  if _policy_context && respond_to?(_policy_context)
    send _policy_context
  else
    self
  end
end