Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
coorasse committed Dec 29, 2020
2 parents f804a26 + 619a57f commit b6012b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/cancan/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def extract_subjects(subject)

def alternative_subjects(subject)
subject = subject.class unless subject.is_a?(Module)
if subject.respond_to?(:subclasses) && subject < ActiveRecord::Base
if subject.respond_to?(:subclasses) && defined?(ActiveRecord::Base) && subject < ActiveRecord::Base
[:all, *(subject.ancestors + subject.subclasses), subject.class.to_s]
else
[:all, *subject.ancestors, subject.class.to_s]
Expand Down
18 changes: 13 additions & 5 deletions lib/cancan/model_adapters/sti_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ class StiNormalizer
class << self
def normalize(rules)
rules_cache = []
rules.delete_if.with_index do |rule, _index|
subjects = rule.subjects.select do |subject|
next if subject == :all || subject.descends_from_active_record?
return unless defined?(ActiveRecord::Base)

rules_cache.push(build_rule_for_subclass(rule, subject))
true
rules.delete_if do |rule|
subjects = rule.subjects.select do |subject|
update_rule(subject, rule, rules_cache)
end
subjects.length == rule.subjects.length
end
Expand All @@ -20,6 +19,15 @@ def normalize(rules)

private

def update_rule(subject, rule, rules_cache)
return false unless subject.respond_to?(:descends_from_active_record?)
return false if subject == :all || subject.descends_from_active_record?
return false unless subject < ActiveRecord::Base

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)
CanCan::Rule.new(rule.base_behavior, rule.actions, subject.superclass,
Expand Down
Loading

0 comments on commit b6012b1

Please sign in to comment.