pagy 4.11.0 → 5.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/pagy.rb +71 -45
  3. data/lib/javascripts/pagy.js +15 -6
  4. data/lib/pagy/backend.rb +9 -12
  5. data/lib/pagy/console.rb +6 -4
  6. data/lib/pagy/countless.rb +21 -23
  7. data/lib/pagy/exceptions.rb +16 -16
  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 +35 -29
  11. data/lib/pagy/extras/bulma.rb +43 -32
  12. data/lib/pagy/extras/countless.rb +12 -16
  13. data/lib/pagy/extras/elasticsearch_rails.rb +64 -47
  14. data/lib/pagy/extras/foundation.rb +29 -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 +38 -39
  19. data/lib/pagy/extras/materialize.rb +31 -30
  20. data/lib/pagy/extras/meilisearch.rb +50 -45
  21. data/lib/pagy/extras/metadata.rb +14 -20
  22. data/lib/pagy/extras/navs.rb +26 -26
  23. data/lib/pagy/extras/overflow.rb +56 -60
  24. data/lib/pagy/extras/searchkick.rb +51 -45
  25. data/lib/pagy/extras/semantic.rb +31 -30
  26. data/lib/pagy/extras/shared.rb +43 -40
  27. data/lib/pagy/extras/standalone.rb +39 -43
  28. data/lib/pagy/extras/support.rb +20 -13
  29. data/lib/pagy/extras/trim.rb +11 -11
  30. data/lib/pagy/extras/uikit.rb +30 -28
  31. data/lib/pagy/frontend.rb +25 -49
  32. data/lib/pagy/i18n.rb +159 -0
  33. data/lib/pagy/url_helpers.rb +24 -0
  34. data/lib/pagy.rb +54 -29
  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,9 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
- module Frontend
8
-
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
9
+ module SemanticExtra
9
10
  # Pagination for semantic: it returns the html with the series of links to the pages
10
11
  def pagy_semantic_nav(pagy, pagy_id: nil, link_extra: '')
11
12
  p_id = %( id="#{pagy_id}") if pagy_id
@@ -13,11 +14,12 @@ class Pagy
13
14
 
14
15
  html = +%(<div#{p_id} class="pagy-semantic-nav ui pagination menu">)
15
16
  html << pagy_semantic_prev_html(pagy, link)
16
- pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
+ pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
18
  html << case item
18
19
  when Integer then link.call item # page link
19
20
  when String then %(<a class="item active">#{item}</a>) # current page
20
21
  when :gap then %(<div class="disabled item">#{pagy_t 'pagy.nav.gap'}</div>) # page gap
22
+ else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
21
23
  end
22
24
  end
23
25
  html << pagy_semantic_next_html(pagy, link)
@@ -25,8 +27,7 @@ class Pagy
25
27
  end
26
28
 
27
29
  # 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
30
+ def pagy_semantic_nav_js(pagy, pagy_id: nil, link_extra: '', steps: nil)
30
31
  p_id = %( id="#{pagy_id}") if pagy_id
31
32
  link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra}))
32
33
  tags = { 'before' => pagy_semantic_prev_html(pagy, link),
@@ -35,46 +36,46 @@ class Pagy
35
36
  'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
36
37
  'after' => pagy_semantic_next_html(pagy, link) }
37
38
 
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>)
39
+ %(<div#{p_id} class="pagy-njs pagy-semantic-nav-js ui pagination menu" role="navigation" #{
40
+ pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
39
41
  end
40
42
 
41
43
  # 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
44
+ def pagy_semantic_combo_nav_js(pagy, pagy_id: nil, link_extra: '')
44
45
  p_id = %( id="#{pagy_id}") if pagy_id
45
46
  link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra}))
46
47
  p_page = pagy.page
47
48
  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">)
49
+ input = %(<input type="number" min="1" max="#{p_pages}" value="#{
50
+ p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length + 1}rem; margin: 0 0.3rem">)
49
51
 
50
52
  %(<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>)
53
+ pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)}>#{
54
+ pagy_semantic_prev_html pagy, link
55
+ }<div class="pagy-combo-input item">#{
56
+ pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
57
+ }</div> #{
58
+ pagy_semantic_next_html pagy, link
59
+ }</div>)
59
60
  end
60
61
 
61
62
  private
62
63
 
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
64
+ def pagy_semantic_prev_html(pagy, link)
65
+ if (p_prev = pagy.prev)
66
+ link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'
67
+ else
68
+ +%(<div class="item disabled"><i class="left small chevron icon"></i></div>)
69
69
  end
