Module: ActionPolicy::Testing::AuthorizeTracker

Defined in:
lib/action_policy/testing.rb

Overview

Collects all Authorizer calls

Defined Under Namespace

Classes: Call, Scoping

Class Method Summary collapse

Class Method Details

.callsObject



98
99
100
# File 'lib/action_policy/testing.rb', line 98

def calls
  Thread.current[:__action_policy_calls] ||= []
end

.scopingsObject



102
103
104
# File 'lib/action_policy/testing.rb', line 102

def scopings
  Thread.current[:__action_policy_scopings] ||= []
end

.track(policy, rule) ⇒ Object

Called from Authorizer



87
88
89
90
# File 'lib/action_policy/testing.rb', line 87

def track(policy, rule)
  return unless tracking?
  calls << Call.new(policy, rule)
end

.track_scope(target, policy, type:, name:, scope_options:) ⇒ Object

Called from Authorizer



93
94
95
96
# File 'lib/action_policy/testing.rb', line 93

def track_scope(target, policy, type:, name:, scope_options:)
  return unless tracking?
  scopings << Scoping.new(policy, target, type, name, scope_options)
end

.trackingObject

Wrap code under inspection into this method to track authorize! calls



77
78
79
80
81
82
83
84
# File 'lib/action_policy/testing.rb', line 77

def tracking
  calls.clear
  scopings.clear
  Thread.current[:__action_policy_tracking] = true
  yield
ensure
  Thread.current[:__action_policy_tracking] = false
end

.tracking?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/action_policy/testing.rb', line 106

def tracking?
  Thread.current[:__action_policy_tracking] == true
end