Class: ActiveRecord::HierarchicalQuery::Orderings

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_record/hierarchical_query/orderings.rb

Constant Summary collapse

NATURAL_SORT_TYPES =
Set[
    :integer, :float, :decimal,
    :datetime, :timestamp, :time, :date,
    :boolean, :itet, :cidr, :ltree
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order_values, table) ⇒ Orderings

Returns a new instance of Orderings.

Parameters:

  • order_values (Array)
  • table (Arel::Table)


16
17
18
19
# File 'lib/active_record/hierarchical_query/orderings.rb', line 16

def initialize(order_values, table)
  @order_values, @table = order_values, table
  @values = nil # cached orderings
end

Instance Attribute Details

#order_valuesObject (readonly)

Returns the value of attribute order_values.



12
13
14
# File 'lib/active_record/hierarchical_query/orderings.rb', line 12

def order_values
  @order_values
end

#tableObject (readonly)

Returns the value of attribute table.



12
13
14
# File 'lib/active_record/hierarchical_query/orderings.rb', line 12

def table
  @table
end

Instance Method Details

#each(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_record/hierarchical_query/orderings.rb', line 21

def each(&block)
  return @values.each(&block) if @values
  return enum_for(__method__) unless block_given?

  @values = []

  order_values.each do |value|
    Array.wrap(as_orderings(value)).each do |ordering|
      @values << ordering
    end
  end

  @values.each(&block)
end

#row_number_expressionArel::Nodes::Node

Returns order expression to be inserted into SELECT clauses of both non-recursive and recursive terms.

Returns:

  • (Arel::Nodes::Node)

    order expression



40
41
42
43
44
45
46
# File 'lib/active_record/hierarchical_query/orderings.rb', line 40

def row_number_expression
  if raw_ordering?
    order_attribute
  else
    Arel.sql("ROW_NUMBER() OVER (ORDER BY #{map(&:to_sql).join(', ')})")
  end
end