Class: Pagy
- Inherits:
-
Object
- Object
- Pagy
- Includes:
- Overflow
- 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/elasticsearch_rails.rb
Overview
See the Pagy documentation: ddnexus.github.io/pagy/extras/elasticsearch_rails encoding: utf-8 frozen_string_literal: true
Direct Known Subclasses
Defined Under Namespace
Modules: Backend, ElasticsearchRails, Frontend, Helpers, Items, Overflow, Searchkick, Trim Classes: Countless, OverflowError, VariableError
Constant Summary collapse
- VERSION =
'4.1.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__'
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(vars) ⇒ Pagy
constructor
Merge and validate the options, do some simple arithmetic and set the instance variables.
-
#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.
Methods included from Overflow
Constructor Details
#initialize(vars) ⇒ Pagy
Merge and validate the options, do some simple arithmetic and set the instance variables
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pagy.rb', line 18 def initialize(vars) @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars { count:0, items:1, outset:0, page:1 }.each do |k,min| # validate instance variables (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \ or raise(VariableError.new(self), "expected :#{k} >= #{min}; got #{@vars[k].inspect}") end @pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings @page <= @last or raise(OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}") @offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset) @items = @count - ((@pages-1) * @items) if @page == @last && @count > 0 # adjust items for last non-empty page @from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item @to = @count == 0 ? 0 : @offset + @items - @outset # page ends to item @prev = (@page-1 unless @page == 1) # nil if no prev page @next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle end |
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
22 23 24 25 26 27 28 |
# File 'lib/pagy/extras/elasticsearch_rails.rb', line 22 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
22 23 24 25 26 27 |
# File 'lib/pagy/extras/searchkick.rb', line 22 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 |
Instance Method Details
#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; next # 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 |