Class: Pagy
- Inherits:
-
Object
- Object
- Pagy
- Includes:
- FrontendHelpers::Pagy, GearboxExtra, OverflowExtra::Pagy
- 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/navs.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/support.rb,
lib/pagy/extras/calendar.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/frontend_helpers.rb,
lib/pagy/extras/elasticsearch_rails.rb
Overview
See the Pagy documentation: ddnexus.github.io/pagy/extras/elasticsearch_rails frozen_string_literal: true
Defined Under Namespace
Modules: ArelExtra, ArrayExtra, Backend, BootstrapExtra, BulmaExtra, CalendarExtra, Console, CountlessExtra, ElasticsearchRailsExtra, FoundationExtra, Frontend, FrontendHelpers, GearboxExtra, HeadersExtra, I18n, I18nExtra, ItemsExtra, MaterializeExtra, MeilisearchExtra, MetadataExtra, NavsExtra, OverflowExtra, SearchkickExtra, SemanticExtra, StandaloneExtra, SupportExtra, TrimExtra, UikitExtra, UrlHelpers Classes: Calendar, Countless, I18nError, InternalError, OverflowError, VariableError
Constant Summary collapse
- VERSION =
'5.10.1'
- DEFAULT =
Default core vars: constant for easy access, but mutable for customizable defaults
{ page: 1, # rubocop:disable Style/MutableConstant items: 20, outset: 0, size: [1, 4, 4, 1], page_param: :page, params: {}, fragment: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }
- PAGE_PLACEHOLDER =
Used for search and replace, hardcoded also in the pagy.js file
'__pagy_page__'
- LABEL_PLACEHOLDER =
'__pagy_label__'
- Searchkick =
SearchkickExtra::Searchkick
- Meilisearch =
MeilisearchExtra::Meilisearch
- ElasticsearchRails =
ElasticsearchRailsExtra::ElasticsearchRails
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
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.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#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
Root pathname to get the path of Pagy files like templates or dictionaries.
Instance Method Summary collapse
-
#initialize(vars) ⇒ Pagy
constructor
Merge and validate the options, do some simple arithmetic and set the instance variables.
-
#label ⇒ Object
Allow the customization of the output (overridden by the calendar extra).
-
#label_for(page) ⇒ Object
Allow the customization of the output (overridden by the calendar extra).
-
#series(size: , **_) ⇒ Object
Return the array of page numbers and :gap items e.g.
Methods included from SearchkickExtra::Pagy
Methods included from MeilisearchExtra::Pagy
Methods included from ElasticsearchRailsExtra::Pagy
Methods included from OverflowExtra::Pagy
Methods included from FrontendHelpers::Pagy
Constructor Details
#initialize(vars) ⇒ Pagy
Merge and validate the options, do some simple arithmetic and set the instance variables
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pagy.rb', line 30 def initialize(vars) normalize_vars(vars) setup_vars(count: 0, page: 1, outset: 0) setup_items_var setup_pages_var setup_offset_var setup_params_var raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last @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.
27 28 29 |
# File 'lib/pagy.rb', line 27 def count @count end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
27 28 29 |
# File 'lib/pagy.rb', line 27 def from @from end |
#in ⇒ Object (readonly)
Returns the value of attribute in.
27 28 29 |
# File 'lib/pagy.rb', line 27 def in @in end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
27 28 29 |
# File 'lib/pagy.rb', line 27 def items @items end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
27 28 29 |
# File 'lib/pagy.rb', line 27 def last @last end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
27 28 29 |
# File 'lib/pagy.rb', line 27 def next @next end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
27 28 29 |
# File 'lib/pagy.rb', line 27 def offset @offset end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
27 28 29 |
# File 'lib/pagy.rb', line 27 def page @page end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
27 28 29 |
# File 'lib/pagy.rb', line 27 def pages @pages end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
27 28 29 |
# File 'lib/pagy.rb', line 27 def params @params end |
#prev ⇒ Object (readonly)
Returns the value of attribute prev.
27 28 29 |
# File 'lib/pagy.rb', line 27 def prev @prev end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
27 28 29 |
# File 'lib/pagy.rb', line 27 def to @to end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
27 28 29 |
# File 'lib/pagy.rb', line 27 def vars @vars end |
Class Method Details
.root ⇒ Object
Root pathname to get the path of Pagy files like templates or dictionaries
11 12 13 |
# File 'lib/pagy.rb', line 11 def self.root @root ||= Pathname.new(__dir__).freeze end |
Instance Method Details
#label ⇒ Object
Allow the customization of the output (overridden by the calendar extra)
80 81 82 |
# File 'lib/pagy.rb', line 80 def label @page.to_s end |
#label_for(page) ⇒ Object
Allow the customization of the output (overridden by the calendar extra)
75 76 77 |
# File 'lib/pagy.rb', line 75 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]
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 |
# File 'lib/pagy.rb', line 47 def series(size: @vars[:size], **_) return [] if size.empty? raise VariableError.new(self, :size, 'to contain 4 items >= 0', size) \ unless 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] # rubocop:disable Layout/ExtraSpacing, Layout/SpaceAroundOperators 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 series = [] 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) series[series.index(@page)] = @page.to_s series end |