70
+ end
70
71
 
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
72
+ def pagy_semantic_next_html(pagy, link)
73
+ if (p_next = pagy.next)
74
+ link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'
75
+ else
76
+ +%(<div class="item disabled"><i class="right small chevron icon"></i></div>)
77
77
  end
78
-
78
+ end
79
79
  end
80
+ Frontend.prepend SemanticExtra
80
81
  end
@@ -3,50 +3,53 @@
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, :steps, 'to define the 0 width', steps) unless steps.key?(0)
23
+
24
+ {}.tap do |sequels|
25
+ steps.each { |width, size| sequels[width.to_s] = series(size) }
26
+ end
27
+ end
25
28
  end
26
- end
27
-
28
- module Frontend
29
29
 
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;')}")
30
+ # Additions for the Frontend
31
+ module Frontend
32
+ if defined?(Oj)
33
+ # Return a script tag with the JSON-serialized args generated with the faster oj gem
34
+ def pagy_json_attr(pagy, *args)
35
+ args << pagy.vars[:page_param] if pagy.vars[:trim_extra]
36
+ %(data-pagy-json="#{Oj.dump(args, mode: :strict).gsub('"', '&quot;')}")
37
+ end
38
+ else
39
+ require 'json'
40
+ # Return a script tag with the JSON-serialized args generated with the slower to_json
41
+ def pagy_json_attr(pagy, *args)
42
+ args << pagy.vars[:page_param] if pagy.vars[:trim_extra]
43
+ %(data-pagy-json="#{args.to_json.gsub('"', '&quot;')}")
44
+ end
35
45
  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
46
 
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;"'
47
+ # Return the marked link to used by pagy.js
48
+ def pagy_marked_link(link)
49
+ link.call PAGE_PLACEHOLDER, '', 'style="display: none;"'
50
+ end
48
51
  end
49
-
50
52
  end
51
-
53
+ prepend SharedExtra::Pagy
54
+ Frontend.prepend SharedExtra::Frontend
52
55
  end
@@ -3,60 +3,56 @@
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;
30
- # 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)
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.
36
+ def pagy_url_for(pagy, page, absolute: nil)
34
37
  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])
38
+ return super unless (url = p_vars[:url])
39
+
40
+ params = p_vars[:params]
43
41
  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]}"
42
+ params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(ItemsExtra)
43
+
44
+ query_string = "?#{QueryUtils.build_nested_query(pagy_massage_params(params))}"
45
+ "#{url}#{query_string}#{p_vars[:fragment]}"
47
46
  end
48
47
  end
48
+ # In ruby 3+ `UrlHelpers.prepend StandaloneExtra` would be enough instead of using the next 2 lines
49
+ Frontend.prepend StandaloneExtra
50
+ Backend.prepend StandaloneExtra
49
51
 
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
52
+ # Define a dummy params method if it's not already defined in the including module
56
53
  module Backend
57
54
  def self.included(controller)
58
- controller.define_method(:params){{}} unless controller.method_defined?(:params)
55
+ controller.define_method(:params) { {} } unless controller.method_defined?(:params)
59
56
  end
60
57
  end
61
-
62
58
  end
@@ -2,45 +2,52 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Pagy
5
-
6
- module Frontend
7
-
5
+ module SupportExtra
6
+ # Return the previous page URL string or nil
8
7
  def pagy_prev_url(pagy)
9
8
  pagy_url_for(pagy, pagy.prev) if pagy.prev
10
9
  end
11
10
 
11
+ # Return the next page URL string or nil
12
12
  def pagy_next_url(pagy)
13
13
  pagy_url_for(pagy, pagy.next) if pagy.next
14
14
  end
15
15
 
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
16
+ # Return the HTML string for the previous page link
17
+ def pagy_prev_link(pagy, text: pagy_t('pagy.nav.prev'), link_extra: '')
19
18
  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>)
19
+ %(<span class="page prev"><a href="#{
20
+ pagy_url_for(pagy, pagy.prev)
21
+ }" rel="prev" aria-label="previous" #{
22
+ pagy.vars[:link_extra]
23
+ } #{link_extra}>#{text}</a></span>)
21
24
  else
22
25
  %(<span class="page prev disabled">#{text}</span>)
23
26
  end
24
27
  end
25
28
 
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
29
+ # Return the HTML string for the next page link
30
+ def pagy_next_link(pagy, text: pagy_t('pagy.nav.next'), link_extra: '')
29
31
  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>)
