Skip to content

Commit

Permalink
Satisfy Rubocop
Browse files Browse the repository at this point in the history
This extracts a lot of stuff into methods. I sure am hopeful this makes
the code more readable!
  • Loading branch information
mamhoff committed Jun 28, 2021
1 parent b0f0436 commit 4f8bc1c
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions lib/cancan/model_adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(relation_or_class, rules)
@compressed_rules = RulesCompressor.new(@rules.reverse).rules_collapsed.reverse
StiNormalizer.normalize(@compressed_rules)
ConditionsNormalizer.normalize(@model_class, @compressed_rules)
# run the extractor on the reversed set of rules to fill the cache properly
@conditions_extractor = ConditionsExtractor.new(@model_class).tap do |extractor|
@compressed_rules.reverse_each { |rule| extractor.tableize_conditions(rule.conditions) }
end
end

def database_records
Expand All @@ -28,19 +32,8 @@ def build_relation
@build_relation ||= begin
return @model_class.none if @compressed_rules.empty?

# run the extractor on the reversed set of rules to fill the cache properly
conditions_extractor = ConditionsExtractor.new(@model_class)
@compressed_rules.reverse_each { |rule| conditions_extractor.tableize_conditions(rule.conditions) }

positive_rules, negative_rules = @compressed_rules.partition(&:base_behavior)

negative_conditions = negative_rules.map { |rule| rule_to_relation(rule, conditions_extractor) }.compact
positive_conditions = positive_rules.map { |rule| rule_to_relation(rule, conditions_extractor) }.compact

@relation = @relation.merge(positive_conditions.reduce(&:or)) if positive_conditions.present?
if negative_conditions.present?
@relation = @relation.where.not(negative_conditions.reduce(:or).where_clause.ast)
end
@relation = merge_positive_conditions
@relation = merge_negative_conditions

build_joins_relation(@relation)
end
Expand All @@ -58,11 +51,45 @@ def joins

private

def rule_to_relation(rule, conditions_extractor)
def rule_to_relation(rule)
if rule.conditions.is_a? ActiveRecord::Relation
rule.conditions
elsif rule.conditions.present?
@model_class.where(conditions_extractor.tableize_conditions(rule.conditions))
@model_class.where(@conditions_extractor.tableize_conditions(rule.conditions))
end
end

def positive_conditions
@positive_conditions ||= begin
@compressed_rules
.select(&:base_behavior)
.map { |rule| rule_to_relation(rule) }
.compact
end
end

def negative_conditions
@negative_conditions ||= begin
@compressed_rules
.reject(&:base_behavior)
.map { |rule| rule_to_relation(rule) }
.compact
end
end

def merge_positive_conditions
if positive_conditions.present?
@relation.merge(positive_conditions.reduce(&:or))
else
@relation
end
end

def merge_negative_conditions
if negative_conditions.present?
@relation.where.not(negative_conditions.reduce(:or).where_clause.ast)
else
@relation
end
end

Expand Down

0 comments on commit 4f8bc1c

Please sign in to comment.