Class: Pagy

Inherits:
Object
  • Object
show all
Includes:
UseOverflowExtra
Defined in:
lib/pagy.rb,
lib/pagy/backend.rb,
lib/pagy/console.rb,
lib/pagy/frontend.rb,
lib/pagy/countless.rb,
lib/pagy/exceptions.rb,
lib/pagy/deprecation.rb,
lib/pagy/extras/arel.rb,
lib/pagy/extras/i18n.rb,
lib/pagy/extras/navs.rb,
lib/pagy/extras/trim.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/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: Backend, Console, ElasticsearchRails, Frontend, Helpers, Meilisearch, QueryUtils, Searchkick, UseItemsExtra, UseOverflowExtra, UseStandaloneExtra, UseTrimExtra Classes: Countless, OverflowError, VariableError

Constant Summary collapse

VERSION =
'4.11.0'
VARS =

default vars

{ page: 1, items: 20, outset: 0, size: [1, 4, 4, 1], page_param: :page,              # rubocop:disable Style/MutableConstant
params: {}, fragment: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }
INSTANCE_VARS_MIN =
{ count: 0, items: 1, page: 1, outset: 0 }.freeze
PAGE_PLACEHOLDER =

string used for search and replace, hardcoded also in the pagy.js file

'__pagy_page__'
I18n =

I18n static hash loaded at startup, used as default alternative to the i18n gem. see ddnexus.github.io/pagy/api/frontend#i18n

eval Pagy.root.join('locales', 'utils', 'i18n.rb').read
ITEMS_PLACEHOLDER =
'__pagy_items__'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UseOverflowExtra

#overflow?

Constructor Details

#initialize(vars) ⇒ Pagy

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

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pagy.rb', line 24

def initialize(vars)
  @vars = VARS.merge( vars.delete_if{|k,v| VARS.key?(k) && (v.nil? || v == '') } )
  @vars[:fragment] = Pagy.deprecated_var(:anchor, @vars[:anchor], :fragment, @vars[:fragment]) if @vars[:anchor]

  INSTANCE_VARS_MIN.each do |name,min|
    raise VariableError.new(self), "expected :#{name} >= #{min}; got #{@vars[name].inspect}" \
          unless @vars[name] && instance_variable_set(:"@#{name}", @vars[name].to_i) >= min
  end
  @pages = @last = [(@count.to_f / @items).ceil, 1].max
  raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" \
        if @page > @last

  @offset = @items * (@page - 1) + @outset
  @items  = @count - ((@pages - 1) * @items) if @page == @last && @count.positive?
  @from   = @count.zero? ? 0 : @offset + 1 - @outset
  @to     = @count.zero? ? 0 : @offset + @items - @outset
  @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.



19
20
21
# File 'lib/pagy.rb', line 19

def count
  @count
end

#fromObject (readonly)

Returns the value of attribute from.



19
20
21
# File 'lib/pagy.rb', line 19

def from
  @from
end

#itemsObject (readonly)

Returns the value of attribute items.



19
20
21
# File 'lib/pagy.rb', line 19

def items
  @items
end

#lastObject (readonly)

Returns the value of attribute last.



19
20
21
# File 'lib/pagy.rb', line 19

def last
  @last
end

#nextObject (readonly)

Returns the value of attribute next.



19
20
21
# File 'lib/pagy.rb', line 19

def next
  @next
end

#offsetObject (readonly)

Returns the value of attribute offset.



19
20
21
# File 'lib/pagy.rb', line 19

def offset
  @offset
end

#pageObject (readonly)

Returns the value of attribute page.



19
20
21
# File 'lib/pagy.rb', line 19

def page
  @page
end

#pagesObject (readonly)

Returns the value of attribute pages.



19
20
21
# File 'lib/pagy.rb', line 19

def pages
  @pages
end

#prevObject (readonly)

Returns the value of attribute prev.



19
20
21
# File 'lib/pagy.rb', line 19

def prev
  @prev
end

#toObject (readonly)

