pagy 1.3.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/pagy.rb +32 -10
  3. data/lib/javascripts/pagy.js +0 -7
  4. data/lib/locales/utils/i18n.rb +18 -0
  5. data/lib/locales/utils/loader.rb +29 -0
  6. data/lib/locales/{plurals.rb → utils/p11n.rb} +20 -18
  7. data/lib/pagy.rb +3 -2
  8. data/lib/pagy/backend.rb +2 -0
  9. data/lib/pagy/countless.rb +4 -1
  10. data/lib/pagy/extras/array.rb +2 -0
  11. data/lib/pagy/extras/bootstrap.rb +12 -14
  12. data/lib/pagy/extras/bulma.rb +10 -12
  13. data/lib/pagy/extras/countless.rb +2 -0
  14. data/lib/pagy/extras/elasticsearch_rails.rb +31 -7
  15. data/lib/pagy/extras/foundation.rb +11 -13
  16. data/lib/pagy/extras/i18n.rb +7 -4
  17. data/lib/pagy/extras/items.rb +16 -17
  18. data/lib/pagy/extras/materialize.rb +11 -13
  19. data/lib/pagy/extras/overflow.rb +36 -34
  20. data/lib/pagy/extras/pagy_search.rb +16 -0
  21. data/lib/pagy/extras/plain.rb +4 -5
  22. data/lib/pagy/extras/searchkick.rb +31 -8
  23. data/lib/pagy/extras/semantic.rb +8 -10
  24. data/lib/pagy/extras/shared.rb +1 -7
  25. data/lib/pagy/extras/support.rb +2 -1
  26. data/lib/pagy/extras/trim.rb +2 -1
  27. data/lib/pagy/frontend.rb +17 -26
  28. data/lib/templates/bootstrap_nav.html.erb +1 -1
  29. data/lib/templates/bootstrap_nav.html.haml +1 -1
  30. data/lib/templates/bootstrap_nav.html.slim +1 -1
  31. data/lib/templates/bulma_nav.html.erb +1 -1
  32. data/lib/templates/bulma_nav.html.haml +1 -1
  33. data/lib/templates/bulma_nav.html.slim +1 -1
  34. data/lib/templates/foundation_nav.html.erb +1 -1
  35. data/lib/templates/foundation_nav.html.haml +1 -1
  36. data/lib/templates/foundation_nav.html.slim +1 -1
  37. data/pagy.gemspec +2 -5
  38. metadata +10 -10
  39. data/lib/pagy/extras/navs.rb +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b68e52cc18266e62b2156ebfafb24479c052e019128a5228c42203d516a17fd6
4
- data.tar.gz: 86065e2c0d2bfdc27330a941e165d33068eaae67cd4ef8521c8e0d18b6ef2fda
3
+ metadata.gz: 984f87dd6d805faf04c9c980bc8b5e2da6d90963beae920805ece77a627ed6f0
4
+ data.tar.gz: '095f11ccb37ee8c4a64e714f0d3384f4c7f6cdddbafc4c4f34b9a3d2efd4c2de'
5
5
  SHA512:
6
- metadata.gz: 66c438b46a53076f3780da6f0c51c92b5571bdb3c6c627805695d0868cc25ea8edcb6122d457e59ca79f6e84e10e435e7d0f30a201bfcc6fd8130b5a2d235410
7
- data.tar.gz: 32aa96c1331ea6a0913b6889b445e0b05fa12c1fdf6e3232bb55a93ea990a38417f89cb809f76c6b854aecd6c564ba7886a789fba0107e704bc4c0985758ef04
6
+ metadata.gz: 9ade0c565e6f30f118ee30884456c1185c3f7dde0723aa6688734ac974dfc41fb0e16979a78d25042ee469178afe02244594a8366bb3e9f2057b031a378a062c
7
+ data.tar.gz: 7087679d2675ac589627085522871b53f59f27be60298fadb4e64752a12e21f3c23fcc13cd72b22ebe7c25a0bcd5c7a3064c4383aad5995e115b10fed1a4be0d
@@ -1,5 +1,8 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  # Pagy initializer file
2
- # Customize only what you really need but notice that Pagy works also without any of the following lines.
5
+ # Customize only what you really need and notice that Pagy works also without any of the following lines.
3
6
 
4
7
 
5
8
  # Extras
@@ -111,15 +114,34 @@
111
114
 
112
115
  # I18n
113
116
 
114
- # I18n faster internal pagy implementation (does not use the I18n gem)
115
- # Use only for single language apps that don't need dynamic translation between multiple languages
117
+ # Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
116
118
  # See https://ddnexus.github.io/pagy/api/frontend#i18n
