pagy 5.0.1 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1345a70a6c730b386f155905f1f64821d0843704a378906cec76a27a68f77bc
4
- data.tar.gz: b067a51f4178b84cf164374382b41e061e4d0e257819961abb2ef8d3e516c83d
3
+ metadata.gz: 46116524913f885ab10fb48fdac0c9b6691654e23abb04f138586791aaca7a3b
4
+ data.tar.gz: 911ca803f1f0764484c0440898d384c768c0ed94da1eeac7dd9317a66750b548
5
5
  SHA512:
6
- metadata.gz: 8aa6a995fff4c56eade45efb978ad63beb36f2ee10152dbf89365cf18d66911f89a7f84f36b35ce94b4228a286f18b6343a6c30c80434bd9ad6a12c177b47b3e
7
- data.tar.gz: 4a9288306aff02bfc789a3198f38c02ed6b7d038d2caddf1eba1d3b4102e4f1f61c111eefd60c126c6a4f5c2e3b88dd745802732324d506bed8bbd118361027f
6
+ metadata.gz: e2a7dcaf8febf7412f73bb54b2bba3189670faed1e58400ab68e8e0fe1c7de305cbe66be9a848762d2bad31bf8de082c6208ede39ff04027c6b71c283e356220
7
+ data.tar.gz: 43ee98762c7e8b1e777dd52d9c550f0b351691f2dad440dc65daeb0328fe1d0f2868830f4b34e75d2b45b10e0530140801bd3e65eb2938cc7ca54a94d65207c4
data/lib/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.0.1)
3
+ # Pagy initializer file (5.1.0)
4
4
  # Customize only what you really need and notice that Pagy works also without any of the following lines.
5
5
  # Should you just cherry pick part of this file, please maintain the require-order of the extras
6
6
 
@@ -153,6 +153,11 @@
153
153
  # set to false only if you want to make :trim_extra an opt-in variable
154
154
  # Pagy::DEFAULT[:trim_extra] = false # default true
155
155
 
156
+ # Standalone extra: Use pagy in non Rack environment/gem
157
+ # See https://ddnexus.github.io/pagy/extras/standalone
158
+ # require 'pagy/extras/standalone'
159
+ # Pagy::DEFAULT[:url] = 'http://www.example.com/subdir' # optional default
160
+
156
161
 
157
162
  # Rails
158
163
 
@@ -3,7 +3,7 @@
3
3
  // Container of the whole pagy stuff
4
4
  function Pagy(){}
5
5
 
6
- Pagy.version = '5.0.1'
6
+ Pagy.version = '5.1.0'
7
7
 
8
8
  // Used by the waitForMe function
9
9
  Pagy.delay = 100
data/lib/pagy/backend.rb CHANGED
@@ -15,16 +15,17 @@ class Pagy
15
15
  end
16
16
 
17
17
  # Sub-method called only by #pagy: here for easy customization of variables by overriding
18
+ # You may need to override the count call for non AR collections
18
19
  def pagy_get_vars(collection, vars)
19
20
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
20
- vars[:count] ||= (c = collection.count(:all)).is_a?(Hash) ? c.size : c
21
+ vars[:count] ||= (count = collection.count(:all)).is_a?(Hash) ? count.size : count
21
22
  vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
22
23
  vars
23
24
  end
24
25
 
25
26
  # Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
27
+ # You may need to override this method for collections without offset|limit
26
28
  def pagy_get_items(collection, pagy)
27
- # This should work with ActiveRecord, Sequel, Mongoid...
28
29
  collection.offset(pagy.offset).limit(pagy.items)
29
30
  end
30
31
  end
data/lib/pagy/console.rb CHANGED
@@ -5,8 +5,9 @@ require 'pagy' # so you can require just the extra in the console
5
5
  require 'pagy/extras/standalone'
6
6
 
7
7
  class Pagy
8
- # Provide ready to use pagy environment when included in irb/rails console
8
+ # Provide a ready to use pagy environment when included in irb/rails console
9
9
  module Console
