Class: Pagy

Inherits:
Object
  • Object
show all
Defined in:
lib/pagy/backend.rb,
lib/pagy.rb,
lib/pagy/frontend.rb,
lib/pagy/countless.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/shared.rb,
lib/pagy/extras/headers.rb,
lib/pagy/extras/support.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/materialize.rb,
lib/pagy/extras/pagy_search.rb,
lib/pagy/extras/elasticsearch_rails.rb

Overview

Support module to capture search calls encoding: utf-8 frozen_string_literal: true

Direct Known Subclasses

Countless

Defined Under Namespace

Modules: Backend, Frontend, Helpers, Search, Series Classes: Countless, OverflowError

Constant Summary collapse

VERSION =
'3.3.0'
VARS =

default vars

{ page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:'', link_extra:'', i18n_key:'pagy.item_name', cycle:false }
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)
OVERFLOW =
true
COUNTLESS =

used by the items extra

true
SEARCHKICK =

used by the items extra

true
ELASTICSEARCH_RAILS =

used by the items extra

true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



17
18
19
# File 'lib/pagy.rb', line 17

def count
  @count
end

#fromObject (readonly)

Returns the value of attribute from.



17
18
19
# File 'lib/pagy.rb', line 17

def from
  @from
end

#itemsObject (readonly)

Returns the value of attribute items.



17
18
19
# File 'lib/pagy.rb', line 17

def items
  @items
end

#lastObject (readonly)

Returns the value of attribute last.



17
18
19
# File 'lib/pagy.rb', line 17

def last
  @last
end

#nextObject (readonly)

Returns the value of attribute next.



17
18
19
# File 'lib/pagy.rb', line 17

def next
  @next
end

#offsetObject (readonly)

Returns the value of attribute offset.



17
18
19
# File 'lib/pagy.rb', line 17

def offset
  @offset
end

#pageObject (readonly)

Returns the value of attribute page.



17
18
19
# File 'lib/pagy.rb', line 17

def page
  @page
end

#pagesObject (readonly)

Returns the value of attribute pages.



17
18
19
# File 'lib/pagy.rb', line 17

def pages
  @pages
end

#prevObject (readonly)

Returns the value of attribute prev.



17
18
19
# File 'lib/pagy.rb', line 17

def prev
  @prev
end

#toObject (readonly)

Returns the value of attribute to.



17
18
19
# File 'lib/pagy.rb', line 17

def to
  @to
end

#varsObject (readonly)

Returns the value of attribute vars.



17
18
19
# File 'lib/pagy.rb', line 17

def vars
  @vars
end

Class Method Details

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

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



13
14
15
16
17
18
# File 'lib/pagy/extras/elasticsearch_rails.rb', line 13

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
  vars[:count] = response.raw_response['hits']['total']
  new(vars)
end

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

create a Pagy object from a Searchkick::Results object



13
14
15
16
17
18
# File 'lib/pagy/extras/searchkick.rb', line 13

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



12
# File 'lib/pagy.rb', line 12

def self.root; @root ||= Pathname.new(__FILE__).dirname.freeze end

Instance Method Details

#initialize_with_overflow(vars) ⇒ Object Also known as: initialize



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pagy/extras/overflow.rb', line 14

def initialize_with_overflow(vars)
  @overflow ||= false                         # don't override if :last_page re-run the method after an overflow
  initialize_without_overflow(vars)
rescue OverflowError
  @overflow = true                            # add the overflow flag
  case @vars[:overflow]
  when :exception
    raise                                     # same as without the extra
  when :last_page
    initial_page = @vars[:page]               # save the very initial page (even after re-run)
    initialize(vars.merge!(page: @last))      # re-run with the last page
    @vars[:page] = initial_page               # restore the inital page
  when :empty_page
    @offset = @items = @from = @to = 0        # vars relative to the actual page
    @prev = @last                             # prev relative to the actual page
    extend(Series)                            # special series for :empty_page
  else
    raise ArgumentError, "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
  end
end

#overflow?Boolean

Returns:

  • (Boolean)


11
# File 'lib/pagy/extras/overflow.rb', line 11

def overflow?; @overflow end

#sequelsObject

‘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



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

def sequels
  steps = @vars[:steps] || {0 => @vars[:size]}
  steps.key?(0) or raise(ArgumentError, "expected :steps to define the 0 width; got #{steps.inspect}")
  sequels = {}; steps.each {|width, size| sequels[width.to_s] = series(size)}; sequels
end

#series(size = ) ⇒ Object

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pagy.rb', line 37

def series(size=@vars[:size])
  (series = []) and size.empty? and return series
  4.times{|i| (size[i]>=0 rescue nil) or raise(ArgumentError, "expected 4 items >= 0 in :size; got #{size.inspect}")}
  [*0..size[0], *@page-size[1]..@page+size[2], *@last-size[3]+1..@last+1].sort!.each_cons(2) do |a, b|
    if    a<0 || a==b || a>@last                                        # skip out of range and duplicates
    elsif a+1 == b; series.push(a)                                      # no gap     -> no additions
    elsif a+2 == b; series.push(a, a+1)                                 # 1 page gap -> fill with missing page
    else            series.push(a, :gap)                                # n page gap -> add gap
    end                                                                 # skip the end boundary (last+1)
  end                                                                   # shift the start boundary (0) and
  series.shift; series[series.index(@page)] = @page.to_s; series        # convert the current page to String
end