Thursday, September 19, 2024 11:51:51 PM
> settings

Customize


Authenticate

> application_controller.rb
# frozen_string_literal: true

class ApplicationController < ActionController::Base
  include Pundit::Authorization

  GIT_REV = lambda do
    path = Rails.root.join("REVISION")
    if path.exist?
      path.read.chomp[..6]
    else
      `git rev-parse --short HEAD`.chomp
    end
  end.call

  rescue_from Pundit::NotAuthorizedError, with: :rescue_from_pundit_not_authorized

  def redirect_not_found!
    raise ActionController::RoutingError.new("Not Found")
  end

  protected

  def guest_user
    @guest_user ||= User.guest_user
  end
  helper_method :guest_user

  def current_or_guest_user
    current_user || guest_user
  end
  helper_method :current_or_guest_user

  def pundit_user
    current_or_guest_user
  end

  private

  def rescue_from_pundit_not_authorized
    flash[:alert] = "You are not authorized to perform this action."
    redirect_back(fallback_location: root_path)
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b