Sha256: 8be08f90c8196b29198e6ebecfdfbd533cf5db2f90f74e314a177869aa28fecb

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require_relative '../sti_detector'

# this class is responsible for detecting sti classes and creating new rules for the
# relevant subclasses, using the inheritance_column as a merger
module CanCan
  module ModelAdapters
    class StiNormalizer
      class << self
        def normalize(rules)
          rules_cache = []
          return unless defined?(ActiveRecord::Base)

          rules.delete_if do |rule|
            subjects = rule.subjects.select do |subject|
              update_rule(subject, rule, rules_cache)
            end
            subjects.length == rule.subjects.length
          end
          rules_cache.each { |rule| rules.push(rule) }
        end

        private

        def update_rule(subject, rule, rules_cache)
          return false unless StiDetector.sti_class?(subject)

          rules_cache.push(build_rule_for_subclass(rule, subject))
          true
        end

        # create a new rule for the subclasses that links on the inheritance_column
        def build_rule_for_subclass(rule, subject)
          sti_conditions = { subject.inheritance_column => subject.sti_name }
          new_rule_conditions =
            if rule.with_scope?
              rule.conditions.where(sti_conditions)
            else
              rule.conditions.merge(sti_conditions)
            end

          CanCan::Rule.new(rule.base_behavior, rule.actions, subject.superclass,
                           new_rule_conditions, rule.block)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

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