Module: SearchFlip::Paginatable
- Included in:
- Aggregation, Criteria
- Defined in:
- lib/search_flip/paginatable.rb
Overview
The SearchFlip::Paginatable mixin provides chainable methods to allow paginating the search results
Class Method Summary collapse
Instance Method Summary collapse
-
#limit(value) ⇒ SearchFlip::Criteria
Sets the request limit, ie Elasticsearch’s size parameter that is used to restrict the results that get returned.
-
#limit_value_with_default ⇒ Fixnum
private
Returns the limit value or, if not yet set, the default limit value (30).
-
#offset(value) ⇒ SearchFlip::Criteria
Sets the request offset, ie SearchFlip’s from parameter that is used to skip results in the result set from being returned.
-
#offset_value_with_default ⇒ Fixnum
private
Returns the offset value or, if not yet set, the default limit value (0).
- #page(value) ⇒ Object
-
#paginate(page: 1, per_page: 30) ⇒ SearchFlip::Criteria
Sets pagination parameters for the criteria by using offset and limit, ie Elasticsearch’s from and size parameters.
- #per(value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 |
# File 'lib/search_flip/paginatable.rb', line 6 def self.included(base) base.class_eval do attr_accessor :offset_value, :limit_value end end |
Instance Method Details
#limit(value) ⇒ SearchFlip::Criteria
Sets the request limit, ie Elasticsearch’s size parameter that is used to restrict the results that get returned.
50 51 52 53 54 |
# File 'lib/search_flip/paginatable.rb', line 50 def limit(value) fresh.tap do |criteria| criteria.limit_value = value.to_i end end |
#limit_value_with_default ⇒ Fixnum
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the limit value or, if not yet set, the default limit value (30).
62 63 64 |
# File 'lib/search_flip/paginatable.rb', line 62 def limit_value_with_default (limit_value || 30).to_i end |
#offset(value) ⇒ SearchFlip::Criteria
Sets the request offset, ie SearchFlip’s from parameter that is used to skip results in the result set from being returned.
23 24 25 26 27 |
# File 'lib/search_flip/paginatable.rb', line 23 def offset(value) fresh.tap do |criteria| criteria.offset_value = value.to_i end end |
#offset_value_with_default ⇒ Fixnum
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the offset value or, if not yet set, the default limit value (0).
35 36 37 |
# File 'lib/search_flip/paginatable.rb', line 35 def offset_value_with_default (offset_value || 0).to_i end |
#page(value) ⇒ Object
85 86 87 |
# File 'lib/search_flip/paginatable.rb', line 85 def page(value) paginate(page: value, per_page: limit_value_with_default) end |
#paginate(page: 1, per_page: 30) ⇒ SearchFlip::Criteria
Sets pagination parameters for the criteria by using offset and limit, ie Elasticsearch’s from and size parameters.
78 79 80 81 82 83 |
# File 'lib/search_flip/paginatable.rb', line 78 def paginate(page: 1, per_page: 30) page = [page.to_i, 1].max per_page = per_page.to_i offset((page - 1) * per_page).limit(per_page) end |
#per(value) ⇒ Object
89 90 91 |
# File 'lib/search_flip/paginatable.rb', line 89 def per(value) paginate(page: 1 + (offset_value_with_default / limit_value_with_default), per_page: value) end |