Sha256: 392b29fd99cd9fa06a120ae203c7c140edf00c0861b26e57c6ad36d04509ce48

Contents?: true

Size: 965 Bytes

Versions: 1

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

module ActionPolicy # :nodoc:
  module Policy
    module Rails
      # Add ActiveSupport::Notifications support.
      #
      # Fires `action_policy.apply_rule` event on every `#apply` call.
      # Fires `action_policy.init` event on every policy initialization.
      module Instrumentation
        INIT_EVENT_NAME = "action_policy.init"
        APPLY_EVENT_NAME = "action_policy.apply_rule"

        def initialize(record = nil, **params)
          event = {policy: self.class.name}
          ActiveSupport::Notifications.instrument(INIT_EVENT_NAME, event) { super }
        end

        def apply_r(rule)
          event = {policy: self.class.name, rule: rule.to_s}
          ActiveSupport::Notifications.instrument(APPLY_EVENT_NAME, event) do
            result = super
            event[:cached] = result.cached?
            event[:value] = result.value
            result
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
action_policy-0.7.3 lib/action_policy/rails/policy/instrumentation.rb