32
+ %(<span class="page next"><a href="#{
33
+ pagy_url_for(pagy, pagy.next)
34
+ }" rel="next" aria-label="next" #{
35
+ pagy.vars[:link_extra]
36
+ } #{link_extra}>#{text}</a></span>)
31
37
  else
32
38
  %(<span class="page next disabled">#{text}</span>)
33
39
  end
34
40
  end
35
41
 
42
+ # Return the HTML link tag for the previous page or nil
36
43
  def pagy_prev_link_tag(pagy)
37
44
  %(<link href="#{pagy_url_for(pagy, pagy.prev)}" rel="prev"/>) if pagy.prev
38
45
  end
39
46
 
47
+ # Return the HTML link tag for the next page or nil
40
48
  def pagy_next_link_tag(pagy)
41
49
  %(<link href="#{pagy_url_for(pagy, pagy.next)}" rel="next"/>) if pagy.next
42
50
  end
43
-
44
51
  end
45
-
52
+ Frontend.prepend SupportExtra
46
53
  end
@@ -2,27 +2,27 @@
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 pagy_link_proc.
9
+ # Call the pagy_trim method if the trim_extra is enabled.
10
+ def pagy_link_proc(pagy, link_extra: '')
12
11
  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=''|
12
+ return link_proc unless pagy.vars[:trim_extra]
13
+
14
+ lambda do |num, text = num, extra = ''|
15
15
  link = +link_proc.call(num, text, extra)
16
16
  return link unless num == 1
17
+
17
18
  pagy_trim(pagy, link)
18
19
  end
19
20
  end
20
21
 
22
+ # Remove the the :page_param param from the first page link
21
23
  def pagy_trim(pagy, link)
22
24
  link.sub!(/[?&]#{pagy.vars[:page_param]}=1\b(?!&)|\b#{pagy.vars[:page_param]}=1&/, '')
23
25
  end
24
-
25
26
  end
26
- Frontend.prepend UseTrimExtra
27
-
27
+ Frontend.prepend TrimExtra
28
28
  end
@@ -4,8 +4,9 @@
4
4
  require 'pagy/extras/shared'
5
5
 
6
6
  class Pagy
7
- module Frontend
8
-
7
+ # Frontend modules are specially optimized for performance.
8
+ # The resulting code may not look very elegant, but produces the best benchmarks
9
+ module UikitExtra
9
10
  # Pagination for uikit: it returns the html with the series of links to the pages
10
11
  def pagy_uikit_nav(pagy, pagy_id: nil, link_extra: '')
11
12
  p_id = %( id="#{pagy_id}") if pagy_id
@@ -13,10 +14,11 @@ class Pagy
13
14
 
14
15
  html = +%(<ul#{p_id} class="pagy-uikit-nav uk-pagination uk-flex-center">#{pagy_uikit_prev_html pagy, link})
15
16
  pagy.series.each do |item|
16
- html << case item
17
+ html << case item
17
18
  when Integer then %(<li>#{link.call item}</li>)
18
19
  when String then %(<li class="uk-active"><span>#{item}</span></li>)
19
20
  when :gap then %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>)
21
+ else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
20
22
  end
21
23
  end
22
24
  html << pagy_uikit_next_html(pagy, link)
@@ -24,8 +26,7 @@ class Pagy
24
26
  end
25
27
 
26
28
  # 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
29
+ def pagy_uikit_nav_js(pagy, pagy_id: nil, link_extra: '', steps: nil)
29
30
  p_id = %( id="#{pagy_id}") if pagy_id
30
31
  link = pagy_link_proc(pagy, link_extra: link_extra)
31
32
  tags = { 'before' => pagy_uikit_prev_html(pagy, link),
@@ -34,48 +35,49 @@ class Pagy
34
35
  'gap' => %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>),
35
36
  'after' => pagy_uikit_next_html(pagy, link) }
36
37
 
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>)
38
+ %(<ul#{p_id} class="pagy-njs pagy-uikit-nav-js uk-pagination uk-flex-center" #{
39
+ pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></ul>)
38
40
  end
39
41
 
40
42
  # 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
43
+ def pagy_uikit_combo_nav_js(pagy, pagy_id: nil, link_extra: '')
43
44
  p_id = %( id="#{pagy_id}") if pagy_id
44
45
  link = pagy_link_proc(pagy, link_extra: link_extra)
45
46
  p_page = pagy.page
46
47
  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;">)
48
+ input = %(<input type="number" min="1" max="#{p_pages}" value="#{
49
+ p_page}" style="text-align: center; width: #{p_pages.to_s.length + 1}rem;">)
48
50
 
49
51
  %(<ul#{p_id} class="pagy-uikit-combo-nav-js uk-button-group uk-pagination uk-flex-center" #{
50
52
  pagy_json_attr pagy, :combo_nav, p_page, pagy_marked_link(link)
51
- }>#{
53
+ }>#{
52
54
  pagy_uikit_prev_html pagy, link
53
- }<li>#{
55
+ }<li>#{
54
56
  pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages
