Sha256: b26f464494c2ff1551c4fa004d28fc4c82cad4cacbb04088e55971d47af489e2

Contents?: true

Size: 938 Bytes

Versions: 3

Compression:

Stored size: 938 Bytes

Contents

require_relative 'sti_detector'

# This class is responsible for matching classes and their subclasses as well as
# upmatching classes to their ancestors.
# This is used to generate sti connections
class SubjectClassMatcher
  def self.matches_subject_class?(subjects, subject)
    subjects.any? do |sub|
      has_subclasses = subject.respond_to?(:subclasses)
      matching_class_check(subject, sub, has_subclasses)
    end
  end

  def self.matching_class_check(subject, sub, has_subclasses)
    matches = matches_class_or_is_related(subject, sub)
    if has_subclasses
      return matches unless StiDetector.sti_class?(sub)

      matches || subject.subclasses.include?(sub)
    else
      matches
    end
  end

  def self.matches_class_or_is_related(subject, sub)
    sub.is_a?(Module) && (subject.is_a?(sub) ||
        subject.class.to_s == sub.to_s ||
        (subject.is_a?(Module) && subject.ancestors.include?(sub)))
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/cancancan-3.6.1/lib/cancan/class_matcher.rb
cancancan-3.5.0 lib/cancan/class_matcher.rb
cancancan-3.4.0 lib/cancan/class_matcher.rb