117
- # Notice: Do not use any of the following lines if you use the i18n extra below
118
- # Pagy::Frontend::I18N.load(file: Pagy.root.join('locale', 'es.yml'), language:'es') # load 'es' pagy language file
119
- # Pagy::Frontend::I18N.load(file:'path/to/dictionary.yml', language:'en') # load a custom 'en' file
120
- # Pagy::Frontend::I18N[:plural] = -> (count) {(['zero', 'one'][count] || 'other')} # default
121
-
122
- # I18n extra: Use the `I18n` gem instead of the pagy implementation
123
- # (slower but allows dynamic translation between multiple languages)
119
+ # Notice: No need to use any of the following lines if you use the i18n extra below
120
+ #
121
+ # Examples:
122
+ # load the "de" built-in locale:
123
+ # Pagy::I18n.load(locale: 'de')
124
+ #
125
+ # load the "de" locale defined in the custom file at :filepath:
126
+ # Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
127
+ #
128
+ # load the "de", "en" and "es" built-in locales:
129
+ # (the first passed :locale will be used also as the default_locale)
130
+ # Pagy::I18n.load({locale: 'de'},
131
+ # {locale: 'en'},
132
+ # {locale: 'es'})
133
+ #
134
+ # load the "en" built-in locale, a custom "es" locale,
135
+ # and a totally custom locale complete with the :pluralize proc:
136
+ # (the first passed :locale will be used also as the default_locale)
137
+ # Pagy::I18n.load({locale: 'en'},
138
+ # {locale: 'es', filepath: 'path/to/pagy-es.yml'},
139
+ # {locale: 'xyz', # not built-in
140
+ # filepath: 'path/to/pagy-xyz.yml',
141
+ # pluralize: lambda{|count| ... } )
142
+
143
+
144
+ # I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
145
+ # than the default pagy internal i18n (see above)
124
146
  # See https://ddnexus.github.io/pagy/extras/i18n
125
147
  # require 'pagy/extras/i18n'
@@ -84,13 +84,6 @@ Pagy.init = function(arg){
84
84
  var args = JSON.parse(jsonTags[i].innerHTML);
85
85
  Pagy[args.shift()].apply(null, args);
86
86
  }
87
- // Support for legacy overridden helpers. It will be removed in 2.0
88
- ['compact', 'items', 'responsive'].forEach(function(name){
89
- var json = document.getElementsByClassName("pagy-"+name+"-json");
90
- for (var i = 0, len = json.length; i < len; i++) {
91
- Pagy[name].apply(null, JSON.parse(json[i].innerHTML))
92
- }
93
- })
94
87
  };
95
88
 
96
89
  // namespace for custom init functions
