Module: ActionPolicy::Policy::Rails::Instrumentation

Defined in:
lib/action_policy/rails/policy/instrumentation.rb

Overview

Add ActiveSupport::Notifications support.

Fires ‘action_policy.apply_rule` event on every `#apply` call. Fires `action_policy.init` event on every policy initialization.

Constant Summary collapse

INIT_EVENT_NAME =
"action_policy.init"
APPLY_EVENT_NAME =
"action_policy.apply_rule"

Instance Method Summary collapse

Instance Method Details

#apply(rule) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/action_policy/rails/policy/instrumentation.rb', line 19

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

#initialize(record = nil, **params) ⇒ Object



14
15
16
17
# File 'lib/action_policy/rails/policy/instrumentation.rb', line 14

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