10
+ # Include Backend, Frontend and set the default URL
10
11
  def self.included(main)
11
12
  main.include(Backend)
12
13
  main.include(Frontend)
@@ -14,12 +14,12 @@ class Pagy
14
14
  @offset = (@items * (@page - 1)) + @outset
15
15
  end
16
16
 
17
- # Finalize the instance variables based on the fetched items
18
- def finalize(fetched)
19
- raise OverflowError.new(self, :page, "to be < #{@page}") if fetched.zero? && @page > 1
17
+ # Finalize the instance variables based on the fetched size
18
+ def finalize(fetched_size)
19
+ raise OverflowError.new(self, :page, "to be < #{@page}") if fetched_size.zero? && @page > 1
20
20
 
21
- @pages = @last = (fetched > @items ? @page + 1 : @page)
22
- @in = [fetched, @items].min
21
+ @pages = @last = (fetched_size > @items ? @page + 1 : @page)
22
+ @in = [fetched_size, @items].min
23
23
  @from = @in.zero? ? 0 : @offset - @outset + 1
24
24
  @to = @offset - @outset + @in
25
25
  @prev = (@page - 1 unless @page == 1)
@@ -27,6 +27,8 @@ class Pagy
27
27
  self
28
28
  end
29
29
 
30
+ # Override the original series.
31
+ # Return nil if :countless_minimal is enabled
30
32
  def series(_size = @vars[:size])
31
33
  super unless @vars[:countless_minimal]
32
34
  end
@@ -5,6 +5,7 @@ class Pagy
5
5
  class VariableError < ArgumentError
6
6
  attr_reader :pagy, :variable, :value
7
7
 
8
+ # Set the variables and prepare the message
8
9
  def initialize(pagy, variable, description, value = nil)
9
10
  @pagy = pagy
10
11
  @variable = variable
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module BootstrapExtra
8
10
  # Pagination for bootstrap: it returns the html with the series of links to the pages
9
11
  def pagy_bootstrap_nav(pagy, pagy_id: nil, link_extra: '')
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module BulmaExtra
8
10
  # Pagination for Bulma: it returns the html with the series of links to the pages
9
11
  def pagy_bulma_nav(pagy, pagy_id: nil, link_extra: '')
@@ -23,16 +23,13 @@ class Pagy
23
23
  end
24
24
 
25
25
  # Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
26
+ # You may need to override this method for collections without offset|limit
26
27
  def pagy_countless_get_items(collection, pagy)
27
- # This should work with ActiveRecord, Sequel, Mongoid...
28
28
  return collection.offset(pagy.offset).limit(pagy.items) if pagy.vars[:countless_minimal]
29
29
 
30
- items = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
31
- items_size = items.size
32
- items.pop if items_size == pagy.items + 1
33
- # finalize may adjust pagy.items, so must be used after checking the size
34
- pagy.finalize(items_size)
35
- items
30
+ fetched = collection.offset(pagy.offset).limit(pagy.items + 1).to_a # eager load items + 1
31
+ pagy.finalize(fetched.size) # finalize the pagy object
32
+ fetched[0, pagy.items] # ignore eventual extra item
36
33
  end
37
34
  end
38
35
  Backend.prepend CountlessExtra
@@ -19,8 +19,8 @@ class Pagy
19
19
 
20
20
  module ElasticsearchRails
21
21
  # Return an array used to delay the call of #search
22
- # after the pagination variables are merged to the options
23
- # it also pushes to the same array an eventually called method
22
+ # after the pagination variables are merged to the options.
23
+ # It also pushes to the same array an optional method call.
24
24
  def pagy_elasticsearch_rails(query_or_payload, **options)
25
25
  [self, query_or_payload, options].tap do |args|
26
26
  args.define_singleton_method(:method_missing) { |*a| args += a }
@@ -57,7 +57,7 @@ class Pagy
57
57
  pagy = ::Pagy.new(vars)
58
58
  # with :last_page overflow we need to re-run the method in order to get the hits
59
59
  return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page)) \
