Sha256: fcda4b5829022c5b8562f06711dff1cddfc66886a9e1ab86b4166c1beef395e8

Contents?: true

Size: 1.1 KB

Versions: 27

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module LinkedRails
  module Errors
    class Forbidden < StandardError
      attr_reader :query, :record, :policy, :action

      # @param [Hash] options
      # @option options [String] query The action of the request
      # @option options [ActiveRecord::Base] record The record that was requested
      # @option options [Policy] policy The policy that raised the exception
      # @option options [String] message Override the default error message
      # @return [String] the message
      def initialize(**options)
        @query  = options.fetch(:query).to_s
        @record = options[:record]
        @policy = options[:policy]
        @action = @query[-1] == '?' ? @query[0..-2] : @query
        @message = options[:message]

        raise StandardError if @query.blank? && @message.blank?

        super(@message || default_message)
      end

      private

      def default_message
        I18n.t(
          "pundit.#{@policy.class.to_s.underscore}.#{@query}",
          action: @action,
          default: I18n.t('errors.access_denied')
        )
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
linked_rails-0.0.4.pre.gb0b6aea40 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g792a1b887 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g58af47992 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g0ec6e30ca lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g9e3bcac55 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g222bfeeca lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.gb254a2893 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g2c6c26e86 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.ge881247c3 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.gfabf7e9f5 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g8c045be3a lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g8d7195543 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.gfe77aae19 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.gc866c1806 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g3e8f5731a lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g58e27be7f lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g96885856b lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g1faca9643 lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g4c8e21c9f lib/linked_rails/errors/forbidden.rb
linked_rails-0.0.4.pre.g76dedd8ba lib/linked_rails/errors/forbidden.rb