Class: ActiveRecord::HierarchicalQuery::CTE::CycleDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/hierarchical_query/cte/cycle_detector.rb

Constant Summary collapse

COLUMN_NAME =
'__path'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ CycleDetector

Returns a new instance of CycleDetector.



10
11
12
# File 'lib/active_record/hierarchical_query/cte/cycle_detector.rb', line 10

def initialize(query)
  @query = query
end

Instance Method Details

#apply_to_non_recursive(arel) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/active_record/hierarchical_query/cte/cycle_detector.rb', line 14

def apply_to_non_recursive(arel)
  if enabled?
    arel.project Arel::Nodes::PostgresArray.new([primary_key]).as(column_name)
  end

  arel
end

#apply_to_recursive(arel) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/active_record/hierarchical_query/cte/cycle_detector.rb', line 22

def apply_to_recursive(arel)
  if enabled?
    arel.project Arel::Nodes::ArrayConcat.new(parent_column, primary_key)
    arel.constraints << Arel::Nodes::Not.new(primary_key.eq(any(parent_column)))
  end

  arel
end