Sha256: 42eea6daef439ae19c46bfe1693eb71ce74eb3a88e2870e0dbd1041cf19b04ed
Contents?: true
Size: 1.5 KB
Versions: 9
Compression:
Stored size: 1.5 KB
Contents
# frozen_string_literal: true module ActionPolicy # Testing utils module Testing # Collects all Authorizer calls module AuthorizeTracker class Call # :nodoc: attr_reader :policy, :rule def initialize(policy, rule) @policy = policy @rule = rule end def matches?(policy_class, actual_rule, target) policy_class == policy.class && target == policy.record && rule == actual_rule end def inspect "#{policy.record.inspect} was authorized with #{policy.class}##{rule}" end end class << self # Wrap code under inspection into this method # to track authorize! calls def tracking calls.clear Thread.current[:__action_policy_tracking] = true yield ensure Thread.current[:__action_policy_tracking] = false end # Called from Authorizer def track(policy, rule) return unless tracking? calls << Call.new(policy, rule) end def calls Thread.current[:__action_policy_calls] ||= [] end def tracking? Thread.current[:__action_policy_tracking] == true end end end # Extend authorizer to add tracking functionality module AuthorizerExt def call(*args) AuthorizeTracker.track(*args) super end end ActionPolicy::Authorizer.singleton_class.prepend(AuthorizerExt) end end
Version data entries
9 entries across 9 versions & 1 rubygems