Class: Pagy
- Inherits:
-
Object
- Object
- Pagy
- Defined in:
- lib/pagy/backend.rb,
lib/pagy.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/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/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
Defined Under Namespace
Modules: Backend, Frontend, Helpers, Search, Series Classes: Countless, OverflowError, VariableError
Constant Summary collapse
- VERSION =
'3.11.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 }
- 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__'
- DEPRECATED_LOCALES =
this file returns the I18n hash used as default alternative to the i18n gem
{'pt-br' =>'pt-BR', 'se' => 'sv-SE'}
- 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
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#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.
-
#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
-
.new_from_elasticsearch_rails(response, vars = {}) ⇒ Object
create a Pagy object from an Elasticsearch::Model::Response::Response object.
-
.new_from_searchkick(results, vars = {}) ⇒ Object
create a Pagy object from a Searchkick::Results object.
-
.root ⇒ Object
Root pathname to get the path of Pagy files like templates or dictionaries.
Instance Method Summary collapse
- #initialize_with_overflow(vars) ⇒ Object (also: #initialize)
- #overflow? ⇒ Boolean
-
#sequels ⇒ Object
‘Pagy` instance method used by the `pagy*_nav_js` helpers.
-
#series(size = ) ⇒ Object
Return the array of page numbers and :gap items e.g.
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
15 16 17 |
# File 'lib/pagy.rb', line 15 def count @count end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
15 16 17 |
# File 'lib/pagy.rb', line 15 def from @from end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
15 16 17 |
# File 'lib/pagy.rb', line 15 def items @items end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
15 16 17 |
# File 'lib/pagy.rb', line 15 def last @last end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
15 16 17 |
# File 'lib/pagy.rb', line 15 def next @next end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
15 16 17 |
# File 'lib/pagy.rb', line 15 def offset @offset end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
15 16 17 |
# File 'lib/pagy.rb', line 15 def page @page end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
15 16 17 |
# File 'lib/pagy.rb', line 15 def pages @pages end |
#prev ⇒ Object (readonly)
Returns the value of attribute prev.
15 16 17 |
# File 'lib/pagy.rb', line 15 def prev @prev end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
15 16 17 |
# File 'lib/pagy.rb', line 15 def to @to end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
15 16 17 |
# File 'lib/pagy.rb', line 15 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 19 |
# File 'lib/pagy/extras/elasticsearch_rails.rb', line 13 def self.new_from_elasticsearch_rails(response, vars={}) vars[:items] = response.search.[:size] || 10 vars[:page] = (response.search.[: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_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.[:per_page] vars[:page] = results.[:page] vars[:count] = results.total_count new(vars) end |
.root ⇒ Object
Root pathname to get the path of Pagy files like templates or dictionaries
10 |
# File 'lib/pagy.rb', line 10 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 VariableError.new(self), "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}" end end |
#overflow? ⇒ Boolean
11 |
# File 'lib/pagy/extras/overflow.rb', line 11 def overflow?; @overflow end |
#sequels ⇒ 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
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(VariableError.new(self), "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]
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pagy.rb', line 35 def series(size=@vars[:size]) (series = []) and size.empty? and return series 4.times{|i| (size[i]>=0 rescue nil) or raise(VariableError.new(self), "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 |