Sha256: 4cf741d15a003441957a471c0b265e96536c1cfa5a7a1d8835e5026bda9f8570

Contents?: true

Size: 1.77 KB

Versions: 20

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
module Kadmin
  class ApplicationController < ActionController::Base
    layout 'kadmin/application'

    helper Kadmin::ApplicationHelper
    helper Kadmin::BootstrapHelper
    helper Kadmin::AlertHelper

    include Kadmin::Concerns::AuthorizedUser

    before_action :authorize
    before_action :set_default_format

    # Each controller should specify which navbar section they
    # belong to, if any. By default, each controller is setup to
    # be its own section.
    class_attribute :navbar_section

    # @!group Error Handling

    if Kadmin.config.handle_errors && !defined?(BetterErrors)
      rescue_from StandardError, with: :handle_unexpected_error
      rescue_from ActiveRecord::RecordNotFound, with: :not_found
      rescue_from ActionController::ParameterMissing, with: :params_missing
    end

    def params_missing(error)
      handle_error(error, title: I18n.t('kadmin.errors.params_missing'), status: :bad_request)
    end

    def not_found(error)
      handle_error(error, title: I18n.t('kadmin.errors.not_found'), status: :not_found)
    end

    def handle_unexpected_error(error)
      Rails.logger.error(error)
      handle_error(error, title: I18n.t('kadmin.errors.unexpected'), message: I18n.t('kadmin.errors.unexpected_message'))
    end

    def handle_error(error, options = {})
      options = {
        title: error.try(:title) || error.class.name,
        message: error.message,
        status: :internal_server_error,
        error: error
      }.merge(options)
      render 'kadmin/error', status: options[:status], locals: options
    end

    # @!endgroup

    # @!group Helpers

    def set_default_format
      params[:format] = 'html' if params[:format].blank?
    end
    protected :set_default_format

    # @!endgroup
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
kadmin-0.6.19 app/controllers/kadmin/application_controller.rb
kadmin-0.6.18 app/controllers/kadmin/application_controller.rb
kadmin-0.6.17 app/controllers/kadmin/application_controller.rb
kadmin-0.6.16 app/controllers/kadmin/application_controller.rb
kadmin-0.6.13 app/controllers/kadmin/application_controller.rb
kadmin-0.6.12 app/controllers/kadmin/application_controller.rb
kadmin-0.6.11 app/controllers/kadmin/application_controller.rb
kadmin-0.6.10 app/controllers/kadmin/application_controller.rb
kadmin-0.6.9 app/controllers/kadmin/application_controller.rb
kadmin-0.6.8 app/controllers/kadmin/application_controller.rb
kadmin-0.6.7 app/controllers/kadmin/application_controller.rb
kadmin-0.6.6 app/controllers/kadmin/application_controller.rb
kadmin-0.6.5 app/controllers/kadmin/application_controller.rb
kadmin-0.6.4 app/controllers/kadmin/application_controller.rb
kadmin-0.6.3 app/controllers/kadmin/application_controller.rb
kadmin-0.6.1 app/controllers/kadmin/application_controller.rb
kadmin-0.5.7 app/controllers/kadmin/application_controller.rb
kadmin-0.5.6 app/controllers/kadmin/application_controller.rb
kadmin-0.4.2 app/controllers/kadmin/application_controller.rb
kadmin-0.4.1 app/controllers/kadmin/application_controller.rb