Class: OrderQuery::RelativeOrder
- Inherits:
-
Object
- Object
- OrderQuery::RelativeOrder
- Defined in:
- lib/order_query/relative_order.rb
Overview
Search around a record in a scope
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #after ⇒ ActiveRecord::Relation
- #before ⇒ ActiveRecord::Relation
- #count ⇒ Integer
- #first ⇒ ActiveRecord::Base
-
#initialize(record, order_space) ⇒ RelativeOrder
constructor
A new instance of RelativeOrder.
- #last ⇒ ActiveRecord::Base
- #next(loop = true) ⇒ ActiveRecord::Base
- #position ⇒ Integer
- #previous(loop = true) ⇒ ActiveRecord::Base
- #records(direction) ⇒ ActiveRecord::Relation
Constructor Details
#initialize(record, order_space) ⇒ RelativeOrder
Returns a new instance of RelativeOrder.
13 14 15 16 17 |
# File 'lib/order_query/relative_order.rb', line 13 def initialize(record, order_space) @record = record @order = order_space @query_builder = WhereBuilder.new record, order_space end |
Instance Attribute Details
#order ⇒ Object (readonly)
Returns the value of attribute order.
8 9 10 |
# File 'lib/order_query/relative_order.rb', line 8 def order @order end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
8 9 10 |
# File 'lib/order_query/relative_order.rb', line 8 def record @record end |
Instance Method Details
#after ⇒ ActiveRecord::Relation
51 52 53 |
# File 'lib/order_query/relative_order.rb', line 51 def after records :after end |
#before ⇒ ActiveRecord::Relation
56 57 58 |
# File 'lib/order_query/relative_order.rb', line 56 def before records :before end |
#count ⇒ Integer
30 31 32 |
# File 'lib/order_query/relative_order.rb', line 30 def count @total ||= scope.count end |
#first ⇒ ActiveRecord::Base
20 21 22 |
# File 'lib/order_query/relative_order.rb', line 20 def first scope.first end |
#last ⇒ ActiveRecord::Base
25 26 27 |
# File 'lib/order_query/relative_order.rb', line 25 def last reverse_scope.first end |
#next(loop = true) ⇒ ActiveRecord::Base
41 42 43 |
# File 'lib/order_query/relative_order.rb', line 41 def next(loop = true) unless_record_eq after.first || (first if loop) end |
#position ⇒ Integer
35 36 37 |
# File 'lib/order_query/relative_order.rb', line 35 def position count - after.count end |
#previous(loop = true) ⇒ ActiveRecord::Base
46 47 48 |
# File 'lib/order_query/relative_order.rb', line 46 def previous(loop = true) unless_record_eq before.first || (last if loop) end |
#records(direction) ⇒ ActiveRecord::Relation
62 63 64 65 66 67 68 69 70 |
# File 'lib/order_query/relative_order.rb', line 62 def records(direction) scope = (direction == :after ? order.scope : order.reverse_scope) query, query_args = @query_builder.build_query(direction) if query.present? scope.where(query, *query_args) else scope end end |