60
- if defined?(::Pagy::OverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
60
+ if defined?(::Pagy::OverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
61
61
 
62
62
  [pagy, called.empty? ? response : response.send(*called)]
63
63
  end
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module FoundationExtra
8
10
  # Pagination for Foundation: it returns the html with the series of links to the pages
9
11
  def pagy_foundation_nav(pagy, pagy_id: nil, link_extra: '')
@@ -14,9 +14,9 @@ class Pagy # Default variables for this extra
14
14
 
15
15
  # Set the items variable considering the params and other pagy variables
16
16
  def pagy_set_items_from_params(vars)
17
- return if vars[:items]
18
- return unless vars.key?(:items_extra) ? vars[:items_extra] : DEFAULT[:items_extra]
19
- return unless (items = params[vars[:items_param] || DEFAULT[:items_param]])
17
+ return if vars[:items] # :items explicitly set
18
+ return unless vars.key?(:items_extra) ? vars[:items_extra] : DEFAULT[:items_extra] # :items_extra is false
19
+ return unless (items = params[vars[:items_param] || DEFAULT[:items_param]]) # no items from request params
20
20
 
21
21
  vars[:items] = [items.to_i, vars.key?(:max_items) ? vars[:max_items] : DEFAULT[:max_items]].compact.min
22
22
  end
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module MaterializeExtra
8
10
  # Pagination for materialize: it returns the html with the series of links to the pages
9
11
  def pagy_materialize_nav(pagy, pagy_id: nil, link_extra: '')
@@ -39,13 +39,13 @@ class Pagy
39
39
  pagy = ::Pagy.new(vars)
40
40
  # with :last_page overflow we need to re-run the method in order to get the hits
41
41
  return pagy_meilisearch(pagy_search_args, vars.merge(page: pagy.page)) \
42
- if defined?(::Pagy::OverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
42
+ if defined?(::Pagy::OverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
43
43
 
44
44
  [pagy, results]
45
45
  end
46
46
 
47
- # Sub-method called only by #pagy_meilisearch: here for easy customization of variables by overriding
48
- # the _collection argument is not available when the method is called
47
+ # Sub-method called only by #pagy_meilisearch: here for easy customization of variables by overriding.
48
+ # The _collection argument is not available when the method is called.
49
49
  def pagy_meilisearch_get_vars(_collection, vars)
50
50
  pagy_set_items_from_params(vars) if defined?(ItemsExtra)
51
51
  vars[:items] ||= DEFAULT[:items]
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module NavsExtra
8
10
  # Javascript pagination: it returns a nav and a JSON tag used by the Pagy.nav javascript
9
11
  def pagy_nav_js(pagy, pagy_id: nil, link_extra: '', steps: nil)
@@ -35,6 +35,7 @@ class Pagy
35
35
  end
36
36
  end
37
37
 
38
+ # Special series for empty page
38
39
  module Series
39
40
  def series(size = @vars[:size])
40
41
  @page = @last # series for last page
@@ -7,8 +7,8 @@ class Pagy
7
7
  module SearchkickExtra
8
8
  module Searchkick
9
9
  # Return an array used to delay the call of #search
10
- # after the pagination variables are merged to the options
11
- # it also pushes to the same array an eventually called method
10
+ # after the pagination variables are merged to the options.
11
+ # It also pushes to the same array an optional method call.
12
12
  def pagy_searchkick(term = '*', **options, &block)
13
13
  [self, term, options, block].tap do |args|
14
14
  args.define_singleton_method(:method_missing) { |*a| args += a }
@@ -44,7 +44,7 @@ class Pagy
44
44
  pagy = ::Pagy.new(vars)
45
45
  # with :last_page overflow we need to re-run the method in order to get the hits
46
46
  return pagy_searchkick(pagy_search_args, vars.merge(page: pagy.page)) \
47
- if defined?(::Pagy::OverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
47
+ if defined?(::Pagy::OverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
48
48
 
49
49
  [pagy, called.empty? ? results : results.send(*called)]
50
50
  end
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module SemanticExtra
8
10
  # Pagination for semantic: it returns the html with the series of links to the pages
9
11
  def pagy_semantic_nav(pagy, pagy_id: nil, link_extra: '')
@@ -30,14 +30,14 @@ class Pagy
30
30
  # Additions for the Frontend
31
31
  module Frontend
32
32
  if defined?(Oj)
33
- # it returns a script tag with the JSON-serialized args generated with the faster oj gem
33
+ # Return a script tag with the JSON-serialized args generated with the faster oj gem
34
34
  def pagy_json_attr(pagy, *args)
35
35
  args << pagy.vars[:page_param] if pagy.vars[:trim_extra]
36
36
  %(data-pagy-json="#{Oj.dump(args, mode: :strict).gsub('"', '&quot;')}")
37
37
  end
38
38
  else
39
39
  require 'json'
40
- # it returns a script tag with the JSON-serialized args generated with the slower to_json
40
+ # Return a script tag with the JSON-serialized args generated with the slower to_json
41
41
  def pagy_json_attr(pagy, *args)
42
42
  args << pagy.vars[:page_param] if pagy.vars[:trim_extra]
43
43
  %(data-pagy-json="#{args.to_json.gsub('"', '&quot;')}")
@@ -30,21 +30,22 @@ class Pagy
30
30
  end
31
31
  end
32
32
 
33
- # Without any :url var it works exactly as the regular #pagy_url_for;
34
- # with a defined :url variable it does not use rack/request
33
+ # Return the URL for the page. If there is no pagy.vars[:url]
34
+ # it works exactly as the regular #pagy_url_for, relying on the params method and Rack.
35
+ # If there is a defined pagy.vars[:url] variable it does not need the params method nor Rack.
35
36
  def pagy_url_for(pagy, page, absolute: nil)
36
- p_vars = pagy.vars
37
- return super unless (url = p_vars[:url])
37
+ vars = pagy.vars
38
+ return super unless (url = vars[:url])
39
+
40
+ params = vars[:params]
41
+ params[vars[:page_param]] = page
42
+ params[vars[:items_param]] = vars[:items] if vars[:items_extra]
38
43
 
39
- params = p_vars[:params]
40
- params[p_vars[:page_param].to_s] = page
41
- params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(ItemsExtra)
42
- # no Rack required in standalone mode
43
44
  query_string = "?#{QueryUtils.build_nested_query(pagy_massage_params(params))}"
44
- "#{url}#{query_string}#{p_vars[:fragment]}"
45
+ "#{url}#{query_string}#{vars[:fragment]}"
45
46
  end
46
47
  end
47
- # In ruby 3+ we could just use `UrlHelpers.prepend StandaloneExtra` instead of using the next 2 lines
48
+ # In ruby 3+ `UrlHelpers.prepend StandaloneExtra` would be enough instead of using the next 2 lines
48
49
  Frontend.prepend StandaloneExtra
49
50
  Backend.prepend StandaloneExtra
50
51
 
@@ -3,14 +3,17 @@
3
3
 
4
4
  class Pagy
5
5
  module SupportExtra
6
+ # Return the previous page URL string or nil
6
7
  def pagy_prev_url(pagy)
7
8
  pagy_url_for(pagy, pagy.prev) if pagy.prev
8
9
  end
9
10
 
11
+ # Return the next page URL string or nil
10
12
  def pagy_next_url(pagy)
11
13
  pagy_url_for(pagy, pagy.next) if pagy.next
12
14
  end
13
15
 
16
+ # Return the HTML string for the previous page link
14
17
  def pagy_prev_link(pagy, text: pagy_t('pagy.nav.prev'), link_extra: '')
15
18
  if pagy.prev
16
19
  %(<span class="page prev"><a href="#{
@@ -23,6 +26,7 @@ class Pagy
23
26
  end
24
27
  end
25
28
 
29
+ # Return the HTML string for the next page link
26
30
  def pagy_next_link(pagy, text: pagy_t('pagy.nav.next'), link_extra: '')
27
31
  if pagy.next
28
32
  %(<span class="page next"><a href="#{
@@ -35,10 +39,12 @@ class Pagy
35
39
  end
36
40
  end
37
41
 
42
+ # Return the HTML link tag for the previous page or nil
38
43
  def pagy_prev_link_tag(pagy)
39
44
  %(<link href="#{pagy_url_for(pagy, pagy.prev)}" rel="prev"/>) if pagy.prev
40
45
  end
41
46
 
47
+ # Return the HTML link tag for the next page or nil
42
48
  def pagy_next_link_tag(pagy)
43
49
  %(<link href="#{pagy_url_for(pagy, pagy.next)}" rel="next"/>) if pagy.next
44
50
  end
@@ -5,7 +5,8 @@ class Pagy
5
5
  DEFAULT[:trim_extra] = true # extra enabled by default
6
6
 
7
7
  module TrimExtra
8
- # Override the original using the pagy_trim method
8
+ # Override the original pagy_link_proc.
9
+ # Call the pagy_trim method if the trim_extra is enabled.
9
10
  def pagy_link_proc(pagy, link_extra: '')
10
11
  link_proc = super(pagy, link_extra: link_extra)
11
12
  return link_proc unless pagy.vars[:trim_extra]
@@ -4,6 +4,8 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
7
9
  module UikitExtra
8
10
  # Pagination for uikit: it returns the html with the series of links to the pages
9
11
  def pagy_uikit_nav(pagy, pagy_id: nil, link_extra: '')
data/lib/pagy/frontend.rb CHANGED
@@ -8,8 +8,8 @@ class Pagy
8
8
  # Used for search and replace, hardcoded also in the pagy.js file
9
9
  PAGE_PLACEHOLDER = '__pagy_page__'
10
10
 
11
- # All the code here has been optimized for performance: it may not look very pretty
12
- # (as most code dealing with many long strings), but its performance makes it very sexy! ;)
11
+ # Frontend modules are specially optimized for performance.
12
+ # The resulting code may not look very elegant, but produces the best benchmarks
13
13
  module Frontend
14
14
  include UrlHelpers
15
15
 
data/lib/pagy/i18n.rb CHANGED
@@ -10,7 +10,7 @@ class Pagy
10
10
 
11
11
  # Pluralization rules
12
12
  module P11n
13
- # utility variables
13
+ # Utility variables
14
14
  from0to1 = [0, 1].freeze
15
15
  from2to4 = [2, 3, 4].freeze
16
16
  from5to9 = [5, 6, 7, 8, 9].freeze
@@ -3,15 +3,17 @@
3
3
  class Pagy
4
4
  # Provide the helpers to handle the url in frontend and backend
5
5
  module UrlHelpers
6
- # This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
6
+ # Return the URL for the page, relying on the params method and Rack by default.
7
+ # It supports all Rack-based frameworks (Sinatra, Padrino, Rails, ...).
8
+ # For non-rack environments you can use the standalone extra
7
9
  def pagy_url_for(pagy, page, absolute: nil)
8
- p_vars = pagy.vars
9
- params = request.GET.merge(p_vars[:params])
10
- params[p_vars[:page_param].to_s] = page
11
- params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(ItemsExtra)
12
- # we rely on Rack by default: use the standalone extra in non rack environments
10
+ vars = pagy.vars
11
+ params = self.params.merge(vars[:params])
12
+ params[vars[:page_param]] = page
13
+ params[vars[:items_param]] = vars[:items] if vars[:items_extra]
14
+
13
15
  query_string = "?#{Rack::Utils.build_nested_query(pagy_massage_params(params))}"
14
- "#{request.base_url if absolute}#{request.path}#{query_string}#{p_vars[:fragment]}"
16
+ "#{request.base_url if absolute}#{request.path}#{query_string}#{vars[:fragment]}"
15
17
  end
16
18
 
17
19
  # Sub-method called only by #pagy_url_for: here for easy customization of params by overriding
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.0.1'
8
+ VERSION = '5.1.0'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagy
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-18 00:00:00.000000000 Z
11
+ date: 2021-10-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Agnostic pagination in plain ruby. It does it all. Better.
14
14
  email: