Class: Babik::QuerySet::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/components/order.rb

Overview

Manages the order of the QuerySet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, *ordering) ⇒ Order

Construct the order manager

Raises:

  • (RuntimeError)

    Invalid type of order



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/babik/queryset/components/order.rb', line 24

def initialize(model, *ordering)
  @model = model
  # Convert the types of each order field
  order_as_array_or_pairs = ordering.map do |order|
    if [Hash, String, Symbol].include?(order.class)
      self.send("_order_from_#{order.class.to_s.downcase}", order)
    elsif order.class == Array
      order
    else
      raise "Invalid type of order: #{order}"
    end
  end
  _initialize_field_orders(order_as_array_or_pairs)
end

Instance Attribute Details

#order_fieldsObject (readonly)

Returns the value of attribute order_fields.



11
12
13
# File 'lib/babik/queryset/components/order.rb', line 11

def order_fields
  @order_fields
end

Instance Method Details

#_initialize_field_orders(order) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the order paths



69
70
71
72
73
74
75
76
77
# File 'lib/babik/queryset/components/order.rb', line 69

def _initialize_field_orders(order)
  # Check
  @order_fields = []
  order.each_with_index do |order_field_direction, _order_field_index|
    order_field_path = order_field_direction[0]
    order_direction = order_field_direction[1]
    @order_fields << OrderField.new(@model, order_field_path, order_direction)
  end
end

#_order_from_hash(order) ⇒ Array

Get order from a hash



59
60
61
62
63
64
# File 'lib/babik/queryset/components/order.rb', line 59

def _order_from_hash(order)
  raise "More than one key found in order by for class #{self.class}" if order.keys.length > 1
  order_field = order.keys[0]
  order_value = order[order_field]
  [order_field, order_value]
end

#_order_from_string(order) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get order from string



43
44
45
46
# File 'lib/babik/queryset/components/order.rb', line 43

def _order_from_string(order)
  return [order, :ASC] if order[0] != '-'
  [order[1..-1], :DESC]
end

#_order_from_symbol(order) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get order from symbol



52
53
54
# File 'lib/babik/queryset/components/order.rb', line 52

def _order_from_symbol(order)
  _order_from_string(order.to_s)
end

#invertArray<OrderField>

Return an direction inversion of this order e.g.

User, first_name, ASC => invert => User, first_name, DESC


83
84
85
# File 'lib/babik/queryset/components/order.rb', line 83

def invert
  @order_fields.map(&:invert)
end

#invert!Object

Invert actual order direction



88
89
90
# File 'lib/babik/queryset/components/order.rb', line 88

def invert!
  @order_fields = self.invert
end

#left_joins_by_aliasHash

Return the left joins this order include, grouped by alias



94
95
96
97
98
99
100
# File 'lib/babik/queryset/components/order.rb', line 94

def left_joins_by_alias
  left_joins_by_alias = {}
  @order_fields.each do |order_field|
    left_joins_by_alias.merge!(order_field.left_joins_by_alias)
  end
  left_joins_by_alias
end

#sqlSQL

Return sql of the fields to order. Does not include ORDER BY.



105
106
107
# File 'lib/babik/queryset/components/order.rb', line 105

def sql
  @order_fields.map(&:sql).join(', ')
end