Sha256: 0fca534a98105e6639e751f0d022cd6b44db2952d92212145def57b5817e835a

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module Chaltron
  module Controllers
    # Those helpers are convenience methods added to ApplicationController.
    module Helpers
      extend ActiveSupport::Concern
      included do
        rescue_from ::CanCan::AccessDenied do |exception|
          alert = current_user.nil? ? t('chaltron.access_denied_try_login') : t('chaltron.access_denied')
          redirect_to root_url, alert: alert
        end
      end

      module ClassMethods
        def log_category
          defined?(@log_category) ? @log_category : self.to_s.downcase
        end

        def default_log_category(cat)
          @log_category = cat.to_s
        end
      end

      #
      # Utilities for logging
      #
      def info(message, category = nil)
        create_log_message(message, category, :info)
      end

      def debug(message, category = nil)
        create_log_message(message, category, :debug)
      end

      def error(message, category = nil)
        create_log_message(message, category, :error)
      end

      private
      def create_log_message(message, category, severity)
        category ||= self.class.log_category
        ::Log.create(
          message: message,
          category: category.to_s,
          severity: severity.to_s
        )
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chaltron-0.1.1 lib/chaltron/controllers/helpers.rb
chaltron-0.1.0 lib/chaltron/controllers/helpers.rb