55
- }</li>#{
57
+ }</li>#{
56
58
  pagy_uikit_next_html pagy, link
57
- }</ul>)
59
+ }</ul>)
58
60
  end
59
61
 
60
62
  private
61
63
 
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
64
+ def pagy_uikit_prev_html(pagy, link)
65
+ previous_span = %(<span uk-pagination-previous>#{pagy_t 'pagy.nav.prev'}</span>)
66
+ if (p_prev = pagy.prev)
67
+ %(<li>#{link.call p_prev, previous_span}</li>)
68
+ else
69
+ %(<li class="uk-disabled"><a href="#">#{previous_span}</a></li>)
69
70
  end
71
+ end
70
72
 
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
73
+ def pagy_uikit_next_html(pagy, link)
74
+ next_span = %(<span uk-pagination-next>#{pagy_t 'pagy.nav.next'}</span>)
75
+ if (p_next = pagy.next)
76
+ %(<li>#{link.call p_next, next_span}</li>)
77
+ else
78
+ %(<li class="uk-disabled"><a href="#">#{next_span}</a></li>)
78
79
  end
79
-
80
+ end
80
81
  end
82
+ Frontend.include UikitExtra
81
83
  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
8
+ # Used for search and replace, hardcoded also in the pagy.js file
9
+ PAGE_PLACEHOLDER = '__pagy_page__'
7
10
 
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
32
-
33
- # All the code here has been optimized for performance: it may not look very pretty
34
- # (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
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,17 +20,18 @@ 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
56
33
  when :gap then %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap
34
+ else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
57
35
  end
58
36
  end
59
37
  html << if p_next
@@ -64,30 +42,29 @@ class Pagy
64
42
  html << %(</nav>)
65
43
  end
66
44
 
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
45
+ # Return examples: "Displaying items 41-60 of 324 in total" or "Displaying Products 41-60 of 324 in total"
46
+ def pagy_info(pagy, pagy_id: nil, item_name: nil, i18n_key: nil)
47
+ p_id = %( id="#{pagy_id}") if pagy_id
71
48
  p_count = pagy.count
72
49
  key = if p_count.zero? then 'pagy.info.no_items'
73
50
  elsif pagy.pages == 1 then 'pagy.info.single_page'
74
- else 'pagy.info.multiple_pages'
51
+ else 'pagy.info.multiple_pages' # rubocop:disable Lint/ElseLayout
75
52
  end
76
53
 
77
54
  %(<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>)
55
+ pagy_t key, item_name: item_name || pagy_t(i18n_key || pagy.vars[:i18n_key], count: p_count),
56
+ count: p_count, from: pagy.from, to: pagy.to
57
+ }</span>)
81
58
  end
82
59
 
83
- # Returns a performance optimized proc to generate the HTML links
60
+ # Return a performance optimized proc to generate the HTML links
84
61
  # 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=''|
62
+ def pagy_link_proc(pagy, link_extra: '')
63
+ p_prev = pagy.prev
64
+ p_next = pagy.next
65
+ left, right = %(<a href="#{pagy_url_for pagy, PAGE_PLACEHOLDER}" #{
66
+ pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2)
67
+ lambda do |num, text = num, extra_attrs = ''|
91
68
  %(#{left}#{num}#{right}#{ case num
92
69
  when p_prev then ' rel="prev"'
93
70
  when p_next then ' rel="next"'
@@ -99,8 +76,7 @@ class Pagy
99
76
  # Similar to I18n.t: just ~18x faster using ~10x less memory
100
77
  # (@pagy_locale explicitly initialized in order to avoid warning)
101
78
  def pagy_t(key, **opts)
102
- Pagy::I18n.t @pagy_locale||=nil, key, **opts
79
+ Pagy::I18n.t(@pagy_locale ||= nil, key, **opts)
103
80
  end
104
-
105
81
  end
106
82
  end