pagy 5.2.3 → 5.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 500b8bfdb84150bd2ec01036eb55b53eeadcb1b0ab1fdc0721cee5fdf7fed46e
4
- data.tar.gz: 9a75dbbf42e34e1ef12106e4a567acb408d22600fdb34fd42137a4b0dd61c8f3
3
+ metadata.gz: b456869f0028668579c9839d1083b7baf7693d593fcbed24f371bc52dfff4467
4
+ data.tar.gz: ec4f4b71da52860950262812d2bfbbe9190b1f0a1c3836cbc8f316e0c04ecd0b
5
5
  SHA512:
6
- metadata.gz: 7ee826ffb154387f67275568fef53b557e6b16879017a0658701999bfcb34934322a1c645aa26afc599fa7fc2fbc69743878582d8d5f0d21054609610d49e578
7
- data.tar.gz: c0334539b13212ac107aaabab36d2a00460682693e75bab9519b320b2446942a8340f191542cab531ef0de6f9af2df3baf9d74a77b2f2c7154178954428b9616
6
+ metadata.gz: e16ea640c3cc34b506c02d585c48fa8365b1a5e67d3b487d69669aa9d700a1f930929844e92759929f3963e78f2d72c53b4ef2d4caefeb2adbbff262ff0dd54d
7
+ data.tar.gz: 3086cf995b60b71212b64b42d7998e2f2831883abf41d22f9952b0632143936871a4c50d16da6b1849e805ce90de190be555e96ba893b5ee28c01b8650af7e03
data/lib/config/pagy.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Pagy initializer file (5.2.3)
3
+ # Pagy initializer file (5.3.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
 
@@ -22,6 +22,7 @@
22
22
  # See https://ddnexus.github.io/pagy/api/pagy#other-variables
23
23
  # Pagy::DEFAULT[:size] = [1,4,4,1] # default
24
24
  # Pagy::DEFAULT[:page_param] = :page # default
25
+ # The :params can be also set as a lambda e.g ->(params){ params.exclude('useless').merge!('custom' => 'useful') }
25
26
  # Pagy::DEFAULT[:params] = {} # default
26
27
  # Pagy::DEFAULT[:fragment] = '#fragment' # example
27
28
  # Pagy::DEFAULT[:link_extra] = 'data-remote="true"' # example
@@ -1,9 +1,11 @@
1
1
  // See the Pagy documentation: https://ddnexus.github.io/pagy/extras#javascript
2
2
 
3
+ // This code should be OK also with very old browsers
4
+
3
5
  // Container of the whole pagy stuff
4
6
  function Pagy(){}
5
7
 
6
- Pagy.version = '5.2.3'
8
+ Pagy.version = '5.3.0'
7
9
 
8
10
  // Used by the waitForMe function
9
11
  Pagy.delay = 100
@@ -23,10 +25,11 @@ Pagy.init =
23
25
 
24
26
  // Power the pagy*_nav_js helpers
25
27
  Pagy.nav =
