pagy 4.11.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/pagy.rb +66 -45
  3. data/lib/javascripts/pagy.js +15 -6
  4. data/lib/pagy/backend.rb +6 -10
  5. data/lib/pagy/console.rb +5 -4
  6. data/lib/pagy/countless.rb +13 -20
  7. data/lib/pagy/exceptions.rb +2 -4
  8. data/lib/pagy/extras/arel.rb +6 -6
  9. data/lib/pagy/extras/array.rb +6 -6
  10. data/lib/pagy/extras/bootstrap.rb +32 -29
  11. data/lib/pagy/extras/bulma.rb +40 -32
  12. data/lib/pagy/extras/countless.rb +8 -9
  13. data/lib/pagy/extras/elasticsearch_rails.rb +64 -47
  14. data/lib/pagy/extras/foundation.rb +26 -26
  15. data/lib/pagy/extras/gearbox.rb +42 -0
  16. data/lib/pagy/extras/headers.rb +24 -16
  17. data/lib/pagy/extras/i18n.rb +7 -16
  18. data/lib/pagy/extras/items.rb +37 -38
  19. data/lib/pagy/extras/materialize.rb +28 -30
  20. data/lib/pagy/extras/meilisearch.rb +50 -45
  21. data/lib/pagy/extras/metadata.rb +29 -13
  22. data/lib/pagy/extras/navs.rb +24 -26
  23. data/lib/pagy/extras/overflow.rb +57 -60
  24. data/lib/pagy/extras/searchkick.rb +51 -45
  25. data/lib/pagy/extras/semantic.rb +28 -30
  26. data/lib/pagy/extras/shared.rb +44 -40
  27. data/lib/pagy/extras/standalone.rb +37 -42
  28. data/lib/pagy/extras/support.rb +14 -13
  29. data/lib/pagy/extras/trim.rb +10 -11
  30. data/lib/pagy/extras/uikit.rb +27 -28
  31. data/lib/pagy/frontend.rb +22 -47
  32. data/lib/pagy/i18n.rb +159 -0
  33. data/lib/pagy/url_helpers.rb +22 -0
  34. data/lib/pagy.rb +52 -26
  35. data/lib/templates/uikit_nav.html.erb +1 -1
  36. data/lib/templates/uikit_nav.html.slim +1 -1
  37. metadata +7 -10
  38. data/lib/locales/utils/i18n.rb +0 -17
  39. data/lib/locales/utils/loader.rb +0 -31
  40. data/lib/locales/utils/p11n.rb +0 -112
  41. data/lib/pagy/deprecation.rb +0 -27
@@ -4,8 +4,7 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
- module Frontend
8
-
7
+ module SemanticExtra
9
8
  # Pagination for semantic: it returns the html with the series of links to the pages
10
9
  def pagy_semantic_nav(pagy, pagy_id: nil, link_extra: '')
11
10
  p_id = %( id="#{pagy_id}") if pagy_id
@@ -13,7 +12,7 @@ class Pagy
13
12
 
14
13
  html = +%(<div#{p_id} class="pagy-semantic-nav ui pagination menu">)
15
14
  html << pagy_semantic_prev_html(pagy, link)
16
- pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
15
+ pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
16
  html << case item
18
17
  when Integer then link.call item # page link
19
18
  when String then %(<a class="item active">#{item}</a>) # current page
@@ -25,8 +24,7 @@ class Pagy
25
24
  end
26
25
 
27
26
  # Javascript pagination for semantic: it returns a nav and a JSON tag used by the Pagy.nav javascript
28
- def pagy_semantic_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
29
- pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
27
+ def pagy_semantic_nav_js(pagy, pagy_id: nil, link_extra: '', steps: nil)
30
28
  p_id = %( id="#{pagy_id}") if pagy_id
31
29
  link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra}))
32
30
  tags = { 'before' => pagy_semantic_prev_html(pagy, link),
@@ -35,46 +33,46 @@ class Pagy
35
33
  'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
36
34
  'after' => pagy_semantic_next_html(pagy, link) }