@@ -0,0 +1,18 @@
1
+ # See https://ddnexus.github.io/pagy/api/frontend#i18n
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
4
+
5
+ # this file returns the I18n hash used as default alternative to the i18n gem
6
+
7
+ Hash.new{|h,_| h.first[1]}.tap do |i18n| # first loaded locale used as default
8
+ i18n.define_singleton_method(:load) do |*args|
9
+ # eval: we don't need to keep the loader proc in memory
10
+ eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n, *args) #rubocop:disable Security/Eval
11
+ end
12
+ i18n.define_singleton_method(:t) do |locale, path, vars={}|
13
+ data, pluralize = self[locale]
14
+ translate = data[path] || vars[:count] && data[path+=".#{pluralize.call(vars[:count])}"] or return %([translation missing: "#{path}"])
15
+ translate.call(vars)
16
+ end
17
+ i18n.load(locale: 'en')
18
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ # the whole file will be eval'ed/executed and gc-collected after returning/executing the loader proc
5
+
6
+ # eval: no need for the whole file in memory
7
+ p11n = eval(Pagy.root.join('locales', 'utils', 'p11n.rb').read) #rubocop:disable Security/Eval
8
+
9
+ # flatten the dictionary file nested keys
10
+ # convert each value to a simple ruby interpolation proc
11
+ flatten = lambda do |hash, key=''|
12
+ hash.each.reduce({}) do |h, (k, v)|
13
+ v.is_a?(Hash) \
14
+ ? h.merge!(flatten.call(v, "#{key}#{k}."))
15
+ : h.merge!(eval %({"#{key}#{k}" => lambda{|vars|"#{v.gsub(/%{[^}]+?}/){|m| "\#{vars[:#{m[2..-2]}]||'#{m}'}" }}"}})) #rubocop:disable Security/Eval
16
+ end
17
+ end
18
+
19
+ # loader proc
20
+ lambda do |i18n, *args|
21
+ i18n.clear
22
+ args.each do |arg|
23
+ arg[:filepath] ||= Pagy.root.join('locales', "#{arg[:locale]}.yml")
24
+ arg[:pluralize] ||= p11n[arg[:locale]]
25
+ hash = YAML.load(File.read(arg[:filepath], encoding: 'UTF-8')) #rubocop:disable Security/YAMLLoad
26
+ hash.key?(arg[:locale]) or raise ArgumentError, %(Pagy::I18n.load: :locale "#{arg[:locale]}" not found in :filepath "#{arg[:filepath].inspect}")
27
+ i18n[arg[:locale]] = [flatten.call(hash[arg[:locale]]), arg[:pluralize]]
28
+ end
29
+ end
@@ -1,7 +1,10 @@
1
- # This file adds support for multiple built-in plualization types.
2
- # It defines the pluralization procs and gets eval(ed) at I18N.load time.
1
+ # See https://ddnexus.github.io/pagy/api/frontend#i18n
2
+ # encoding: utf-8
3
3
  # frozen_string_literal: true
4
4
 
5
+ # This file adds support for multiple built-in plualization types.
6
+ # It defines the pluralization procs and gets eval(ed) and gc-collected at Pagy::I18n.load time.
7
+
5
8
  # utility variables
6
9
  zero_one = ['zero', 'one'].freeze
7
10
  from2to4 = (2..4).freeze
@@ -9,13 +12,13 @@ from5to9 = (5..9).freeze
9
12
  from11to14 = (11..14).freeze
10
13
  from12to14 = (12..14).freeze
11
14
 
12
- # Plurals
13
- # A plural proc returns a plural type string based on the passed count
14
- # Each plural proc may apply to one or more languages below
15
- plurals = {
16
- zero_one_other: -> (count) {zero_one[count] || 'other'},
15
+ # Pluralization (p11n)
16
+ # A pluralization proc returns a plural type string based on the passed count
17
+ # Each proc may apply to one or more locales below
18
+ p11n = {
19
+ zero_one_other: lambda {|count| zero_one[count] || 'other'},
17
20
 
18
- zero_one_few_many_other: -> (count) do
21
+ zero_one_few_many_other: lambda do |count|
19
22
  mod10, mod100 = count % 10, count % 100
20
23
  if count == 0 ; 'zero'
21
24
  elsif mod10 == 1 && mod100 != 11 ; 'one'
@@ -25,7 +28,7 @@ plurals = {
25
28
  end
26
29
  end,
27
30
 
28
- pl: -> (count) do
31
+ pl: lambda do |count|
29
32
  mod10, mod100 = count % 10, count % 100
30
33
  if count == 0 ; 'zero'
31
34
  elsif count == 1 ; 'one'
@@ -36,14 +39,13 @@ plurals = {
36
39
  end
37
40
  }
38
41
 
39
- # Languages (language/plural pairs)
40
- # Contain all the entries for all the languages defined in the dictionaries.
41
- # The default plural for languages not explicitly listed here
42
- # is the :zero_one_other plural (used for English)
43
- Hash.new(plurals[:zero_one_other]).tap do |languages|
44
- languages['en'] = plurals[:zero_one_other]
45
- languages['ru'] = plurals[:zero_one_few_many_other]
46
- languages['pl'] = plurals[:pl]
42
+ # Hash of locale/pluralization pairs
43
+ # Contain all the entries for all the locales defined as dictionaries.
44
+ # The default pluralization for locales not explicitly listed here
45
+ # is the :zero_one_other pluralization proc (used for English)
46
+ Hash.new(p11n[:zero_one_other]).tap do |hash|
47
+ hash['ru'] = p11n[:zero_one_few_many_other]
48
+ hash['pl'] = p11n[:pl]
47
49
  end
48
50
 
49
- # PR for other languages and plurals are very welcome. Thanks!
51
+ # PR for other locales and pluralizations are very welcome. Thanks!
@@ -1,14 +1,15 @@
1
1
  # See Pagy API documentation: https://ddnexus.github.io/pagy/api/pagy
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pathname'
5
6
 
6
- class Pagy ; VERSION = '1.3.3'
7
+ class Pagy ; VERSION = '2.0.0'
7
8
 
8
9
  class OverflowError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end
9
10
 
10
11
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
- def self.root; Pathname.new(__FILE__).dirname end
12
+ def self.root; @root ||= Pathname.new(__FILE__).dirname.freeze end
12
13
 
13
14
  # default vars
14
15
  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 }
@@ -1,4 +1,6 @@
1
1
  # See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/api/backend
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
2
4
 
3
5
  class Pagy
4
6
  # Defines a few generic methods to paginate an ORM collection out of the box,
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  require 'pagy'
2
5
 
3
6
  class Pagy
@@ -6,7 +9,7 @@ class Pagy
6
9
 
7
10
  # Merge and validate the options, do some simple arithmetic and set a few instance variables
8
11
  def initialize(vars={})
9
- @vars ||= VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden)
12
+ @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden)
10
13
  { items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
11
14
  (@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
12
15
  or raise(ArgumentError, "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
@@ -1,4 +1,6 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/array
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
2
4
 
3
5
  class Pagy
4
6
  # Add specialized backend methods to paginate array collections
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bootstrap
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -8,10 +9,10 @@ class Pagy
8
9
 
9
10
  # Pagination for bootstrap: it returns the html with the series of links to the pages
10
11
  def pagy_bootstrap_nav(pagy)
11
- html, link, p_prev, p_next = +'', pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
12
+ link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
12
13
 
13
- html << (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
14
- : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
14
+ html = EMPTY + (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
15
+ : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
15
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
16
17
  html << if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
17
18
  elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
@@ -20,35 +21,33 @@ class Pagy
20
21
  end
21
22
  html << (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
22
23
  : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
23
- %(<nav class="pagy-nav-bootstrap pagy-bootstrap-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
24
+ %(<nav class="pagy-bootstrap-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
24
25
  end
25
- deprecate :pagy_nav_bootstrap, :pagy_bootstrap_nav
26
26
 
27
27
  # Compact pagination for bootstrap: it returns the html with the series of links to the pages
28
28
  # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
29
29
  def pagy_bootstrap_compact_nav(pagy, id=pagy_id)
30
- html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
30
+ link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
31
31
 
32
- html << %(<nav id="#{id}" class="pagy-nav-compact-bootstrap pagy-bootstrap-compact-nav pagination" role="navigation" aria-label="pager">)
32
+ html = EMPTY + %(<nav id="#{id}" class="pagy-bootstrap-compact-nav pagination" role="navigation" aria-label="pager">)
33
33
  html << link.call(MARKER, '', %(style="display: none;" ))
34
34
  (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
35
35
  html << %(<div class="btn-group" role="group">)
36
36
  html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
37
37
  : %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
38
38
  input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="text-primary" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
39
- html << %(<div class="pagy-compact-input btn btn-primary disabled">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
39
+ html << %(<div class="pagy-compact-input btn btn-primary disabled" style="white-space: nowrap;">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
40
40
  html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
41
41
  : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
42
42
  html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
43
43
  end
44
- deprecate :pagy_nav_compact_bootstrap, :pagy_bootstrap_compact_nav
45
44
 
46
45
  # Responsive pagination for bootstrap: it returns the html with the series of links to the pages
47
46
  # rendered by the Pagy.responsive javascript
48
47
  def pagy_bootstrap_responsive_nav(pagy, id=pagy_id)
49
48
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next, pagy.responsive
50
49
 
51
- tags['before'] = +'<ul class="pagination">'
50
+ tags['before'] = EMPTY + '<ul class="pagination">'
52
51
  tags['before'] << (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
53
52
  : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
54
53
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
@@ -57,13 +56,12 @@ class Pagy
57
56
  elsif item == :gap ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
58
57
  end
59
58
  end
60
- tags['after'] = +(p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
61
- : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
59
+ tags['after'] = EMPTY + (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
60
+ : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
62
61
  tags['after'] << '</ul>'
63
62
  script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
64
- %(<nav id="#{id}" class="pagy-nav-responsive-bootstrap pagy-bootstrap-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
63
+ %(<nav id="#{id}" class="pagy-bootstrap-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
65
64
  end
66
- deprecate :pagy_nav_responsive_bootstrap, :pagy_bootstrap_responsive_nav
67
65
 
68
66
  end
69
67
  end
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -8,10 +9,10 @@ class Pagy
8
9
 
9
10
  # Pagination for Bulma: it returns the html with the series of links to the pages
10
11
  def pagy_bulma_nav(pagy)
11
- html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next
12
+ link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
12
13
 
13
- html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
14
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>))
14
+ html = EMPTY + (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
15
+ : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>))
15
16
  html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
16
17
  : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
17
18
  html << '<ul class="pagination-list">'
@@ -22,16 +23,15 @@ class Pagy
22
23
  end
23
24
  end
24
25
  html << '</ul>'
25
- %(<nav class="pagy-nav-bulma pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
26
+ %(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
26
27
  end
27
- deprecate :pagy_nav_bulma, :pagy_bulma_nav
28
28
 
29
29
  # Compact pagination for Bulma: it returns the html with the series of links to the pages
30
30
  # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
31
31
  def pagy_bulma_compact_nav(pagy, id=pagy_id)
32
- html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
32
+ link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
33
33
 
34
- html << %(<nav id="#{id}" class="pagy-nav-compact-bulma pagy-bulma-compact-nav" role="navigation" aria-label="pagination">)
34
+ html = EMPTY + %(<nav id="#{id}" class="pagy-bulma-compact-nav" role="navigation" aria-label="pagination">)
35
35
  html << link.call(MARKER, '', 'style="display: none;"')
36
36
  (html << link.call(1, '', %(style="display: none;"))) if defined?(TRIM)
37
37
  html << %(<div class="field is-grouped is-grouped-centered" role="group">)
@@ -43,15 +43,14 @@ class Pagy
43
43
  : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
44
44
  html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
45
45
  end
46
- deprecate :pagy_nav_compact_bulma, :pagy_bulma_compact_nav
47
46
 
48
47
  # Responsive pagination for Bulma: it returns the html with the series of links to the pages
49
48
  # rendered by the Pagy.responsive javascript
50
49
  def pagy_bulma_responsive_nav(pagy, id=pagy_id)
51
50
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
52
51
 
53
- tags['before'] = +(p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
54
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>))
52
+ tags['before'] = EMPTY + (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
53
+ : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>))
55
54
  tags['before'] << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
56
55
  : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
57
56
  tags['before'] << '<ul class="pagination-list">'
@@ -63,9 +62,8 @@ class Pagy
63
62
  end
64
63
  tags['after'] = '</ul>'
65
64
  script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
66
- %(<nav id="#{id}" class="pagy-nav-responsive-bulma pagy-bulma-responsive-nav pagination is-centered" role="navigation" aria-label="pagination"></nav>#{script})
65
+ %(<nav id="#{id}" class="pagy-bulma-responsive-nav pagination is-centered" role="navigation" aria-label="pagination"></nav>#{script})
67
66
  end
68
- deprecate :pagy_nav_responsive_bulma, :pagy_bulma_responsive_nav
69
67
 
70
68
  end
71
69
  end
@@ -1,4 +1,6 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/countless
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
2
4
 
3
5
  require 'pagy/countless'
4
6
 
@@ -1,19 +1,43 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/elasticsearch_rails
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
4
+
5
+ require 'pagy/extras/pagy_search'
2
6
 
3
7
  class Pagy
4
- # Add specialized backend methods to paginate ElasticsearchRails::Results
8
+
9
+ # used by the items extra
10
+ ELASTICSEARCH_RAILS = true
11
+
12
+ # create a Pagy object from an Elasticsearch::Model::Response::Response object
13
+ def self.new_from_elasticsearch_rails(response, vars={})
14
+ vars[:items] = response.search.options[:size] || 10
15
+ vars[:page] = (response.search.options[:from] || 0) / vars[:items] + 1
16
+ vars[:count] = response.raw_response['hits']['total']
17
+ new(vars)
18
+ end
19
+
20
+ # Add specialized backend methods to paginate ElasticsearchRails searches
5
21
  module Backend ; private
22
+
6
23
  # Return Pagy object and items
7
- def pagy_elasticsearch_rails(results, vars={})
8
- pagy = Pagy.new(pagy_elasticsearch_rails_get_vars(results, vars))
9
- return pagy, results.offset(pagy.offset).limit(pagy.items)
24
+ def pagy_elasticsearch_rails(search_args, vars={})
25
+ model, query_or_payload, options, _block, *called = search_args
26
+ vars = pagy_elasticsearch_rails_get_vars(nil, vars)
27
+ options[:size] = vars[:items]
28
+ options[:from] = vars[:items] * (vars[:page] - 1)
29
+ response = model.search(query_or_payload, options)
30
+ vars[:count] = response.raw_response['hits']['total']
31
+ return Pagy.new(vars), called.empty? ? response : response.send(*called)
10
32
  end
11
33
 
12
34
  # Sub-method called only by #pagy_elasticsearch_rails: here for easy customization of variables by overriding
13
- def pagy_elasticsearch_rails_get_vars(results, vars)
14
- vars[:count] ||= results.total
15
- vars[:page] ||= params[:page] || 1
35
+ # the _collection argument is not available when the method is called
36
+ def pagy_elasticsearch_rails_get_vars(_collection, vars)
37
+ vars[:items] ||= VARS[:items]
38
+ vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
16
39
  vars
17
40
  end
41
+
18
42
  end
19
43
  end