Class: OrderQuery::Space
- Inherits:
-
Object
- Object
- OrderQuery::Space
- Defined in:
- lib/order_query/space.rb
Overview
Order specification and a scope
Instance Attribute Summary collapse
- #base_scope ⇒ ActiveRecord::Relation readonly
- #conditions ⇒ Array<Condition> readonly
Instance Method Summary collapse
-
#initialize(base_scope, order_spec) ⇒ Space
constructor
A new instance of Space.
-
#reverse_scope ⇒ ActiveRecord::Relation
Scope ordered by conditions in reverse.
-
#scope ⇒ ActiveRecord::Relation
Scope ordered by conditions.
Constructor Details
#initialize(base_scope, order_spec) ⇒ Space
Returns a new instance of Space.
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_scope ⇒ ActiveRecord::Relation (readonly)
9 10 11 |
# File 'lib/order_query/space.rb', line 9 def base_scope @base_scope end |
#conditions ⇒ Array<Condition> (readonly)
7 8 9 |
# File 'lib/order_query/space.rb', line 7 def conditions @conditions end |
Instance Method Details
#reverse_scope ⇒ ActiveRecord::Relation
Returns 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 |
#scope ⇒ ActiveRecord::Relation
Returns 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 |