37
35
 
38
- %(<div#{p_id} class="pagy-njs pagy-semantic-nav-js ui pagination menu" role="navigation" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
36
+ %(<div#{p_id} class="pagy-njs pagy-semantic-nav-js ui pagination menu" role="navigation" #{
37
+ pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
39
38
  end
40
39
 
41
40
  # Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
42
- def pagy_semantic_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
43
- pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
41
+ def pagy_semantic_combo_nav_js(pagy, pagy_id: nil, link_extra: '')
44
42
  p_id = %( id="#{pagy_id}") if pagy_id
45
43
  link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra}))
46
44
  p_page = pagy.page
47
45
  p_pages = pagy.pages
48
- input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin: 0 0.3rem">)
46
+ input = %(<input type="number" min="1" max="#{p_pages}" value="#{
47
+ p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length + 1}rem; margin: 0 0.3rem">)
49
48
 
50
49
  %(<div#{p_id} class="pagy-semantic-combo-nav-js ui compact menu" role="navigation" #{
51
- pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
52
- }>#{
53
- pagy_semantic_prev_html pagy, link
54
- }<div class="pagy-combo-input item">#{
55
- pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
56
- }</div> #{
57
- pagy_semantic_next_html pagy, link
58
- }</div>)
50
+ pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)}>#{
51
+ pagy_semantic_prev_html pagy, link
52
+ }<div class="pagy-combo-input item">#{
53
+ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
54
+ }</div> #{
55
+ pagy_semantic_next_html pagy, link
56
+ }</div>)
59
57
  end
60
58
 
61
59
  private
62
60
 
63
- def pagy_semantic_prev_html(pagy, link)
64
- if (p_prev = pagy.prev)
65
- link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'
66
- else
67
- +%(<div class="item disabled"><i class="left small chevron icon"></i></div>)
68
- end
61
+ def pagy_semantic_prev_html(pagy, link)
62
+ if (p_prev = pagy.prev)
63
+ link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'
64
+ else
65
+ +%(<div class="item disabled"><i class="left small chevron icon"></i></div>)
69
66
  end
67
+ end
70
68
 
71
- def pagy_semantic_next_html(pagy, link)
72
- if (p_next = pagy.next)
73
- link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'
74
- else
75
- +%(<div class="item disabled"><i class="right small chevron icon"></i></div>)
76
- end
69
+ def pagy_semantic_next_html(pagy, link)
70
+ if (p_next = pagy.next)
71
+ link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'
72
+ else
73
+ +%(<div class="item disabled"><i class="right small chevron icon"></i></div>)
77
74
  end
78
-
75
+ end
79
76
  end
77
+ Frontend.prepend SemanticExtra
80
78
  end
@@ -3,50 +3,54 @@
3
3
  require 'digest'
4
4
 
5
5
  class Pagy
6
-
7
- # default :steps: false will use {0 => @vars[:size]}
8
- VARS[:steps] = false
9
-
10
- # `Pagy` instance method used by the `pagy*_nav_js` helpers.
11
- # It returns the sequels of width/series generated from the :steps hash
12
- # Example:
13
- # >> pagy = Pagy.new(count:1000, page: 20, steps: {0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]})
14
- # >> pagy.sequels
15
- # #=> { "0" => [1, :gap, 18, 19, "20", 21, 22, :gap, 50],
16
- # "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
17
- # "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
18
- # Notice: if :steps is false it will use the single {0 => @vars[:size]} size
19
- def sequels(steps=nil)
20
- steps ||= @vars[:steps] || {0 => @vars[:size]}
21
- raise VariableError.new(self), "expected :steps to define the 0 width; got #{steps.inspect}" \
22
- unless steps.key?(0)
23
- {}.tap do |sequels|
24
- steps.each {|width, size| sequels[width.to_s] = series(size)}
6
+ DEFAULT[:steps] = false # default false will use {0 => @vars[:size]}
7
+
8
+ module SharedExtra
9
+ # Additions for the Pagy class
10
+ module Pagy
11
+ # `Pagy` instance method used by the `pagy*_nav_js` helpers.
12
+ # It returns the sequels of width/series generated from the :steps hash
13
+ # Example:
14
+ # >> pagy = Pagy.new(count:1000, page: 20, steps: {0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]})
15
+ # >> pagy.sequels
16
+ # #=> { "0" => [1, :gap, 18, 19, "20", 21, 22, :gap, 50],
17
+ # "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
18
+ # "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
19
+ # Notice: if :steps is false it will use the single {0 => @vars[:size]} size
20
+ def sequels(steps = nil)
21
+ steps ||= @vars[:steps] || { 0 => @vars[:size] }
22
+ raise VariableError.new(self), "expected :steps to define the 0 width; got #{steps.inspect}" \
23
+ unless steps.key?(0)
24
+
25
+ {}.tap do |sequels|
26
+ steps.each { |width, size| sequels[width.to_s] = series(size) }
27
+ end
28
+ end
25
29
  end
