module CanCanExplainer::RuleExtensions

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/cancan_explainer/cancan_extensions.rb, line 5
def initialize(*args)
  @source_location = determine_source_location_from(caller)
  super
end

Public Instance Methods

determine_source_location_from(caller) click to toggle source

The source location of the rule is found in the stack trace entry immediately prior to `Ability#can` (or `cannot`).

# File lib/cancan_explainer/cancan_extensions.rb, line 12
def determine_source_location_from(caller)
  index = caller.index { |line|
    line =~ %r{lib/cancan/ability\.rb:\d+:in `(can|cannot)'}
  }
  index.nil? ? nil : caller[index + 1]
end
matches_conditions?(action, subject, extra_args) click to toggle source
Calls superclass method
# File lib/cancan_explainer/cancan_extensions.rb, line 19
def matches_conditions?(action, subject, extra_args)
  result = super
  CanCanExplainer.
    explanation&.
    add_condition_match(@source_location, action, subject, extra_args)
  result
end