Exception: Pundit::NotAuthorizedError

Inherits:
Error
  • Object
show all
Defined in:
lib/pundit/error.rb

Overview

Error that will be raised when authorization has failed

Since:

  • v0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ NotAuthorizedError #initialize(options) ⇒ NotAuthorizedError

Returns a new instance of NotAuthorizedError.

Overloads:

  • #initialize(message) ⇒ NotAuthorizedError

    Create an error with a simple error message.

    Parameters:

    • message (String)

      A simple error message string.

  • #initialize(options) ⇒ NotAuthorizedError

    Create an error with the specified attributes.

    Parameters:

    • options (Hash)

      The error options.

    Options Hash (options):

    • :message (String)

      Optional custom error message. Will default to a generalized message.

    • :query (Symbol)

      The name of the policy method that was checked.

    • :record (Object)

      The object that was being checked with the policy.

    • :policy (Class)

      The class of policy that was used for the check.

Since:

  • v1.0.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pundit/error.rb', line 36

def initialize(options = {})
  if options.is_a? String
    message = options
  else
    @query  = options[:query]
    @record = options[:record]
    @policy = options[:policy]

    message = options.fetch(:message) do
      record_name = record.is_a?(Class) ? record.to_s : "this #{record.class}"
      "not allowed to #{policy.class}##{query} #{record_name}"
    end
  end

  super(message)
end

Instance Attribute Details

#policyObject (readonly)

See Also:

Since:

  • v0.2.3



21
22
23
# File 'lib/pundit/error.rb', line 21

def policy
  @policy
end

#queryObject (readonly)

See Also:

Since:

  • v0.2.3



15
16
17
# File 'lib/pundit/error.rb', line 15

def query
  @query
end

#recordObject (readonly)

See Also:

Since:

  • v0.2.3



18
19
20
# File 'lib/pundit/error.rb', line 18

def record
  @record
end