Class: OrderQuery::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/order_query/space.rb

Overview

Order specification and a scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_scope, order_spec) ⇒ Space

Returns a new instance of Space.

Parameters:

  • scope (ActiveRecord::Relation)
  • order_spec (Array<Array<Symbol,String>>)


13
14
15
16
17
18
# File 'lib/order_query/space.rb', line 13

def initialize(base_scope, order_spec)
  @base_scope   = base_scope
  order_spec    = [order_spec] unless order_spec.empty? || order_spec.first.is_a?(Array)
  @conditions   = order_spec.map { |spec| Condition.new(spec, base_scope) }
  @order_by_sql = SQL::OrderBy.new(self)
end

Instance Attribute Details

#base_scopeActiveRecord::Relation (readonly)

Returns:

  • (ActiveRecord::Relation)


9
10
11
# File 'lib/order_query/space.rb', line 9

def base_scope
  @base_scope
end

#conditionsArray<Condition> (readonly)

Returns:



7
8
9
# File 'lib/order_query/space.rb', line 7

def conditions
  @conditions
end

Instance Method Details

#reverse_scopeActiveRecord::Relation

Returns scope ordered by conditions in reverse.

Returns:

  • (ActiveRecord::Relation)

    scope ordered by conditions in reverse



26
27
28
# File 'lib/order_query/space.rb', line 26

def reverse_scope
  @base_scope.order(@order_by_sql.build_reverse)
end

#scopeActiveRecord::Relation

Returns scope ordered by conditions.

Returns:

  • (ActiveRecord::Relation)

    scope ordered by conditions



21
22
23
# File 'lib/order_query/space.rb', line 21

def scope
  @base_scope.order(@order_by_sql.build)
end