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/extras/i18n.rb,
lib/pagy/extras/trim.rb,
lib/pagy/extras/array.rb,
lib/pagy/extras/bulma.rb,
lib/pagy/extras/items.rb,
lib/pagy/extras/plain.rb,
lib/pagy/extras/shared.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/elasticsearch_rails.rb
Overview
See the Pagy documentation: ddnexus.github.io/pagy/extras/elasticsearch_rails
Direct Known Subclasses
Defined Under Namespace
Modules: Backend, CountlessOverflow, Frontend, Overflow Classes: Countless, OverflowError
Constant Summary collapse
- VERSION =
'1.3.3'
- VARS =
default vars
{ page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:'', link_extra:'', item_path:'pagy.info.item_name', cycle: false }
- COUNTLESS =
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
-
.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.
- #overflow? ⇒ Boolean
-
#responsive ⇒ Object
Helper for building the page_nav with javascript.
-
#series(size = ) ⇒ Object
Return the array of page numbers and :gap items e.g.
- #to_h ⇒ Object
Constructor Details
#initialize(vars) ⇒ Pagy
Merge and validate the options, do some simple arithmetic and set the instance variables
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pagy.rb', line 19 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(ArgumentError, "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.
16 17 18 |
# File 'lib/pagy.rb', line 16 def count @count end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
16 17 18 |
# File 'lib/pagy.rb', line 16 def from @from end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
16 17 18 |
# File 'lib/pagy.rb', line 16 def items @items end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
16 17 18 |
# File 'lib/pagy.rb', line 16 def last @last end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
16 17 18 |
# File 'lib/pagy.rb', line 16 def next @next end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
16 17 18 |
# File 'lib/pagy.rb', line 16 def offset @offset end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
16 17 18 |
# File 'lib/pagy.rb', line 16 def page @page end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
16 17 18 |
# File 'lib/pagy.rb', line 16 def pages @pages end |
#prev ⇒ Object (readonly)
Returns the value of attribute prev.
16 17 18 |
# File 'lib/pagy.rb', line 16 def prev @prev end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
16 17 18 |
# File 'lib/pagy.rb', line 16 def to @to end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
16 17 18 |
# File 'lib/pagy.rb', line 16 def vars @vars end |
Class Method Details
.root ⇒ Object
Root pathname to get the path of Pagy files like templates or dictionaries
11 |
# File 'lib/pagy.rb', line 11 def self.root; Pathname.new(__FILE__).dirname end |
Instance Method Details
#overflow? ⇒ Boolean
8 |
# File 'lib/pagy/extras/overflow.rb', line 8 def overflow?; !!@overflow end |
#responsive ⇒ Object
Helper for building the page_nav with javascript. For example: with an object like:
Pagy.new count:1000, page: 20, breakpoints: {0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]}
it returns something like:
{ :items => [1, :gap, 18, 19, "20", 21, 22, 50, 2, 17, 23, 49, 3, 16, 24, 48],
:series => { 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] },
:widths => [550, 350, 0] }
where :items is the unordered array union of all the page numbers for all sizes (passed to the PagyResponsive javascript function)
:series is the hash of the series keyed by width (used by the *_responsive helpers to create the JSON string)
:widths is the desc-ordered array of widths (passed to the PagyResponsive javascript function)
23 24 25 26 27 28 29 |
# File 'lib/pagy/extras/shared.rb', line 23 def responsive @responsive ||= {items: [], series: {}, widths:[]}.tap do |r| @vars[:breakpoints].key?(0) || raise(ArgumentError, "expected :breakpoints to contain the 0 size; got #{@vars[:breakpoints].inspect}") @vars[:breakpoints].each {|width, size| r[:items] |= r[:series][width] = series(size)} r[:widths] = r[:series].keys.sort!{|a,b| b <=> a} 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]
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pagy.rb', line 36 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.tap{|s| s.shift; s[s.index(@page)] = @page.to_s} # convert the current page to String end |
#to_h ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pagy/extras/support.rb', line 8 def to_h { count: @count, page: @page, items: @items, pages: @pages, last: @last, offset: @offset, from: @from, to: @to, prev: @prev, next: @next, vars: @vars, series: series } end |