lib/action_policy/testing.rb in action_policy-0.2.4 vs lib/action_policy/testing.rb in action_policy-0.3.0.beta1

- old
+ new

@@ -22,15 +22,54 @@ def inspect "#{policy.record.inspect} was authorized with #{policy.class}##{rule}" end end + class Scoping # :nodoc: + attr_reader :policy, :target, :type, :name, :scope_options + + def initialize(policy, target, type, name, scope_options) + @policy = policy + @target = target + @type = type + @name = name + @scope_options = scope_options + end + + def matches?(policy_class, actual_type, actual_name, actual_scope_options) + policy_class == policy.class && + type == actual_type && + name == actual_name && + actual_scope_options === scope_options + end + + def inspect + "#{policy.class} :#{name} for :#{type} #{scope_options_message}" + end + + private + + def scope_options_message + if scope_options + if defined?(::RSpec::Matchers::Composable) && + scope_options.is_a?(::RSpec::Matchers::Composable) + "with scope options #{scope_options.description}" + else + "with scope options #{scope_options}" + end + else + "without scope options" + end + end + end + class << self # Wrap code under inspection into this method # to track authorize! calls def tracking calls.clear + scopings.clear Thread.current[:__action_policy_tracking] = true yield ensure Thread.current[:__action_policy_tracking] = false end @@ -39,23 +78,38 @@ def track(policy, rule) return unless tracking? calls << Call.new(policy, rule) end + # Called from Authorizer + def track_scope(target, policy, type:, name:, scope_options:) + return unless tracking? + scopings << Scoping.new(policy, target, type, name, scope_options) + end + def calls Thread.current[:__action_policy_calls] ||= [] end + def scopings + Thread.current[:__action_policy_scopings] ||= [] + 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 + + def scopify(*args) + AuthorizeTracker.track_scope(*args) super end end ActionPolicy::Authorizer.singleton_class.prepend(AuthorizerExt)