26
- function(pagyEl, tags, sequels, trimParam) {
27
- var lastWidth,
28
- pageREg = new RegExp(/__pagy_page__/g),
29
- widths = []
28
+ function(pagyEl, tags, sequels, label_sequels, trimParam ) {
29
+ label_sequels = (label_sequels === null) ? sequels : label_sequels
30
+ var widths = [], lastWidth,
31
+ fill = function(string, item, label) { return string.replace(/__pagy_page__/g, item)
32
+ .replace(/__pagy_label__/g, label) }
30
33
  for (var width in sequels) {
31
34
  if (sequels.hasOwnProperty(width)) { widths.push(parseInt(width, 10)) }
32
35
  }
@@ -40,13 +43,15 @@ Pagy.nav =
40
43
  }
41
44
  if (width !== lastWidth) {
42
45
  var html = tags.before,
43
- series = sequels[width]
46
+ series = sequels[width],
47
+ labels = label_sequels[width]
44
48
  for (i = 0, len = series.length; i < len; i++) {
45
- var item = series[i]
46
- if (typeof(trimParam) === 'string' && item === 1) { html += Pagy.trim(tags.link.replace(pageREg, item), trimParam) }
47
- else if (typeof(item) === 'number') { html += tags.link.replace(pageREg, item) }
49
+ var item = series[i],
50
+ label = labels[i]
51
+ if (typeof(trimParam) === 'string' && item === 1) { html += Pagy.trim(fill(tags.link, item, label), trimParam) }
52
+ else if (typeof(item) === 'number') { html += fill(tags.link, item, label) }
48
53
  else if (item === 'gap') { html += tags.gap }
49
- else if (typeof(item) === 'string') { html += tags.active.replace(pageREg, item) }
54
+ else if (typeof(item) === 'string') { html += fill(tags.active, item, label) }
50
55
  }
51
56
  html += tags.after
52
57
  this.insertAdjacentHTML('afterbegin', html)
data/lib/pagy/calendar.rb CHANGED
@@ -24,6 +24,7 @@ class Pagy # :nodoc:
24
24
  normalize_vars(vars)
25
25
  setup_vars(page: 1, week_offset: 0)
26
26
  setup_unit_vars
27
+ setup_params_var
27
28
  raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
28
29
 
29
30
  @prev = (@page - 1 unless @page == 1)
@@ -31,20 +32,24 @@ class Pagy # :nodoc:
31
32
  end
32
33
 
33
34
  # Generate a label for each page, with the specific `Time` period it refers to
34
- def page_label(num = @page, format = nil)
35
- snap = snap(num.to_i)
36
- format ||= @vars[:"#{@unit}_format"]
37
- case @unit
38
- when :year then new_time(@initial.year + snap)
39
- when :month then bump_month(@initial, snap)
40
- when :week then @initial + (snap * WEEK)
41
- when :day then @initial + (snap * DAY)
42
- else raise InternalError, "expected @unit to be in [:year, :month, :week, :day]; got #{@unit.inspect}"
43
- end.strftime(format)
35
+ # It can pass along the I18n gem opts when it's used with the i18n extra
36
+ def label_for(page, **opts)
37
+ snap = snap(page.to_i)
38
+ time = case @unit
39
+ when :year then new_time(@initial.year + snap)
40
+ when :month then bump_month(@initial, snap)
41
+ when :week then @initial + (snap * WEEK)
42
+ when :day then @initial + (snap * DAY)
43
+ else raise InternalError, "expected @unit to be in [:year, :month, :week, :day]; got #{@unit.inspect}"
44
+ end
45
+ opts[:format] ||= @vars[:"#{@unit}_format"]
46
+ strftime(time, **opts)
44
47
  end
45
48
 
46
- def current_page_label(format = nil)
47
- page_label(@page, format)
49
+ # The label for the current page
50
+ # It can pass along the I18n gem opts when it's used with the i18n extra
51
+ def label(**opts)
52
+ label_for(@page, **opts)
48
53
  end
49
54
 
50
55
  DAY = 60 * 60 * 24
@@ -113,6 +118,11 @@ class Pagy # :nodoc:
113
118
  @utc_to = @utc_from + DAY
114
119
  end
115
120
 
121
+ # The last method in the labelling chain: it is overridden by the I18n when localization is required.
122
+ def strftime(time, **opts)
123
+ time.strftime(opts[:format])
124
+ end
125
+
116
126
  private
117
127
 
118
128
  # Simple trick to snap the page into its ordered position,
@@ -11,6 +11,7 @@ class Pagy
11
11
  normalize_vars(vars)
12
12
  setup_vars(page: 1, outset: 0)
13
13
  setup_items_var
14
+ setup_params_var
14
15
  @offset = (@items * (@page - 1)) + @outset
15
16
  end
16
17
 
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bootstrap
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -34,13 +34,13 @@ class Pagy # :nodoc:
34
34
  p_id = %( id="#{pagy_id}") if pagy_id
35
35
  link = pagy_link_proc(pagy, link_extra: %(class="page-link" #{link_extra}))
36
36
  tags = { 'before' => %(<ul class="pagination">#{pagy_bootstrap_prev_html pagy, link}),
37
- 'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
37
+ 'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}</li>),
38
38
  'active' => %(<li class="page-item active">#{mark}</li>),
39
39
  'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>),
40
40
  'after' => %(#{pagy_bootstrap_next_html pagy, link}</ul>) }
41
41
 
42
42
  %(<nav#{p_id} class="pagy-njs pagy-bootstrap-nav-js" aria-label="pager" #{
43
- pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
43
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></nav>)
44
44
  end
45
45
 
46
46
  # Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -18,9 +18,9 @@ class Pagy # :nodoc:
18
18
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
19
19
  html << case item
20
20
  when Integer
21
- %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}")}</li>)
21
+ %(<li>#{link.call item, pagy.label_for(item), %(class="pagination-link" aria-label="goto page #{item}")}</li>)
22
22
  when String
23
- %(<li>#{link.call item, item,
23
+ %(<li>#{link.call item, pagy.label_for(item),
24
24
  %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>)
25
25
  when :gap
26
26
  %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>)
@@ -34,17 +34,16 @@ class Pagy # :nodoc:
34
34
  p_id = %( id="#{pagy_id}") if pagy_id
35
35
  link = pagy_link_proc(pagy, link_extra: link_extra)
36
36
  tags = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
37
- 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER,
37
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, LABEL_PLACEHOLDER,
38
38
  %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>),
39
- 'active' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER,
39
+ 'active' => %(<li>#{link.call PAGE_PLACEHOLDER, LABEL_PLACEHOLDER,
40
40
  %(class="pagination-link is-current" aria-current="page" aria-label="page #{
41
41
  PAGE_PLACEHOLDER}")}</li>),
42
42
  'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>),
43
43
  'after' => '</ul>' }
44
44
 
45
- %(<nav#{p_id} class="pagy-njs pagy-bulma-nav-js pagination is-centered" aria-label="pagination" #{pagy_json_attr(
46
- pagy, :nav, tags, pagy.sequels(steps)
47
- )}></nav>)
45
+ %(<nav#{p_id} class="pagy-njs pagy-bulma-nav-js pagination is-centered" aria-label="pagination" #{
46
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></nav>)
48
47
  end
49
48
 
50
49
  # Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -32,15 +32,6 @@ class Pagy # :nodoc:
32
32
  'the items for the unit by selecting the records with Time from pagy.from to pagy.to'
33
33
  end
34
34
  end
35
-
36
- # Additions for the Frontend module
37
- module Frontend
38
- # Change the text shown in the nav bar links to the actual unit of each page.
39
- def pagy_labeler(pagy, num)
40
- pagy.is_a?(Calendar) ? pagy.page_label(num) : num
41
- end
42
- end
43
35
  end
44
36
  Backend.prepend CalendarExtra::Backend
45
- Frontend.prepend CalendarExtra::Frontend
46
37
  end
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/foundation
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -17,7 +17,7 @@ class Pagy # :nodoc:
17
17
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
18
18
  html << case item
19
19
  when Integer then %(<li>#{link.call item}</li>) # page link
20
- when String then %(<li class="current">#{pagy_labeler(pagy, item)}</li>) # active page
20
+ when String then %(<li class="current">#{pagy.label_for(item)}</li>) # active page
21
21
  when :gap then %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
22
22
  else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
23
23
  end
@@ -31,13 +31,13 @@ class Pagy # :nodoc:
31
31
  p_id = %( id="#{pagy_id}") if pagy_id
32
32
  link = pagy_link_proc(pagy, link_extra: link_extra)
33
33
  tags = { 'before' => %(<ul class="pagination">#{pagy_foundation_prev_html pagy, link}),
34
- 'link' => %(<li>#{link.call PAGE_PLACEHOLDER}</li>),
35
- 'active' => %(<li class="current">#{pagy.page}</li>),
34
+ 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}</li>),
35
+ 'active' => %(<li class="current">#{LABEL_PLACEHOLDER}</li>),
36
36
  'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>),
37
37
  'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) }
38
38
 
39
39
  %(<nav#{p_id} class="pagy-njs pagy-foundation-nav-js" aria-label="Pagination" #{
40
- pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
40
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></nav>)
41
41
  end
42
42
 
43
43
  # Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -5,7 +5,8 @@ require 'digest'
5
5
  class Pagy # :nodoc:
6
6
  DEFAULT[:steps] = false # default false will use {0 => @vars[:size]}
7
7
 
8
- module SharedExtra
8
+ # Private module documented in the main classes
9
+ module FrontendHelpers
9
10
  # Additions for the Pagy class
10
11
  module Pagy
11
12
  # `Pagy` instance method used by the `pagy*_nav_js` helpers.
@@ -25,6 +26,20 @@ class Pagy # :nodoc:
25
26
  steps.each { |width, size| sequels[width.to_s] = series(size) }
26
27
  end
27
28
  end
29
+
30
+ # Support for the Calendar API
31
+ def label_sequels(*); end
32
+ end
33
+
34
+ # Additions for Calendar class
35
+ module Calendar
36
+ def label_sequels(sequels = self.sequels)
37
+ {}.tap do |label_sequels|
38
+ sequels.each do |width, series|
39
+ label_sequels[width] = series.map { |item| item == :gap ? :gap : label_for(item) }
40
+ end
41
+ end
42
+ end
28
43
  end
29
44
 
30
45
  # Additions for the Frontend
@@ -50,6 +65,7 @@ class Pagy # :nodoc:
50
65
  end
51
66
  end
52
67
  end
53
- prepend SharedExtra::Pagy
54
- Frontend.prepend SharedExtra::Frontend
68
+ prepend FrontendHelpers::Pagy
69
+ Calendar.prepend FrontendHelpers::Calendar if defined?(Calendar)
70
+ Frontend.prepend FrontendHelpers::Frontend
55
71
  end
@@ -4,11 +4,22 @@
4
4
  class Pagy # :nodoc:
5
5
  # Use ::I18n gem
6
6
  module I18nExtra
7
- def pagy_t(key, **opts)
8
- ::I18n.t(key, **opts)
7
+ # Frontend overriding
8
+ module Frontend
9
+ def pagy_t(key, **opts)
10
+ ::I18n.t(key, **opts)
11
+ end
12
+ end
13
+
14
+ # Calendar overriding
15
+ module Calendar
16
+ def strftime(time, **opts)
17
+ ::I18n.l(time, **opts)
18
+ end
9
19
  end
10
20
  end
11
- Frontend.prepend I18nExtra
21
+ Frontend.prepend I18nExtra::Frontend
22
+ Calendar.prepend I18nExtra::Calendar if defined?(Calendar)
12
23
 
13
24
  # Add the pagy locales to the I18n.load_path
14
25
  ::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/items
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  DEFAULT[:items_param] = :items
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/materialize
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -32,13 +32,13 @@ class Pagy # :nodoc:
32
32
  link = pagy_link_proc(pagy, link_extra: link_extra)
33
33
 
34
34
  tags = { 'before' => %(<ul class="pagination">#{pagy_materialize_prev_html pagy, link}),
35
- 'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
35
+ 'link' => %(<li class="waves-effect">#{mark = link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}</li>),
36
36
  'active' => %(<li class="active">#{mark}</li>),
37
37
  'gap' => %(<li class="gap disabled"><a href="#">#{pagy_t 'pagy.nav.gap'}</a></li>),
38
38
  'after' => %(#{pagy_materialize_next_html pagy, link}</ul>) }
39
39
 
40
40
  %(<div#{p_id} class="pagy-njs pagy-materialize-nav-js" role="navigation" aria-label="pager" #{
41
- pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></div>)
41
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></div>)
42
42
  end
43
43
 
44
44
  # Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/navs
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -12,13 +12,13 @@ class Pagy # :nodoc:
12
12
  p_id = %( id="#{pagy_id}") if pagy_id
13
13
  link = pagy_link_proc(pagy, link_extra: link_extra)
14
14
  tags = { 'before' => pagy_nav_prev_html(pagy, link),
15
- 'link' => %(<span class="page">#{link.call(PAGE_PLACEHOLDER)}</span> ),
16
- 'active' => %(<span class="page active">#{pagy.page}</span> ),
15
+ 'link' => %(<span class="page">#{link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}</span> ),
16
+ 'active' => %(<span class="page active">#{LABEL_PLACEHOLDER}</span> ),
17
17
  'gap' => %(<span class="page gap">#{pagy_t 'pagy.nav.gap'}</span> ),
18
18
  'after' => pagy_nav_next_html(pagy, link) }
19
19
 
20
20
  %(<nav#{p_id} class="pagy-njs pagy-nav-js pagination" aria-label="pager" #{
21
- pagy_json_attr(pagy, :nav, tags, pagy.sequels(steps))}></nav>)
21
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></nav>)
22
22
  end
23
23
 
24
24
  # Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -63,7 +63,7 @@ class Pagy # :nodoc:
63
63
  when :exception
64
64
  raise # same as without the extra
65
65
  when :empty_page
66
- @offset = @in = @from = @to = 0 # vars relative to the actual page
66
+ @offset = @items = @from = @to = 0 # vars relative to the actual page
67
67
  @vars[:size] = [] # no page in the series
68
68
  self
69
69
  else
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/semantic
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -17,7 +17,7 @@ class Pagy # :nodoc:
17
17
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
18
18
  html << case item
19
19
  when Integer then link.call item
20
- when String then %(<a class="item active">#{pagy_labeler(pagy, item)}</a>)
20
+ when String then %(<a class="item active">#{pagy.label_for(item)}</a>)
21
21
  when :gap then %(<div class="disabled item">#{pagy_t 'pagy.nav.gap'}</div>)
22
22
  else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
23
23
  end
@@ -31,13 +31,13 @@ class Pagy # :nodoc:
31
31
  p_id = %( id="#{pagy_id}") if pagy_id
32
32
  link = pagy_link_proc(pagy, link_extra: %(class="item" #{link_extra}))
33
33
  tags = { 'before' => pagy_semantic_prev_html(pagy, link),
34
- 'link' => link.call(PAGE_PLACEHOLDER),
35
- 'active' => %(<a class="item active">#{pagy.page}</a>),
34
+ 'link' => link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER),
35
+ 'active' => %(<a class="item active">#{LABEL_PLACEHOLDER}</a>),
36
36
  'gap' => %(<div class="disabled item">#{pagy_t('pagy.nav.gap')}</div>),
37
37
  'after' => pagy_semantic_next_html(pagy, link) }
38
38
 
39
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>)
40
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></div>)
41
41
  end
42
42
 
43
43
  # Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
@@ -37,15 +37,18 @@ class Pagy # :nodoc:
37
37
  # it works exactly as the regular #pagy_url_for, relying on the params method and Rack.
38
38
  # If there is a defined pagy.vars[:url] variable it does not need the params method nor Rack.
39
39
  def pagy_url_for(pagy, page, absolute: nil)
40
- vars = pagy.vars
41
- return super unless (url = vars[:url])
40
+ return super unless pagy.vars[:url]
42
41
 
43
- params = vars[:params].clone # safe when it gets reused
44
- params[vars[:page_param]] = page
45
- params[vars[:items_param]] = vars[:items] if vars[:items_extra]
46
-
47
- query_string = "?#{QueryUtils.build_nested_query(pagy_massage_params(params))}"
48
- "#{url}#{query_string}#{vars[:fragment]}"
42
+ vars = pagy.vars
43
+ page_param = vars[:page_param].to_s
44
+ items_param = vars[:items_param].to_s
45
+ params = pagy.params.is_a?(Hash) ? pagy.params.clone : {} # safe when it gets reused
46
+ params[page_param] = page
47
+ params[items_param] = vars[:items] if vars[:items_extra]
48
+ query_string = "?#{QueryUtils.build_nested_query(pagy_deprecated_params(pagy, params))}" # remove in 6.0
49
+ # params = pagy.params.call(params) if pagy.params.is_a?(Proc) # add in 6.0
50
+ # query_string = "?#{Rack::Utils.build_nested_query(params)}" # add in 6.0
51
+ "#{vars[:url]}#{query_string}#{vars[:fragment]}"
49
52
  end
50
53
  end
51
54
  # In ruby 3+ `UrlHelpers.prepend StandaloneExtra` would be enough instead of using the next 2 lines
@@ -12,9 +12,9 @@ class Pagy # :nodoc:
12
12
  link_proc = super(pagy, link_extra: link_extra)
13
13
  return link_proc unless pagy.vars[:trim_extra]
14
14
 
15
- lambda do |num, text = num, extra = ''|
16
- link = +link_proc.call(num, text, extra)
17
- return link unless num == 1
15
+ lambda do |page, text = pagy.label_for(page), extra = ''|
16
+ link = +link_proc.call(page, text, extra)
17
+ return link unless page == 1
18
18
 
19
19
  pagy_trim(pagy, link)
20
20
  end
@@ -1,7 +1,7 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/uikit
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pagy/extras/shared'
4
+ require 'pagy/extras/frontend_helpers'
5
5
 
6
6
  class Pagy # :nodoc:
7
7
  # Frontend modules are specially optimized for performance.
@@ -16,7 +16,7 @@ class Pagy # :nodoc:
16
16
  pagy.series.each do |item|
17
17
  html << case item
18
18
  when Integer then %(<li>#{link.call item}</li>)
19
- when String then %(<li class="uk-active"><span>#{pagy_labeler(pagy, item)}</span></li>)
19
+ when String then %(<li class="uk-active"><span>#{pagy.label_for(item)}</span></li>)
20
20
  when :gap then %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>)
21
21
  else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
22
22
  end
@@ -30,13 +30,13 @@ class Pagy # :nodoc:
30
30
  p_id = %( id="#{pagy_id}") if pagy_id
31
31
  link = pagy_link_proc(pagy, link_extra: link_extra)
32
32
  tags = { 'before' => pagy_uikit_prev_html(pagy, link),
33
- 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>),
34
- 'active' => %(<li class="uk-active"><span>#{PAGE_PLACEHOLDER}</span></li>),
33
+ 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER, LABEL_PLACEHOLDER)}</li>),
34
+ 'active' => %(<li class="uk-active"><span>#{LABEL_PLACEHOLDER}</span></li>),
35
35
  'gap' => %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>),
36
36
  'after' => pagy_uikit_next_html(pagy, link) }
37
37
 
38
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>)
39
+ pagy_json_attr(pagy, :nav, tags, (sequels = pagy.sequels(steps)), pagy.label_sequels(sequels))}></ul>)
40
40
  end
41
41
 
42
42
  # Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
data/lib/pagy/frontend.rb CHANGED
@@ -6,7 +6,8 @@ require 'pagy/i18n'
6
6
 
7
7
  class Pagy
8
8
  # Used for search and replace, hardcoded also in the pagy.js file
9
- PAGE_PLACEHOLDER = '__pagy_page__'
9
+ PAGE_PLACEHOLDER = '__pagy_page__'
10
+ LABEL_PLACEHOLDER = '__pagy_label__'
10
11
 
11
12
  # Frontend modules are specially optimized for performance.
12
13
  # The resulting code may not look very elegant, but produces the best benchmarks
@@ -29,7 +30,7 @@ class Pagy
29
30
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
30
31
  html << case item
31
32
  when Integer then %(<span class="page">#{link.call item}</span> )
32
- when String then %(<span class="page active">#{pagy_labeler(pagy, item)}</span> )
33
+ when String then %(<span class="page active">#{pagy.label_for(item)}</span> )
33
34
  when :gap then %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> )
34
35
  else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}"
35
36
  end
@@ -64,20 +65,15 @@ class Pagy
64
65
  p_next = pagy.next
65
66
  left, right = %(<a href="#{pagy_url_for pagy, PAGE_PLACEHOLDER}" #{
66
67
  pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2)
67
- lambda do |num, text = pagy_labeler(pagy, num), extra_attrs = ''|
68
- %(#{left}#{num}#{right}#{ case num
69
- when p_prev then ' rel="prev"'
70
- when p_next then ' rel="next"'
71
- else ''
72
- end } #{extra_attrs}>#{text}</a>)
68
+ lambda do |page, text = pagy.label_for(page), extra_attrs = ''|
69
+ %(#{left}#{page}#{right}#{ case page
70
+ when p_prev then ' rel="prev"'
71
+ when p_next then ' rel="next"'
72
+ else ''
73
+ end } #{extra_attrs}>#{text}</a>)
73
74
  end
74
75
  end
75
76
 
76
- # Allow customization of the output by overriding (used by the calendar extra)
77
- def pagy_labeler(_pagy, num)
78
- num
79
- end
80
-
81
77
  # Similar to I18n.t: just ~18x faster using ~10x less memory
82
78
  # (@pagy_locale explicitly initialized in order to avoid warning)
83
79
  def pagy_t(key, **opts)
@@ -7,18 +7,32 @@ class Pagy
7
7
  # It supports all Rack-based frameworks (Sinatra, Padrino, Rails, ...).
8
8
  # For non-rack environments you can use the standalone extra
9
9
  def pagy_url_for(pagy, page, absolute: nil)
10
- vars = pagy.vars
11
- params = request.GET.merge(vars[:params].transform_keys(&:to_s))
12
- params[vars[:page_param].to_s] = page
13
- params[vars[:items_param].to_s] = vars[:items] if vars[:items_extra]
14
-
15
- query_string = "?#{Rack::Utils.build_nested_query(pagy_massage_params(params))}"
10
+ vars = pagy.vars
11
+ page_param = vars[:page_param].to_s
12
+ items_param = vars[:items_param].to_s
13
+ params = pagy.params.is_a?(Hash) ? pagy.params.transform_keys(&:to_s) : {}
14
+ params = request.GET.merge(params)
15
+ params[page_param] = page
16
+ params[items_param] = vars[:items] if vars[:items_extra]
17
+ query_string = "?#{Rack::Utils.build_nested_query(pagy_deprecated_params(pagy, params))}" # remove in 6.0
18
+ # params = pagy.params.call(params) if pagy.params.is_a?(Proc) # add in 6.0
19
+ # query_string = "?#{Rack::Utils.build_nested_query(params)}" # add in 6.0
16
20
  "#{request.base_url if absolute}#{request.path}#{query_string}#{vars[:fragment]}"
17
21
  end
18
22
 
19
- # Sub-method called only by #pagy_url_for: here for easy customization of params by overriding
20
- def pagy_massage_params(params)
21
- params
23
+ private
24
+
25
+ # Transitional code to handle params deprecations. It will be removed in version 6.0
26
+ def pagy_deprecated_params(pagy, params) # remove in 6.0
27
+ if pagy.params.is_a?(Proc) # new code
28
+ pagy.params.call(params)
29
+ elsif respond_to?(:pagy_massage_params) # deprecated code
30
+ Warning.warn '[PAGY WARNING] The pagy_massage_params method has been deprecated and it will be ignored from version 6. ' \
31
+ 'Set the :params variable to a Proc with the same code as the pagy_massage_params method.'
32
+ pagy_massage_params(params)
33
+ else
34
+ params # no massage params
35
+ end
22
36
  end
23
37
  end
24
38
  end
data/lib/pagy.rb CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  # Core class
7
7
  class Pagy
8
- VERSION = '5.2.3'
8
+ VERSION = '5.3.0'
9
9
 
10
10
  # Root pathname to get the path of Pagy files like templates or dictionaries
11
11
  def self.root
@@ -24,7 +24,7 @@ class Pagy
24
24
  i18n_key: 'pagy.item_name',
25
25
  cycle: false }
26
26
 
27
- attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :in, :from, :to, :prev, :next
27
+ attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :in, :from, :to, :prev, :next, :params
28
28
 
29
29
  # Merge and validate the options, do some simple arithmetic and set the instance variables
30
30
  def initialize(vars)
@@ -32,6 +32,7 @@ class Pagy
32
32
  setup_vars(count: 0, page: 1, outset: 0)
33
33
  setup_items_var
34
34
  setup_pages_var
35
+ setup_params_var
35
36
  raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last
36
37
 
37
38
  @offset = (@items * (@page - 1)) + @outset
@@ -70,6 +71,15 @@ class Pagy
70
71
  series
71
72
  end
72
73
 
74
+ # Allow the customization of the output (overridden by the calendar extra)
75
+ def label_for(page)
76
+ page.to_s
77
+ end
78
+
79
+ def label
80
+ @page.to_s
81
+ end
82
+
73
83
  protected
74
84
 
75
85
  # Apply defaults, cleanup blanks and set @vars
@@ -94,6 +104,12 @@ class Pagy
94
104
  def setup_pages_var
95
105
  @pages = @last = [(@count.to_f / @items).ceil, 1].max
96
106
  end
107
+
108
+ # Setup and validates the params
109
+ def setup_params_var
110
+ raise VariableError.new(self, :params, 'must be a Hash or a Proc', @params) \
111
+ unless (@params = @vars[:params]).is_a?(Hash) || @params.is_a?(Proc)
112
+ end
97
113
  end
98
114
 
99
115
  require 'pagy/backend'
@@ -13,7 +13,7 @@
13
13
  <% end -%>
14
14
  <% pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] -%>
15
15
  <% if item.is_a?(Integer) -%> <li><%== link.call(item) %></li>
16
- <% elsif item.is_a?(String) -%> <li class="current"><%= pagy_labeler(pagy, item) %></li>
16
+ <% elsif item.is_a?(String) -%> <li class="current"><%= pagy.label_for(item) %></li>
17
17
  <% elsif item == :gap -%> <li class="ellipsis gap" aria-hidden="true"></li>
18
18
  <% end -%>
19
19
  <% end -%>
@@ -22,7 +22,7 @@
22
22
 
23
23
  - elsif item.is_a?(String) # current page
24
24
  %li.current
25
- = pagy_labeler(pagy, item)
25
+ = pagy.label_for(item)
26
26
 
27
27
  - elsif item == :gap # page gap
28
28
  %li.ellipsis.gap{"aria-hidden" => true}
@@ -22,7 +22,7 @@ nav.pagy-foundation-nav role="navigation" aria-label="Pagination"
22
22
 
23
23
  - elsif item.is_a?(String) # current page
24
24
  li.current
25
- = pagy_labeler(pagy, item)
25
+ = pagy.label_for(item)
26
26
 
27
27
  - elsif item == :gap # page gap
28
28
  li.ellipsis.gap aria-hidden="true"
@@ -5,7 +5,7 @@
5
5
  <% end -%>
6
6
  <% pagy.series.each do |item| -%>
7
7
  <% if item.is_a?(Integer) -%> <li><%== link.call(item) %></li>
8
- <% elsif item.is_a?(String) -%> <li class="uk-active"><span><%== pagy_labeler(pagy, item) %></span></li>
8
+ <% elsif item.is_a?(String) -%> <li class="uk-active"><span><%== pagy.label_for(item) %></span></li>
9
9
  <% elsif item == :gap -%> <li class="uk-disabled"><span><%== pagy_t('pagy.nav.gap') %></span></li>
10
10
  <% end -%>
11
11
  <% end -%>
@@ -14,7 +14,7 @@
14
14
 
15
15
  - elsif item.is_a?(String)
16
16
  %li.uk-active
17
- %span!= pagy_labeler(pagy, item)
17
+ %span!= pagy.label_for(item)
18
18
 
19
19
  - elsif item == :gap
20
20
  %li.uk-disabled
@@ -14,7 +14,7 @@ ul.uk-pagination.uk-flex-center
14
14
 
15
15
  - elsif item.is_a?(String)
16
16
  li.uk-active
17
- span== pagy_labeler(pagy, item)
17
+ span== pagy.label_for(item)
18
18
 
19
19
  - elsif item == :gap
20
20
  li.uk-disabled
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagy
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.3
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
@@ -65,6 +65,7 @@ files:
65
65
  - lib/pagy/extras/countless.rb
66
66
  - lib/pagy/extras/elasticsearch_rails.rb
67
67
  - lib/pagy/extras/foundation.rb
68
+ - lib/pagy/extras/frontend_helpers.rb
68
69
  - lib/pagy/extras/gearbox.rb
69
70
  - lib/pagy/extras/headers.rb
70
71
  - lib/pagy/extras/i18n.rb
@@ -76,7 +77,6 @@ files:
76
77
  - lib/pagy/extras/overflow.rb
77
78
  - lib/pagy/extras/searchkick.rb
78
79
  - lib/pagy/extras/semantic.rb
79
- - lib/pagy/extras/shared.rb
80
80
  - lib/pagy/extras/standalone.rb
81
81
  - lib/pagy/extras/support.rb
82
82
  - lib/pagy/extras/trim.rb