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/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/jsonapi.rb,
lib/pagy/extras/support.rb,
lib/pagy/calendar/helper.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/docs/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, JsonApiExtra, MaterializeExtra, MeilisearchExtra, MetadataExtra, NavsExtra, OverflowExtra, SearchkickExtra, SemanticExtra, StandaloneExtra, SupportExtra, TrimExtra, UikitExtra, UrlHelpers Classes: Calendar, Countless, I18nError, InternalError, OverflowError, VariableError
Constant Summary collapse
- VERSION =
'7.0.10'
- DEFAULT =
Default core vars: constant for easy access, but mutable for customizable defaults
{ page: 1, # rubocop:disable Style/MutableConstant items: 20, outset: 0, size: 7, page_param: :page, params: {}, fragment: '', link_extra: '', item_i18n_key: 'pagy.item_name', nav_i18n_key: 'pagy.aria_label.nav', cycle: false, request_path: '', count_args: [:all] }
- 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
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.
-
#request_path ⇒ Object
readonly
Returns the value of attribute request_path.
-
#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
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 OverflowExtra::PagyOverride
Methods included from FrontendHelpers::PagyAddOn
Constructor Details
#initialize(vars) ⇒ Pagy
Merge and validate the options, do some simple arithmetic and set the instance variables
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pagy.rb', line 33 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 setup_request_path_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.
30 31 32 |
# File 'lib/pagy.rb', line 30 def count @count end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
30 31 32 |
# File 'lib/pagy.rb', line 30 def from @from end |
#in ⇒ Object (readonly)
Returns the value of attribute in.
30 31 32 |
# File 'lib/pagy.rb', line 30 def in @in end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
30 31 32 |
# File 'lib/pagy.rb', line 30 def items @items end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
30 31 32 |
# File 'lib/pagy.rb', line 30 def last @last end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
30 31 32 |
# File 'lib/pagy.rb', line 30 def next @next end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
30 31 32 |
# File 'lib/pagy.rb', line 30 def offset @offset end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
30 31 32 |
# File 'lib/pagy.rb', line 30 def page @page end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
30 31 32 |
# File 'lib/pagy.rb', line 30 def pages @pages end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
30 31 32 |
# File 'lib/pagy.rb', line 30 def params @params end |
#prev ⇒ Object (readonly)
Returns the value of attribute prev.
30 31 32 |
# File 'lib/pagy.rb', line 30 def prev @prev end |
#request_path ⇒ Object (readonly)
Returns the value of attribute request_path.
30 31 32 |
# File 'lib/pagy.rb', line 30 def request_path @request_path end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
30 31 32 |
# File 'lib/pagy.rb', line 30 def to @to end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
30 31 32 |
# File 'lib/pagy.rb', line 30 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
Label for the current page. Allow the customization of the output (overridden by the calendar extra)
98 99 100 |
# File 'lib/pagy.rb', line 98 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)
93 94 95 |
# File 'lib/pagy.rb', line 93 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]
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 84 85 86 87 88 89 90 |
# File 'lib/pagy.rb', line 51 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 = @pages if size > @pages # reduce the max size to @pages 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 > @pages - (size - left) # end pages @pages - 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 |