class Babik::QuerySet::Order

Manages the order of the QuerySet

Attributes

order_fields[R]

Public Class Methods

new(model, *ordering) click to toggle source

Construct the order manager @param model [ActiveRecord::Base] base model. @param ordering [Array, String, Hash] ordering that will be applied to the QuerySet.

Each item of the array represents an order:
- field1: field1 ASC
- -field1: field1 DESC
- [field1, :ASC]: field1 ASC
- [field1, :DESC]: field1 DESC
- {field1, :ASC}: field1 ASC
- {field1, :DESC}: field1 DESC

@raise [RuntimeError] Invalid type of order

# 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

Public Instance Methods

_initialize_field_orders(order) click to toggle source

Initialize the order paths @api private @return [Array] Conversion of order as hash to array.

# 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) click to toggle source

Get order from a hash @param order [Hash] The string of the form <field>: <ORD> (where <ORD> is :ASC or :DESC) @return [Array] Conversion of order as hash to array.

# 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) click to toggle source

Get order from string @param order [String] The string of the form 'field1' @api private @return [Array] Conversion of order as string to array.

# 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) click to toggle source

Get order from symbol @param order [Symbol] The symbol of the form :field1 @api private @return [Array] Conversion of order as symbol to array.

# File lib/babik/queryset/components/order.rb, line 52
def _order_from_symbol(order)
  _order_from_string(order.to_s)
end
invert() click to toggle source

Return an direction inversion of this order e.g.

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

@return [Array<OrderField>] Inverted order.

# File lib/babik/queryset/components/order.rb, line 83
def invert
  @order_fields.map(&:invert)
end
invert!() click to toggle source

Invert actual order direction

# File lib/babik/queryset/components/order.rb, line 88
def invert!
  @order_fields = self.invert
end
left_joins_by_alias() click to toggle source

Return the left joins this order include, grouped by alias @return [Hash] Hash with the key equal to alias and the value equals to a Join.

# 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
sql() click to toggle source

Return sql of the fields to order. Does not include ORDER BY. @return [SQL] SQL code for fields to order.

# File lib/babik/queryset/components/order.rb, line 105
def sql
  @order_fields.map(&:sql).join(', ')
end