Class: Pagy

Inherits:
Object
  • Object
show all
Extended by:
ElasticsearchRailsExtra::Pagy, MeilisearchExtra::Pagy, SearchkickExtra::Pagy
Includes:
GearboxExtra, OverflowExtra::Pagy, SharedExtra::Pagy
Defined in:
lib/pagy.rb,
lib/pagy/i18n.rb,
lib/pagy/backend.rb,
lib/pagy/console.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/extras/array.rb,
lib/pagy/extras/bulma.rb,
lib/pagy/extras/items.rb,
lib/pagy/extras/uikit.rb,
lib/pagy/extras/shared.rb,
lib/pagy/extras/gearbox.rb,
lib/pagy/extras/headers.rb,
lib/pagy/extras/support.rb,
lib/pagy/extras/metadata.rb,
lib/pagy/extras/overflow.rb,
lib/pagy/extras/semantic.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/extras/elasticsearch_rails frozen_string_literal: true

Direct Known Subclasses

Countless

Defined Under Namespace

Modules: ArelExtra, ArrayExtra, Backend, BootstrapExtra, BulmaExtra, Console, CountlessExtra, ElasticsearchRailsExtra, FoundationExtra, Frontend, GearboxExtra, HeadersExtra, I18n, I18nExtra, ItemsExtra, MaterializeExtra, MeilisearchExtra, MetadataExtra, NavsExtra, OverflowExtra, SearchkickExtra, SemanticExtra, SharedExtra, StandaloneExtra, SupportExtra, TrimExtra, UikitExtra, UrlHelpers Classes: Countless, OverflowError, VariableError

Constant Summary collapse

VERSION =
'5.0.0'
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__'
Searchkick =
SearchkickExtra::Searchkick
Meilisearch =
MeilisearchExtra::Meilisearch
ElasticsearchRails =
ElasticsearchRailsExtra::ElasticsearchRails

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchkickExtra::Pagy

new_from_searchkick

Methods included from MeilisearchExtra::Pagy

new_from_meilisearch

Methods included from ElasticsearchRailsExtra::Pagy

new_from_elasticsearch_rails

Methods included from SharedExtra::Pagy

#sequels

Methods included from OverflowExtra::Pagy

#overflow?

Constructor Details

#initialize(vars) ⇒ Pagy

Merge and validate the options, do some simple arithmetic and set the instance variables

Raises:



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
  raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" \
        if @page > @last

  @offset = (@items * (@page - 1)) + @outset
  @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

#countObject (readonly)

Returns the value of attribute count.



27
28
29
# File 'lib/pagy.rb', line 27

def count
  @count
end

#fromObject (readonly)

Returns the value of attribute from.



27
28
29
# File 'lib/pagy.rb', line 27

def from
  @from
end

#inObject (readonly)

Returns the value of attribute in.



27
28
29
# File 'lib/pagy.rb', line 27

def in
  @in
end

#itemsObject (readonly)

Returns the value of attribute items.



27
28
29
# File 'lib/pagy.rb', line 27

def items
  @items
end

#lastObject (readonly)

Returns the value of attribute last.



27
28
29
# File 'lib/pagy.rb', line 27

def last
  @last
end

#nextObject (readonly)

Returns the value of attribute next.



27
28
29
# File 'lib/pagy.rb', line 27

def next
  @next
end

#offsetObject (readonly)

Returns the value of attribute offset.



27
28
29
# File 'lib/pagy.rb', line 27

def offset
  @offset
end

#pageObject (readonly)

Returns the value of attribute page.



27
28
29
# File 'lib/pagy.rb', line 27

def page
  @page
end

#pagesObject (readonly)

Returns the value of attribute pages.



27
28
29
# File 'lib/pagy.rb', line 27

def pages
  @pages
end

#prevObject (readonly)

Returns the value of attribute prev.



27
28
29
# File 'lib/pagy.rb', line 27

def prev
  @prev
end

#toObject (readonly)

Returns the value of attribute to.



27
28
29
# File 'lib/pagy.rb', line 27

def to
  @to
end

#varsObject (readonly)

Returns the value of attribute vars.



27
28
29
# File 'lib/pagy.rb', line 27

def vars
  @vars
end

Class Method Details

.rootObject

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

#series(size = ) ⇒ Object

Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, “9”, 10, 11, :gap, 36]

Raises:



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), "expected 4 items >= 0 in :size; got #{size.inspect}" \
        unless 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 - 1), :gap)
    start = left_gap_end + 1
  end
  if (right_gap_end - right_gap_start).positive?
    series.push(*start..(right_gap_start - 1), :gap)
    start = right_gap_end + 1
  end
  series.push(*start..@last)
  series[series.index(@page)] = @page.to_s
  series
end