Returns the value of attribute to.



19
20
21
# File 'lib/pagy.rb', line 19

def to
  @to
end

#varsObject (readonly)

Returns the value of attribute vars.



19
20
21
# File 'lib/pagy.rb', line 19

def vars
  @vars
end

Class Method Details

.deprecated_arg(arg, val, new_key, new_val) ⇒ Object

deprecated posiitioal arguments



20
21
22
23
24
# File 'lib/pagy/deprecation.rb', line 20

def deprecated_arg(arg, val, new_key, new_val)
  value = val || new_val  # we use the new_val if present
  Warning.warn %([PAGY WARNING] deprecated use of positional '#{arg}' arg will not be supported in 5.0! Use only the keyword arg '#{new_key}: #{value.inspect}' instead.)
  value
end

.deprecated_order(pagy, page) ⇒ Object

deprecated pagy_url_for argument order



13
14
15
16
# File 'lib/pagy/deprecation.rb', line 13

def deprecated_order(pagy, page)
  Warning.warn %([PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead.)
  [page, pagy]
end

.deprecated_var(var, val, new_var, new_val) ⇒ Object

deprecated variables



6
7
8
9
10
# File 'lib/pagy/deprecation.rb', line 6

def deprecated_var(var, val, new_var, new_val)
  value = val || new_val
  Warning.warn %([PAGY WARNING] deprecated use of '#{var}' var will not be supported in 5.0! Use '#{new_var}: #{value.inspect}' instead.)
  value
end

.new_from_elasticsearch_rails(response, vars = {}) ⇒ Object

create a Pagy object from an Elasticsearch::Model::Response::Response object



21
22
23
24
25
26
27
# File 'lib/pagy/extras/elasticsearch_rails.rb', line 21

def self.new_from_elasticsearch_rails(response, vars={})
  vars[:items] = response.search.options[:size] || 10
  vars[:page]  = (response.search.options[:from] || 0) / vars[:items] + 1
  total        = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
  vars[:count] = total.is_a?(Hash) ? total['value'] : total
  new(vars)
end

.new_from_meilisearch(results, vars = {}) ⇒ Object

create a Pagy object from a Meilisearch results



17
18
19
20
21
22
# File 'lib/pagy/extras/meilisearch.rb', line 17

def self.new_from_meilisearch(results, vars={})
  vars[:items] = results.raw_answer['limit']
  vars[:page]  = [results.raw_answer['offset'] / vars[:items], 1].max
  vars[:count] = results.raw_answer['nbHits']
  new(vars)
end

.new_from_searchkick(results, vars = {}) ⇒ Object

create a Pagy object from a Searchkick::Results object



21
22
23
24
25
26
# File 'lib/pagy/extras/searchkick.rb', line 21

def self.new_from_searchkick(results, vars={})
  vars[:items] = results.options[:per_page]
  vars[:page]  = results.options[:page]
  vars[:count] = results.total_count
  new(vars)
end

.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

#sequels(steps = nil) ⇒ Object

‘Pagy` instance method used by the `pagy*_nav_js` helpers. It returns the sequels of width/series generated from the :steps hash Example: >> pagy = Pagy.new(count:1000, page: 20, steps: => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]) >> pagy.sequels #=> { “0” => [1, :gap, 18, 19, “20”, 21, 22, :gap, 50],

"350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
"550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }

Notice: if :steps is false it will use the single => @vars size

Raises:



19
20
21
22
23
24
25
26
# File 'lib/pagy/extras/shared.rb', line 19

def sequels(steps=nil)
  steps ||= @vars[:steps] || {0 => @vars[:size]}
  raise VariableError.new(self), "expected :steps to define the 0 width; got #{steps.inspect}" \
        unless steps.key?(0)
  {}.tap do |sequels|
    steps.each {|width, size| sequels[width.to_s] = series(size)}
  end
end

#series(size = ) ⇒ Object

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

Raises:



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
# File 'lib/pagy.rb', line 45

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]
  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