module NoBrainer::Criteria::OrderBy

Public Instance Methods

order(*rules, &block)
Alias for: order_by
order_by(*rules, &block) click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 8
def order_by(*rules, &block)
  # Note: We are relying on the fact that Hashes are ordered (since 1.9)
  rules = [*rules, block].flatten.compact.map do |rule|
    case rule
    when Hash then
      bad_rule = rule.values.reject { |v| v.in? [:asc, :desc] }.first
      raise_bad_rule(bad_rule) if bad_rule
      rule
    when String, Symbol, Proc then { rule => :asc }
    else raise_bad_rule(rule)
    end
  end.reduce({}, :merge)

  rules.keys.each { |k| model.ensure_valid_key!(k) unless k.is_a?(Proc) } if model

  chain(:order_by => rules, :ordering_mode => :explicit,
                            :reversed_ordering => false)
end
Also aliased as: order
order_by_index_name() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 41
def order_by_index_name
  order_by_index_finder.index_name
end
order_by_indexed?() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 37
def order_by_indexed?
  !!order_by_index_name
end
reverse_order() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 33
def reverse_order
  chain(:reversed_ordering => !@options[:reversed_ordering])
end
without_ordering() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 29
def without_ordering
  chain(:ordering_mode => :disabled)
end

Private Instance Methods

compile_rql_pass1() click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/order_by.rb, line 101
def compile_rql_pass1
  rql = super
  _effective_order = effective_order
  return rql unless _effective_order.present?

  rql_rules = _effective_order.map do |k,v|
    if order_by_index_finder.index_name == k
      k = model.lookup_index_alias(k)
    else
      k = model.lookup_field_alias(k)
    end

    case v
    when :asc  then reverse_order? ? RethinkDB::RQL.new.desc(k) : RethinkDB::RQL.new.asc(k)
    when :desc then reverse_order? ? RethinkDB::RQL.new.asc(k)  : RethinkDB::RQL.new.desc(k)
    end
  end

  # We can only apply an indexed order_by on a table() RQL term.
  # If we can, great. Otherwise, the ordering is applied in pass2, which will
  # happen after a potential filter(), which is better for perfs.
  if order_by_index_finder.could_find_index?
    options = { :index => rql_rules.shift }
    rql = rql.order_by(*rql_rules, options)
  else
    @rql_rules_pass2 = rql_rules
  end

  rql
end
compile_rql_pass2() click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/order_by.rb, line 132
def compile_rql_pass2
  rql = super
  if @rql_rules_pass2
    rql = rql.order_by(*@rql_rules_pass2)
    @rql_rules_pass2 = nil
  end
  rql
end
effective_order() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 55
def effective_order
  # reversing the order happens later.
  case ordering_mode
  when :disabled then nil
  when :explicit then @options[:order_by]
  when :implicit then model && {model.pk_name => :asc}
  end
end
order_by_index_finder() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 96
def order_by_index_finder
  return finalized_criteria.__send__(:order_by_index_finder) unless finalized?
  @order_by_index_finder ||= IndexFinder.new(self).tap(&:find_index)
end
ordering_mode() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 47
def ordering_mode
  @options[:ordering_mode] || :implicit
end
raise_bad_rule(rule) click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 141
def raise_bad_rule(rule)
  raise "order_by() takes arguments such as `:field1 => :desc, :field2 => :asc', not `#{rule}'"
end
reverse_order?() click to toggle source
# File lib/no_brainer/criteria/order_by.rb, line 51
def reverse_order?
  !!@options[:reversed_ordering]
end