26
- end
27
-
28
- module Frontend
29
30
 
30
- if defined?(Oj)
31
- # it returns a script tag with the JSON-serialized args generated with the faster oj gem
32
- def pagy_json_attr(pagy, *args)
33
- args << pagy.vars[:page_param] if pagy.vars[:enable_trim_extra]
34
- %(data-pagy-json="#{Oj.dump(args, mode: :strict).gsub('"', '&quot;')}")
31
+ # Additions for the Frontend
32
+ module Frontend
33
+ if defined?(Oj)
34
+ # it returns a script tag with the JSON-serialized args generated with the faster oj gem
35
+ def pagy_json_attr(pagy, *args)
36
+ args << pagy.vars[:page_param] if pagy.vars[:trim_extra]
37
+ %(data-pagy-json="#{Oj.dump(args, mode: :strict).gsub('"', '&quot;')}")
38
+ end
39
+ else
40
+ require 'json'
41
+ # it returns a script tag with the JSON-serialized args generated with the slower to_json
42
+ def pagy_json_attr(pagy, *args)
43
+ args << pagy.vars[:page_param] if pagy.vars[:trim_extra]
44
+ %(data-pagy-json="#{args.to_json.gsub('"', '&quot;')}")
45
+ end
35
46
  end
36
- else
37
- require 'json'
38
- # it returns a script tag with the JSON-serialized args generated with the slower to_json
39
- def pagy_json_attr(pagy, *args)
40
- args << pagy.vars[:page_param] if pagy.vars[:enable_trim_extra]
41
- %(data-pagy-json="#{args.to_json.gsub('"', '&quot;')}")
42
- end
43
- end
44
47
 
45
- # it returns the marked link to used by pagy.js
46
- def pagy_marked_link(link)
47
- link.call PAGE_PLACEHOLDER, '', 'style="display: none;"'
48
+ # Return the marked link to used by pagy.js
49
+ def pagy_marked_link(link)
50
+ link.call PAGE_PLACEHOLDER, '', 'style="display: none;"'
51
+ end
48
52
  end
49
-
50
53
  end
51
-
54
+ prepend SharedExtra::Pagy
55
+ Frontend.prepend SharedExtra::Frontend
52
56
  end
@@ -3,60 +3,55 @@
3
3
 
4
4
  require 'uri'
5
5
  class Pagy
6
+ module StandaloneExtra
7
+ # Extracted from Rack::Utils and reformatted for rubocop
8
+ module QueryUtils
9
+ module_function
6
10
 
7
- # extracted from Rack::Utils and reformatted for rubocop
8
- module QueryUtils
9
- module_function
10
- def escape(str)
11
- URI.encode_www_form_component(str)
12
- end
13
- def build_nested_query(value, prefix = nil)
14
- case value
15
- when Array
16
- value.map { |v| build_nested_query(v, "#{prefix}[]") }.join('&')
17
- when Hash
18
- value.map { |k, v| build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) }.delete_if(&:empty?).join('&')
19
- when nil
20
- prefix
21
- else
22
- raise ArgumentError, 'value must be a Hash' if prefix.nil?
23
- "#{prefix}=#{escape(value)}"
11
+ def escape(str)
12
+ URI.encode_www_form_component(str)
13
+ end
14
+
15
+ def build_nested_query(value, prefix = nil)
16
+ case value
17
+ when Array
18
+ value.map { |v| build_nested_query(v, "#{prefix}[]") }.join('&')
19
+ when Hash
20
+ value.map do |k, v|
21
+ build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
22
+ end.delete_if(&:empty?).join('&')
23
+ when nil
24
+ prefix
25
+ else
26
+ raise ArgumentError, 'value must be a Hash' if prefix.nil?
27
+
28
+ "#{prefix}=#{escape(value)}"
29
+ end
24
30
  end
25
31
  end
26
- end
27
32
 
28
- module UseStandaloneExtra
29
- # without any :url var it works exactly as the regular #pagy_url_for;
33
+ # Without any :url var it works exactly as the regular #pagy_url_for;
30
34
  # with a defined :url variable it does not use rack/request
31
- def pagy_url_for(pagy, page, deprecated_url=nil, absolute: nil)
32
- absolute = Pagy.deprecated_arg(:url, deprecated_url, :absolute, absolute) if deprecated_url
33
- pagy, page = Pagy.deprecated_order(pagy, page) if page.is_a?(Pagy)
35
+ def pagy_url_for(pagy, page, absolute: nil)
34
36
  p_vars = pagy.vars
35
- if p_vars[:url]
36
- url_string = p_vars[:url]
37
- params = {}
38
- else
39
- url_string = "#{request.base_url if absolute}#{request.path}"
40
- params = request.GET
41
- end
42
- params = params.merge(p_vars[:params])
37
+ return super unless (url = p_vars[:url])
38
+
39
+ params = p_vars[:params]
43
40
  params[p_vars[:page_param].to_s] = page
44
- params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(UseItemsExtra)
45
- query_string = "?#{QueryUtils.build_nested_query(pagy_get_params(params))}" unless params.empty?
46
- "#{url_string}#{query_string}#{p_vars[:fragment]}"
41
+ params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(ItemsExtra)
42
+ # no Rack required in standalone mode
43
+ query_string = "?#{QueryUtils.build_nested_query(pagy_massage_params(params))}" unless params.empty?
44
+ "#{url}#{query_string}#{p_vars[:fragment]}"
47
45
  end
48
46
  end
47
+ # In ruby 3+ we could just use `UrlHelpers.prepend StandaloneExtra` instead of using the next 2 lines
48
+ Frontend.prepend StandaloneExtra
49
+ Backend.prepend StandaloneExtra
49
50
 
50
- # single line in order to avoid complicating simplecov already tested with other GitHub Actions
51
- Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0') \
52
- && Helpers.prepend(UseStandaloneExtra) \
53
- || ( Frontend.prepend(UseStandaloneExtra); Backend.prepend(UseStandaloneExtra) if defined?(Pagy::Backend::METADATA) ) # rubocop:disable Style/Semicolon
54
-
55
- # defines a dummy #params method if not already defined in the including module
51
+ # Define a dummy params method if it's not already defined in the including module
56
52
  module Backend
57
53
  def self.included(controller)
58
- controller.define_method(:params){{}} unless controller.method_defined?(:params)
54
+ controller.define_method(:params) { {} } unless controller.method_defined?(:params)
59
55
  end
60
56
  end
61
-
62
57
  end
@@ -2,9 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy
5
-
6
- module Frontend
7
-
5
+ module SupportExtra
8
6
  def pagy_prev_url(pagy)
9
7
  pagy_url_for(pagy, pagy.prev) if pagy.prev
10
8
  end
@@ -13,21 +11,25 @@ class Pagy
13
11
  pagy_url_for(pagy, pagy.next) if pagy.next
14
12
  end
15
13
 
16
- def pagy_prev_link(pagy, deprecated_text=nil, deprecated_link_extra=nil, text: pagy_t('pagy.nav.prev'), link_extra: '')
17
- text = Pagy.deprecated_arg(:text, deprecated_text, :text, text) if deprecated_text
18
- link_extra = Pagy.deprecated_arg(:link_extra, deprecated_link_extra, :link_extra, link_extra) if deprecated_link_extra
14
+ def pagy_prev_link(pagy, text: pagy_t('pagy.nav.prev'), link_extra: '')
19
15
  if pagy.prev
20
- %(<span class="page prev"><a href="#{pagy_url_for(pagy, pagy.prev)}" rel="prev" aria-label="previous" #{pagy.vars[:link_extra]} #{link_extra}>#{text}</a></span>)
16
+ %(<span class="page prev"><a href="#{
17
+ pagy_url_for(pagy, pagy.prev)
18
+ }" rel="prev" aria-label="previous" #{
19
+ pagy.vars[:link_extra]
20
+ } #{link_extra}>#{text}</a></span>)
21
21
  else
22
22
  %(<span class="page prev disabled">#{text}</span>)
23
23
  end
24
24
  end
25
25
 
26
- def pagy_next_link(pagy, deprecated_text=nil, deprecated_link_extra=nil, text: pagy_t('pagy.nav.next'), link_extra: '')
27
- text = Pagy.deprecated_arg(:text, deprecated_text, :text, text) if deprecated_text
28
- link_extra = Pagy.deprecated_arg(:link_extra, deprecated_link_extra, :link_extra, link_extra) if deprecated_link_extra
26
+ def pagy_next_link(pagy, text: pagy_t('pagy.nav.next'), link_extra: '')
29
27
  if pagy.next
30
- %(<span class="page next"><a href="#{pagy_url_for(pagy, pagy.next)}" rel="next" aria-label="next" #{pagy.vars[:link_extra]} #{link_extra}>#{text}</a></span>)
28
+ %(<span class="page next"><a href="#{
29
+ pagy_url_for(pagy, pagy.next)
30
+ }" rel="next" aria-label="next" #{
31
+ pagy.vars[:link_extra]
32
+ } #{link_extra}>#{text}</a></span>)
31
33
  else
32
34
  %(<span class="page next disabled">#{text}</span>)
33
35
  end
@@ -40,7 +42,6 @@ class Pagy
40
42
  def pagy_next_link_tag(pagy)
41
43
  %(<link href="#{pagy_url_for(pagy, pagy.next)}" rel="next"/>) if pagy.next
42
44
  end
43
-
44
45
  end
45
-
46
+ Frontend.prepend SupportExtra
46
47
  end
@@ -2,27 +2,26 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy
5
+ DEFAULT[:trim_extra] = true # extra enabled by default
5
6
 
6
- VARS[:enable_trim_extra] = true
7
-
8
- module UseTrimExtra
9
-
10
- def pagy_link_proc(pagy, deprecated_link_extra=nil, link_extra: '')
11
- link_extra = Pagy.deprecated_arg(:link_extra, deprecated_link_extra, :link_extra, link_extra) if deprecated_link_extra
7
+ module TrimExtra
8
+ # Override the original using the pagy_trim method
9
+ def pagy_link_proc(pagy, link_extra: '')
12
10
  link_proc = super(pagy, link_extra: link_extra)
13
- return link_proc unless pagy.vars[:enable_trim_extra]
14
- lambda do |num, text=num, extra=''|
11
+ return link_proc unless pagy.vars[:trim_extra]
12
+
13
+ lambda do |num, text = num, extra = ''|
15
14
  link = +link_proc.call(num, text, extra)
16
15
  return link unless num == 1
16
+
17
17
  pagy_trim(pagy, link)
18
18
  end
19
19
  end
20
20
 
21
+ # Remove the the :page_param param from the first page link
21
22
  def pagy_trim(pagy, link)
22
23
  link.sub!(/[?&]#{pagy.vars[:page_param]}=1\b(?!&)|\b#{pagy.vars[:page_param]}=1&/, '')
23
24
  end
24
-
25
25
  end
26
- Frontend.prepend UseTrimExtra
27
-
26
+ Frontend.prepend TrimExtra
28
27
  end
@@ -4,8 +4,7 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
- module Frontend
8
-
7
+ module UikitExtra
9
8
  # Pagination for uikit: it returns the html with the series of links to the pages
10
9
  def pagy_uikit_nav(pagy, pagy_id: nil, link_extra: '')
11
10
  p_id = %( id="#{pagy_id}") if pagy_id
@@ -13,7 +12,7 @@ class Pagy
13
12
 
14
13
  html = +%(<ul#{p_id} class="pagy-uikit-nav uk-pagination uk-flex-center">#{pagy_uikit_prev_html pagy, link})
15
14
  pagy.series.each do |item|
16
- html << case item
15
+ html << case item
17
16
  when Integer then %(<li>#{link.call item}</li>)
18
17
  when String then %(<li class="uk-active"><span>#{item}</span></li>)
19
18
  when :gap then %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>)
@@ -24,8 +23,7 @@ class Pagy
24
23
  end
25
24
 
26
25
  # Javascript pagination for uikit: it returns a nav and a JSON tag used by the Pagy.nav javascript
27
- def pagy_uikit_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', steps: nil)
28
- pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
26
+ def pagy_uikit_nav_js(pagy, pagy_id: nil, link_extra: '', steps: nil)
29
27
  p_id = %( id="#{pagy_id}") if pagy_id
30
28
  link = pagy_link_proc(pagy, link_extra: link_extra)
31
29
  tags = { 'before' => pagy_uikit_prev_html(pagy, link),
@@ -34,48 +32,49 @@ class Pagy
34
32
  'gap' => %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>),
35
33
  'after' => pagy_uikit_next_html(pagy, link) }
36
34
 
37
- %(<ul#{p_id} class="pagy-njs pagy-uikit-nav-js uk-pagination uk-flex-center" #{pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></ul>)
35
+ %(<ul#{p_id} class="pagy-njs pagy-uikit-nav-js uk-pagination uk-flex-center" #{
36
+ pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></ul>)
38
37
  end
39
38
 
40
39
  # Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
41
- def pagy_uikit_combo_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '')
42
- pagy_id = Pagy.deprecated_arg(:id, deprecated_id, :pagy_id, pagy_id) if deprecated_id
40
+ def pagy_uikit_combo_nav_js(pagy, pagy_id: nil, link_extra: '')
43
41
  p_id = %( id="#{pagy_id}") if pagy_id
44
42
  link = pagy_link_proc(pagy, link_extra: link_extra)
45
43
  p_page = pagy.page
46
44
  p_pages = pagy.pages
47
- input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="text-align: center; width: #{p_pages.to_s.length+1}rem;">)
45
+ input = %(<input type="number" min="1" max="#{p_pages}" value="#{
46
+ p_page}" style="text-align: center; width: #{p_pages.to_s.length + 1}rem;">)
48
47
 
49
48
  %(<ul#{p_id} class="pagy-uikit-combo-nav-js uk-button-group uk-pagination uk-flex-center" #{
50
49
  pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
51
- }>#{
50
+ }>#{
52
51
  pagy_uikit_prev_html pagy, link
53
- }<li>#{
52
+ }<li>#{
54
53
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
55
- }</li>#{
54
+ }</li>#{
56
55
  pagy_uikit_next_html pagy, link
57
- }</ul>)
56
+ }</ul>)
58
57
  end
