Class: Pagy
- Inherits:
-
Object
- Object
- Pagy
- Extended by:
- ElasticsearchRailsExtra::PagyAddOn, MeilisearchExtra::PagyExtension, SearchkickExtra::PagyExtension
- Defined in:
- lib/pagy.rb,
lib/pagy/i18n.rb,
lib/pagy/backend.rb,
lib/pagy/console.rb,
lib/pagy/calendar.rb,
lib/pagy/frontend.rb,
lib/pagy/countless.rb,
lib/pagy/exceptions.rb,
lib/pagy/extras/arel.rb,
lib/pagy/extras/i18n.rb,
lib/pagy/extras/pagy.rb,
lib/pagy/extras/trim.rb,
lib/pagy/url_helpers.rb,
lib/pagy/calendar/day.rb,
lib/pagy/extras/array.rb,
lib/pagy/extras/bulma.rb,
lib/pagy/extras/items.rb,
lib/pagy/extras/uikit.rb,
lib/pagy/calendar/week.rb,
lib/pagy/calendar/year.rb,
lib/pagy/calendar/month.rb,
lib/pagy/extras/gearbox.rb,
lib/pagy/extras/headers.rb,
lib/pagy/extras/jsonapi.rb,
lib/pagy/calendar/helper.rb,
lib/pagy/extras/calendar.rb,
lib/pagy/extras/js_tools.rb,
lib/pagy/extras/metadata.rb,
lib/pagy/extras/overflow.rb,
lib/pagy/extras/semantic.rb,
lib/pagy/calendar/quarter.rb,
lib/pagy/extras/bootstrap.rb,
lib/pagy/extras/countless.rb,
lib/pagy/extras/foundation.rb,
lib/pagy/extras/searchkick.rb,
lib/pagy/extras/standalone.rb,
lib/pagy/extras/materialize.rb,
lib/pagy/extras/meilisearch.rb,
lib/pagy/extras/elasticsearch_rails.rb
Overview
See the Pagy documentation: ddnexus.github.io/pagy/docs/extras/elasticsearch_rails frozen_string_literal: true
Defined Under Namespace
Modules: ArelExtra, ArrayExtra, Backend, BootstrapExtra, BulmaExtra, CalendarExtra, Console, CountlessExtra, ElasticsearchRailsExtra, FoundationExtra, Frontend, GearboxExtra, HeadersExtra, I18n, I18nExtra, ItemsExtra, JSTools, JsonApiExtra, MaterializeExtra, MeilisearchExtra, MetadataExtra, OverflowExtra, PagyExtra, SearchkickExtra, SemanticExtra, StandaloneExtra, TrimExtra, UikitExtra, UrlHelpers Classes: Calendar, Countless, I18nError, InternalError, OverflowError, VariableError
Constant Summary collapse
- VERSION =
'8.1.2'
- DEFAULT =
Core defult: constant for easy access, but mutable for customizable defaults
{ page: 1, # rubocop:disable Style/MutableConstant items: 20, outset: 0, size: 7, cycle: false, count_args: [:all], # AR friendly page_param: :page }
- PAGE_TOKEN =
Used for search and replace, hardcoded also in the pagy.js file
'__pagy_page__'
- LABEL_TOKEN =
'__pagy_label__'
- Searchkick =
ModelExtension
- Meilisearch =
ModelExtension
- ElasticsearchRails =
ModelExtension
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#in ⇒ Object
readonly
Returns the value of attribute in.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#last ⇒ Object
(also: #pages)
readonly
Returns the value of attribute last.
-
#next ⇒ Object
readonly
Returns the value of attribute next.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#prev ⇒ Object
readonly
Returns the value of attribute prev.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Class Method Summary collapse
-
.root ⇒ Object
Gem root pathname to get the path of Pagy files stylesheets, javascripts, apps, locales, etc.
Instance Method Summary collapse
-
#initialize(vars) ⇒ Pagy
constructor
Merge and validate the options, do some simple arithmetic and set the instance variables.
-
#label ⇒ Object
Label for the current page.
-
#label_for(page) ⇒ Object
Label for any page.
-
#series(size: , **_) ⇒ Object
Return the array of page numbers and :gap items e.g.
Methods included from SearchkickExtra::PagyExtension
Methods included from MeilisearchExtra::PagyExtension
Methods included from ElasticsearchRailsExtra::PagyAddOn
Methods included from JSTools::PagyAddOn
Methods included from OverflowExtra::PagyOverride
Constructor Details
#initialize(vars) ⇒ Pagy
Merge and validate the options, do some simple arithmetic and set the instance variables
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pagy.rb', line 28 def initialize(vars) normalize_vars(vars) setup_vars(count: 0, page: 1, outset: 0) setup_items_var setup_last_var raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last setup_offset_var @from = [@offset - @outset + 1, @count].min @to = [@offset - @outset + @items, @count].min @in = [@to - @from + 1, @count].min @prev = (@page - 1 unless @page == 1) @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
24 25 26 |
# File 'lib/pagy.rb', line 24 def count @count end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
24 25 26 |
# File 'lib/pagy.rb', line 24 def from @from end |
#in ⇒ Object (readonly)
Returns the value of attribute in.
24 25 26 |
# File 'lib/pagy.rb', line 24 def in @in end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
24 25 26 |
# File 'lib/pagy.rb', line 24 def items @items end |
#last ⇒ Object (readonly) Also known as: pages
Returns the value of attribute last.
24 25 26 |
# File 'lib/pagy.rb', line 24 def last @last end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
24 25 26 |
# File 'lib/pagy.rb', line 24 def next @next end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
24 25 26 |
# File 'lib/pagy.rb', line 24 def offset @offset end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
24 25 26 |
# File 'lib/pagy.rb', line 24 def page @page end |
#prev ⇒ Object (readonly)
Returns the value of attribute prev.
24 25 26 |
# File 'lib/pagy.rb', line 24 def prev @prev end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
24 25 26 |
# File 'lib/pagy.rb', line 24 def to @to end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
24 25 26 |
# File 'lib/pagy.rb', line 24 def vars @vars end |
Class Method Details
.root ⇒ Object
Gem root pathname to get the path of Pagy files stylesheets, javascripts, apps, locales, etc.
11 12 13 |
# File 'lib/pagy.rb', line 11 def self.root @root ||= Pathname.new(__dir__).parent.freeze end |
Instance Method Details
#label ⇒ Object
Label for the current page. Allow the customization of the output (overridden by the calendar extra)
91 92 93 |
# File 'lib/pagy.rb', line 91 def label @page.to_s end |
#label_for(page) ⇒ Object
Label for any page. Allow the customization of the output (overridden by the calendar extra)
86 87 88 |
# File 'lib/pagy.rb', line 86 def label_for(page) page.to_s end |
#series(size: , **_) ⇒ Object
Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, “9”, 10, 11, :gap, 36]
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pagy.rb', line 44 def series(size: @vars[:size], **_) series = [] if size.is_a?(Array) && size.size == 4 && size.all? { |num| !num.negative? rescue false } # rubocop:disable Style/RescueModifier # This algorithm is up to ~5x faster and ~2.3x lighter than the previous one (pagy < 4.3) left_gap_start = 1 + size[0] left_gap_end = @page - size[1] - 1 right_gap_start = @page + size[2] + 1 right_gap_end = @last - size[3] left_gap_end = right_gap_end if left_gap_end > right_gap_end right_gap_start = left_gap_start if left_gap_start > right_gap_start start = 1 if (left_gap_end - left_gap_start).positive? series.push(*start...left_gap_start, :gap) start = left_gap_end + 1 end if (right_gap_end - right_gap_start).positive? series.push(*start...right_gap_start, :gap) start = right_gap_end + 1 end series.push(*start..@last) elsif size.is_a?(Integer) && size.positive? # only central series # The simplest and fastest algorithm size = @last if size > @last # reduce the max size to @last left = ((size - 1) / 2.0).floor # left half might be 1 page shorter for even size start = if @page <= left # beginning pages 1 elsif @page > @last - (size - left) # end pages @last - size + 1 else # intermediate pages @page - left end series = (start..start + size - 1).to_a else return [] if size.empty? raise VariableError.new(self, :size, 'to be a single positive Integer or an Array of 4', size) end series[series.index(@page)] = @page.to_s series end |