Sha256: 363d6bcb5a36bbb1e5329bf9819e45e4740e9297a2304e628d4f5ebe10e29bff

Contents?: true

Size: 848 Bytes

Versions: 4

Compression:

Stored size: 848 Bytes

Contents

# 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
      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

4 entries across 4 versions & 1 rubygems

Version Path
cancancan-3.3.0 lib/cancan/class_matcher.rb
cancancan-3.2.2 lib/cancan/class_matcher.rb
cancancan-3.2.1 lib/cancan/class_matcher.rb
cancancan-3.2.0 lib/cancan/class_matcher.rb