59
58
 
60
59
  private
61
60
 
62
- def pagy_uikit_prev_html(pagy, link)
63
- previous_span = %(<span uk-pagination-previous>#{pagy_t 'pagy.nav.prev'}</span>)
64
- if (p_prev = pagy.prev)
65
- %(<li>#{link.call p_prev, previous_span}</li>)
66
- else
67
- %(<li class="uk-disabled"><a href="#">#{previous_span}</a></li>)
68
- end
61
+ def pagy_uikit_prev_html(pagy, link)
62
+ previous_span = %(<span uk-pagination-previous>#{pagy_t 'pagy.nav.prev'}</span>)
63
+ if (p_prev = pagy.prev)
64
+ %(<li>#{link.call p_prev, previous_span}</li>)
65
+ else
66
+ %(<li class="uk-disabled"><a href="#">#{previous_span}</a></li>)
69
67
  end
68
+ end
70
69
 
71
- def pagy_uikit_next_html(pagy, link)
72
- next_span = %(<span uk-pagination-next>#{pagy_t 'pagy.nav.next'}</span>)
73
- if (p_next = pagy.next)
74
- %(<li>#{link.call p_next, next_span}</li>)
75
- else
76
- %(<li class="uk-disabled"><a href="#">#{next_span}</a></li>)
77
- end
70
+ def pagy_uikit_next_html(pagy, link)
71
+ next_span = %(<span uk-pagination-next>#{pagy_t 'pagy.nav.next'}</span>)
72
+ if (p_next = pagy.next)
73
+ %(<li>#{link.call p_next, next_span}</li>)
74
+ else
75
+ %(<li class="uk-disabled"><a href="#">#{next_span}</a></li>)
78
76
  end
79
-
77
+ end
80
78
  end
79
+ Frontend.include UikitExtra
81
80
  end
data/lib/pagy/frontend.rb CHANGED
@@ -1,40 +1,17 @@
1
1
  # See Pagy::Frontend API documentation: https://ddnexus.github.io/pagy/api/frontend
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'yaml'
4
+ require 'pagy/url_helpers'
5
+ require 'pagy/i18n'
5
6
 
6
7
  class Pagy
7
-
8
- PAGE_PLACEHOLDER = '__pagy_page__' # string used for search and replace, hardcoded also in the pagy.js file
9
-
10
- # I18n static hash loaded at startup, used as default alternative to the i18n gem.
11
- # see https://ddnexus.github.io/pagy/api/frontend#i18n
12
- I18n = eval Pagy.root.join('locales', 'utils', 'i18n.rb').read #rubocop:disable Security/Eval
13
-
14
- module Helpers
15
- # This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
16
- def pagy_url_for(pagy, page, deprecated_url=nil, absolute: nil)
17
- absolute = Pagy.deprecated_arg(:url, deprecated_url, :absolute, absolute) if deprecated_url
18
- pagy, page = Pagy.deprecated_order(pagy, page) if page.is_a?(Pagy)
19
- p_vars = pagy.vars
20
- params = request.GET.merge(p_vars[:params])
21
- params[p_vars[:page_param].to_s] = page
22
- params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(UseItemsExtra)
23
- query_string = "?#{Rack::Utils.build_nested_query(pagy_get_params(params))}" unless params.empty?
24
- "#{request.base_url if absolute}#{request.path}#{query_string}#{p_vars[:fragment]}"
25
- end
26
-
27
- # Sub-method called only by #pagy_url_for: here for easy customization of params by overriding
28
- def pagy_get_params(params)
29
- params
30
- end
31
- end
8
+ # Used for search and replace, hardcoded also in the pagy.js file
9
+ PAGE_PLACEHOLDER = '__pagy_page__'
32
10
 
33
11
  # All the code here has been optimized for performance: it may not look very pretty
34
12
  # (as most code dealing with many long strings), but its performance makes it very sexy! ;)
35
13
  module Frontend
36
-
37
- include Helpers
14
+ include UrlHelpers
38
15
 
39
16
  # Generic pagination: it returns the html with the series of links to the pages
40
17
  def pagy_nav(pagy, pagy_id: nil, link_extra: '')
@@ -43,13 +20,13 @@ class Pagy
43
20
  p_prev = pagy.prev
44
21
  p_next = pagy.next
45
22
 
46
- html = +%(<nav#{p_id} class="pagy-nav pagination" aria-label="pager">)
23
+ html = +%(<nav#{p_id} class="pagy-nav pagination" aria-label="pager">)
47
24
  html << if p_prev
48
25
  %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
49
26
  else
50
27
  %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> )
51
28
  end
52
- pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
29
+ pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
53
30
  html << case item
54
31
  when Integer then %(<span class="page">#{link.call item}</span> ) # page link
55
32
  when String then %(<span class="page active">#{item}</span> ) # current page
@@ -64,30 +41,29 @@ class Pagy
64
41
  html << %(</nav>)
65
42
  end
66
43
 
67
- # Return examples: "Displaying items 41-60 of 324 in total" of "Displaying Products 41-60 of 324 in total"
68
- def pagy_info(pagy, deprecated_item_name=nil, pagy_id: nil, item_name: nil, i18n_key: nil)
69
- p_id = %( id="#{pagy_id}") if pagy_id
70
- item_name = Pagy.deprecated_arg(:item_name, deprecated_item_name, :item_name, item_name) if deprecated_item_name
44
+ # Return examples: "Displaying items 41-60 of 324 in total" or "Displaying Products 41-60 of 324 in total"
45
+ def pagy_info(pagy, pagy_id: nil, item_name: nil, i18n_key: nil)
46
+ p_id = %( id="#{pagy_id}") if pagy_id
71
47
  p_count = pagy.count
72
48
  key = if p_count.zero? then 'pagy.info.no_items'
73
49
  elsif pagy.pages == 1 then 'pagy.info.single_page'
74
- else 'pagy.info.multiple_pages'
50
+ else 'pagy.info.multiple_pages' # rubocop:disable Lint/ElseLayout
75
51
  end
76
52
 
77
53
  %(<span#{p_id} class="pagy-info">#{
78
- pagy_t key, item_name: item_name || pagy_t(i18n_key || pagy.vars[:i18n_key], count: p_count),
79
- count: p_count, from: pagy.from, to: pagy.to
80
- }</span>)
54
+ pagy_t key, item_name: item_name || pagy_t(i18n_key || pagy.vars[:i18n_key], count: p_count),
55
+ count: p_count, from: pagy.from, to: pagy.to
56
+ }</span>)
81
57
  end
82
58
 
83
- # Returns a performance optimized proc to generate the HTML links
59
+ # Return a performance optimized proc to generate the HTML links
84
60
  # Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails' link_to
85
- def pagy_link_proc(pagy, deprecated_link_extra=nil, link_extra: '')
86
- link_extra = Pagy.deprecated_arg(:link_extra, deprecated_link_extra, :link_extra, link_extra) if deprecated_link_extra
87
- p_prev = pagy.prev
88
- p_next = pagy.next
89
- left, right = %(<a href="#{pagy_url_for pagy, PAGE_PLACEHOLDER}" #{pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2)
90
- lambda do |num, text=num, extra_attrs=''|
61
+ def pagy_link_proc(pagy, link_extra: '')
62
+ p_prev = pagy.prev
63
+ p_next = pagy.next
64
+ left, right = %(<a href="#{pagy_url_for pagy, PAGE_PLACEHOLDER}" #{
65
+ pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2)
66
+ lambda do |num, text = num, extra_attrs = ''|
91
67
  %(#{left}#{num}#{right}#{ case num
92
68
  when p_prev then ' rel="prev"'
93
69
  when p_next then ' rel="next"'
@@ -99,8 +75,7 @@ class Pagy
99
75
  # Similar to I18n.t: just ~18x faster using ~10x less memory
100
76
  # (@pagy_locale explicitly initialized in order to avoid warning)
101
77
  def pagy_t(key, **opts)
102
- Pagy::I18n.t @pagy_locale||=nil, key, **opts
78
+ Pagy::I18n.t(@pagy_locale ||= nil, key, **opts)
103
79
  end
104
-
105
80
  end
106
81
  end