Class: OrderQuery::Point
- Inherits:
-
Object
- Object
- OrderQuery::Point
- Defined in:
- lib/order_query/point.rb
Overview
Search around a record in an order space
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
Instance Method Summary collapse
- #after ⇒ ActiveRecord::Relation
- #before ⇒ ActiveRecord::Relation
- #count ⇒ Integer
- #first ⇒ ActiveRecord::Base
-
#initialize(record, space) ⇒ Point
constructor
A new instance of Point.
- #last ⇒ ActiveRecord::Base
- #next(loop = true) ⇒ ActiveRecord::Base
- #position ⇒ Integer
- #previous(loop = true) ⇒ ActiveRecord::Base
- #records(direction) ⇒ ActiveRecord::Relation
Constructor Details
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
7 8 9 |
# File 'lib/order_query/point.rb', line 7 def record @record end |
#space ⇒ Object (readonly)
Returns the value of attribute space.
7 8 9 |
# File 'lib/order_query/point.rb', line 7 def space @space end |
Instance Method Details
#after ⇒ ActiveRecord::Relation
50 51 52 |
# File 'lib/order_query/point.rb', line 50 def after records :after end |
#before ⇒ ActiveRecord::Relation
55 56 57 |
# File 'lib/order_query/point.rb', line 55 def before records :before end |
#count ⇒ Integer
29 30 31 |
# File 'lib/order_query/point.rb', line 29 def count @total ||= scope.count end |
#first ⇒ ActiveRecord::Base
19 20 21 |
# File 'lib/order_query/point.rb', line 19 def first scope.first end |
#last ⇒ ActiveRecord::Base
24 25 26 |
# File 'lib/order_query/point.rb', line 24 def last reverse_scope.first end |
#next(loop = true) ⇒ ActiveRecord::Base
40 41 42 |
# File 'lib/order_query/point.rb', line 40 def next(loop = true) unless_record_eq after.first || (first if loop) end |
#position ⇒ Integer
34 35 36 |
# File 'lib/order_query/point.rb', line 34 def position count - after.count end |
#previous(loop = true) ⇒ ActiveRecord::Base
45 46 47 |
# File 'lib/order_query/point.rb', line 45 def previous(loop = true) unless_record_eq before.first || (last if loop) end |
#records(direction) ⇒ ActiveRecord::Relation
61 62 63 64 65 66 67 68 69 |
# File 'lib/order_query/point.rb', line 61 def records(direction) query, query_args = @where_sql.build(direction) scope = (direction == :after ? space.scope : space.reverse_scope) if query.present? scope.where(query, *query_args) else scope end end |