Sha256: ba07f9fcef7d0a0263a0b1b7ca5545e919d1fa6c262992ca0ae56c641b75d7e5

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module Pundit
  module Logger
    # Once a controller action completes, the names of the
    # Pundit policies invoked are logged to the configured logger.
    #
    # Intended for mixing into Rails controllers
    #
    # Functionality controlled by
    # Pundit::Logger.log_controller_policy_summary; disable by setting
    # that to false.
    module LogControllerPolicySummary

      extend ActiveSupport::Concern

      included do
        after_action :log_policies_invoked,
          if: -> { Pundit::Logger.log_controller_policy_summary? }
      end

      private

      def log_policies_invoked
        policy_names = invoked_policy_names +
                         invoked_policy_scope_names
        message = if policy_names.empty?
                    'None'
                  else
                    policy_names.uniq.sort.join(', ')
                  end
        Pundit::Logger.log 'Controller Action Policies ' \
                           "Invoked: #{message}"
      end

      def invoked_policy_names
        return [] unless respond_to?(:policies)
        policies.map do |_, policy_obj|
          policy_obj.class.name
        end
      end

      def invoked_policy_scope_names
        Pundit.invoked_policy_scope_names.to_a
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pundit_logger-0.1.1 lib/pundit_logger/log_controller_policy_summary.rb
pundit_logger-0.1.0 lib/pundit_logger/log_controller_policy_summary.rb