pagy 0.10.1 → 0.11.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: 3a66c2944b27ed7d4ce4f4ab521cd8bc45fc675196ca79bcc352f65e5cb8584e
4
- data.tar.gz: 6a2aee40334e9b80291b74b840776ed4f9697afaa4ce4701c61ade1fa8b35349
3
+ metadata.gz: 844ca7b25affe1bb80fd78bc75aa6d91c55000134aecfea338288d57b9587231
4
+ data.tar.gz: dc9e704d31c3fb376d23d560725c921484ef4ea037524a8bf86c67246069f62f
5
5
  SHA512:
6
- metadata.gz: a6bfadbdf1ff037faf2e1bc29e01c4f3fe488983c3c0eb2da2cfb474b3cd38025704773d38ad903c436f97a2e810e99537134e6ab6660257311df5133d3d176c
7
- data.tar.gz: 1a9949404e19dddb77f790147f3cf732ccbfbaf1d20083b963a164344f4154aacf40d4f2f0632284109c17830c20d99c92767a473f649a502a2f0771dcbca509
6
+ metadata.gz: 7fe1bc2404142977f00b1285081a6fc39c9b1be5be6813efa8271cebb2b7e1e58349264458cfd5874afca86bb4d0c3e693b3dc1721d0ceccbdfb0d569a5f4dc2
7
+ data.tar.gz: 2d066a55b2103ff8c96baadab07730413692f8233d4e28eeecbef1fe1fc04770b47b48aa92b3fa4212f6e82652ab72bed47822f3babbb77a8726868cbb6ab064
@@ -22,3 +22,5 @@ en:
22
22
  items:
23
23
  show: "Show"
24
24
  items: "items per page"
25
+
26
+ # PR for other languages are very welcome. Thanks!
@@ -0,0 +1,25 @@
1
+ # This file adds support for multiple built-in plualiztion types.
2
+ # It defines the pluralization procs and gets eval(ed) at I18N.load time
3
+ # frozen_string_literal: true
4
+
5
+ # utility variables
6
+ zero_one = ['zero', 'one']
7
+
8
+ # Rules
9
+ # A rule is a proc returning a plural type string based on the passed count
10
+ # Each plural rule may apply to many language/rule pairs below
11
+ rules = {
12
+ zero_one_other: -> (count) {zero_one[count] || 'other'}
13
+ }
14
+
15
+ # Plurals (language/rule pairs)
16
+ # The default rule for missing languages is the :zero_one_other rule (used for English)
17
+ plurals = Hash.new(rules[:zero_one_other])
18
+
19
+ # Entries for all the languages defined in the pagy.yml dictionary
20
+ plurals['en'] = rules[:zero_one_other]
21
+
22
+ # PR for other languages are very welcome. Thanks!
23
+
24
+ # Return plurals
25
+ plurals
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'pathname'
5
5
 
6
- class Pagy ; VERSION = '0.10.1'
6
+ class Pagy ; VERSION = '0.11.0'
7
7
 
8
8
  class OutOfRangeError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end
9
9
 
@@ -7,21 +7,20 @@ class Pagy
7
7
 
8
8
  # Pagination for bootstrap: it returns the html with the series of links to the pages
9
9
  def pagy_nav_bootstrap(pagy)
10
- tags, link, p_prev, p_next = +'', pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
10
+ html, link, p_prev, p_next = +'', pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
11
11
 
12
- tags << (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
12
+ html << (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
13
13
  : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
14
14
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
15
- tags << if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
15
+ html << if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
16
16
  elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
17
17
  elsif item == :gap ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
18
18
  end
19
19
  end
20
- tags << (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
20
+ html << (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
21
21
  : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
22
- %(<nav class="pagy-nav-boostrap pagination" role="navigation" aria-label="pager"><ul class="pagination">#{tags}</ul></nav>)
22
+ %(<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
23
23
  end
24
24
 
25
25
  end
26
26
  end
27
-
@@ -6,38 +6,37 @@ class Pagy
6
6
  module Frontend
7
7
 
8
8
  # Generic compact pagination: it returns the html with the series of links to the pages
9
- # we use a numeric input tag to set the page and the PagyCompact javascript to navigate
9
+ # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
10
10
  def pagy_nav_compact(pagy, id=caller(1,1)[0].hash)
11
- tags, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
11
+ html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
12
12
 
13
- tags << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact pagination" role="navigation" aria-label="pager">)
14
- tags << link.call(MARKER, '', %(style="display: none;" ))
15
- tags << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
13
+ html << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact pagination" role="navigation" aria-label="pager">)
14
+ html << link.call(MARKER, '', %(style="display: none;" ))
15
+ html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
16
16
  : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
17
17
  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;">)
18
- tags << %(<span class="pagy-compact-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.compact.page')} #{input} #{pagy_t('pagy.compact.of')} #{p_pages}</span> )
19
- tags << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
18
+ html << %(<span class="pagy-compact-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.compact.page')} #{input} #{pagy_t('pagy.compact.of')} #{p_pages}</span> )
19
+ html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
20
20
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
21
- tags << %(</nav><script type="application/json" class="pagy-compact">["#{id}", "#{MARKER}", "#{p_page}"]</script>)
21
+ html << %(</nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}"]</script>)
22
22
  end
23
23
 
24
24
  # Compact pagination for bootstrap: it returns the html with the series of links to the pages
25
- # we use a numeric input tag to set the page and the PagyCompact javascript to navigate
26
- def pagy_nav_bootstrap_compact(pagy, id=caller(1,1)[0].hash)
27
- tags, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
25
+ # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
26
+ def pagy_nav_compact_bootstrap(pagy, id=caller(1,1)[0].hash)
27
+ html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
28
28
 
29
- tags << %(<nav id="pagy-nav-#{id}" class="pagy-nav-bootstrap-compact pagination" role="navigation" aria-label="pager">)
30
- tags << link.call(MARKER, '', %(style="display: none;" ))
31
- tags << %(<div class="btn-group" role="group">)
32
- tags << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
29
+ html << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-bootstrap pagination" role="navigation" aria-label="pager">)
30
+ html << link.call(MARKER, '', %(style="display: none;" ))
31
+ html << %(<div class="btn-group" role="group">)
32
+ html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
33
33
  : %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
34
34
  input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
35
- tags << %(<div class="pagy-compact-input btn btn-primary disabled">#{pagy_t('pagy.compact.page')} #{input} #{pagy_t('pagy.compact.of')} #{p_pages}</div>)
36
- tags << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
35
+ html << %(<div class="pagy-compact-input btn btn-primary disabled">#{pagy_t('pagy.compact.page')} #{input} #{pagy_t('pagy.compact.of')} #{p_pages}</div>)
36
+ html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
37
37
  : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
38
- tags << %(</div></nav><script type="application/json" class="pagy-compact">["#{id}", "#{MARKER}", "#{p_page}"]</script>)
38
+ html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}"]</script>)
39
39
  end
40
40
 
41
41
  end
42
42
  end
43
-
@@ -8,9 +8,8 @@ class Pagy
8
8
 
9
9
  # Override the built-in pagy_t
10
10
  def pagy_t(*args)
11
- I18n.t(*args)
11
+ ::I18n.t(*args)
12
12
  end
13
13
 
14
14
  end
15
15
  end
16
-
@@ -22,21 +22,21 @@ class Pagy
22
22
 
23
23
  module Frontend
24
24
 
25
- # this works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
25
+ # This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
26
26
  def pagy_url_for(page, pagy)
27
27
  p_vars = pagy.vars; params = request.GET.merge(p_vars[:page_param] => page, p_vars[:items_param] => p_vars[:items], **p_vars[:params])
28
28
  "#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
29
29
  end
30
30
 
31
- # return the items selector HTML. For example "Show [20] items per page"
31
+ # Return the items selector HTML. For example "Show [20] items per page"
32
32
  def pagy_items_selector(pagy, id=caller(1,1)[0].hash)
33
33
  pagy = pagy.clone; p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-"
34
34
 
35
- tags = +%(<span id="pagy-items-#{id}">)
36
- tags << %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
35
+ html = +%(<span id="pagy-items-#{id}">)
36
+ html << %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
37
37
  input = %(<input type="number" min="1" max="#{p_vars[:max_items]}" value="#{p_items}" style="padding: 0; text-align: center; width: #{p_items.to_s.length+1}rem;">)
38
- tags << %(#{pagy_t('pagy.items.show')} #{input} #{pagy_t('pagy.items.items')})
39
- tags << %(</span><script type="application/json" class="pagy-items">["#{id}", "#{MARKER}", #{pagy.from}]</script>)
38
+ html << %(#{pagy_t('pagy.items.show')} #{input} #{pagy_t('pagy.items.items')})
39
+ html << %(</span><script type="application/json" class="pagy-items-json">["#{id}", "#{MARKER}", #{pagy.from}]</script>)
40
40
  end
41
41
 
42
42
  end
@@ -1,84 +1,72 @@
1
- // See the Pagy documentation: https://ddnexus.github.io/pagy/extras
1
+ // See the Pagy documentation: https://ddnexus.github.io/pagy/extras#javascript
2
2
 
3
3
  function Pagy(){}
4
4
 
5
+ Pagy.addInputEventListeners = function(input, handler){
6
+ // select the content on click: easier for typing a number
7
+ input.addEventListener('click', function(){ this.select() });
8
+ // go when the input looses focus
9
+ input.addEventListener('focusout', handler);
10
+ // … and when pressing enter inside the input
11
+ input.addEventListener('keyup', function(e){ if (e.which === 13) handler() }.bind(this));
12
+ };
13
+
5
14
  Pagy.compact = function(id, marker, page){
6
15
  var pagyNav = document.getElementById('pagy-nav-'+id),
7
16
  input = pagyNav.getElementsByTagName('input')[0],
8
- link = pagyNav.getElementsByTagName('a')[0];
9
-
10
- var go = function(){
11
- if (page !== input.value) {
12
- var href = link.getAttribute('href').replace(marker, input.value);
13
- link.setAttribute('href', href);
14
- link.click();
15
- }
16
- };
17
-
18
- // select the content on click: easier for typing a number
19
- input.addEventListener('click', function(){ this.select() });
20
- // go when the input looses focus
21
- input.addEventListener('focusout', go);
22
- // … and when pressing enter inside the input
23
- input.addEventListener('keyup', function(e){ if (e.which === 13) go() }.bind(this));
24
-
25
- };
26
-
17
+ link = pagyNav.getElementsByTagName('a')[0],
18
+ go = function(){
19
+ if (page !== input.value) {
20
+ var href = link.getAttribute('href').replace(marker, input.value);
21
+ link.setAttribute('href', href);
22
+ link.click();
23
+ }
24
+ };
25
+ Pagy.addInputEventListeners(input, go);
26
+ };
27
27
 
28
28
  Pagy.items = function(id, marker, from){
29
29
  var pagyNav = document.getElementById('pagy-items-'+id),
30
30
  input = pagyNav.getElementsByTagName('input')[0],
31
31
  current = input.value,
32
- link = pagyNav.getElementsByTagName('a')[0];
33
-
34
- var go = function(){
35
- var items = input.value;
36
- if (current !== items) {
37
- var page = Math.max(Math.ceil(from / items),1);
38
- var href = link.getAttribute('href').replace(marker+'-page-', page).replace(marker+'-items-', items);
39
- link.setAttribute('href', href);
40
- link.click();
41
- }
42
- };
43
-
44
- // select the content on click: easier for typing a number
45
- input.addEventListener('click', function(){ this.select() });
46
- // go when the input looses focus
47
- input.addEventListener('focusout', go);
48
- // … and when pressing enter inside the input
49
- input.addEventListener('keyup', function(e){ if (e.which === 13) go() }.bind(this));
50
-
32
+ link = pagyNav.getElementsByTagName('a')[0],
33
+ go = function(){
34
+ var items = input.value;
35
+ if (current !== items) {
36
+ var page = Math.max(Math.ceil(from / items),1);
37
+ var href = link.getAttribute('href').replace(marker+'-page-', page).replace(marker+'-items-', items);
38
+ link.setAttribute('href', href);
39
+ link.click();
40
+ }
41
+ };
42
+ Pagy.addInputEventListeners(input, go);
51
43
  };
52
44
 
53
- Pagy.responsive = function(id, items, widths, series){
45
+ Pagy.responsive = function(id, tags, widths, series){
54
46
  var pagyNav = document.getElementById('pagy-nav-'+id),
55
47
  pagyBox = pagyNav.firstChild || pagyNav,
56
48
  pagyParent = pagyNav.parentElement,
57
- lastWidth = undefined;
58
-
59
- var render = function(){
60
- var parentWidth = parseInt(pagyParent.clientWidth),
61
- width = widths.find(function(w){return parentWidth > w});
62
- if (width !== lastWidth) {
63
- while (pagyBox.firstChild) { pagyBox.removeChild(pagyBox.firstChild) }
64
- var tags = items['prev'];
65
- series[width].forEach(function(item){tags += items[item]});
66
- tags += items['next'];
67
- pagyBox.insertAdjacentHTML('beforeend', tags);
68
- lastWidth = width;
69
- }
70
- };
71
-
49
+ lastWidth = undefined,
50
+ render = function(){
51
+ var parentWidth = parseInt(pagyParent.clientWidth),
52
+ width = widths.find(function(w){return parentWidth > w});
53
+ if (width !== lastWidth) {
54
+ while (pagyBox.firstChild) { pagyBox.removeChild(pagyBox.firstChild) }
55
+ var html = tags['prev'];
56
+ series[width].forEach(function(item){html += tags[item]});
57
+ html += tags['next'];
58
+ pagyBox.insertAdjacentHTML('beforeend', html);
59
+ lastWidth = width;
60
+ }
61
+ };
72
62
  if (window.attachEvent) { window.attachEvent('onresize', render) }
73
63
  else if (window.addEventListener) { window.addEventListener('resize', render, true) }
74
-
75
64
  render();
76
65
  };
77
66
 
78
-
79
67
  Pagy.init = function(){
80
68
  ['compact', 'items', 'responsive'].forEach(function(name){
81
- Array.from(document.getElementsByClassName("pagy-"+name)).forEach(function(json) {
69
+ Array.from(document.getElementsByClassName("pagy-"+name+"-json")).forEach(function(json) {
82
70
  Pagy[name].apply(null, JSON.parse(json.innerHTML))
83
71
  })
84
72
  })
@@ -22,7 +22,7 @@ class Pagy
22
22
  # :widths is the desc-ordered array of widths (passed to the PagyResponsive javascript function)
23
23
  def responsive
24
24
  @responsive ||= {items: [], series: {}, widths:[]}.tap do |r|
25
- @vars[:breakpoints].key?(0) || raise(ArgumentError, "expected :breakpoints to contain the 0 size; got #{@vars[:breakpoint].inspect}")
25
+ @vars[:breakpoints].key?(0) || raise(ArgumentError, "expected :breakpoints to contain the 0 size; got #{@vars[:breakpoints].inspect}")
26
26
  @vars[:breakpoints].each {|width, size| r[:items] |= r[:series][width] = series(size)}
27
27
  r[:widths] = r[:series].keys.sort!{|a,b| b <=> a}
28
28
  end
@@ -32,41 +32,41 @@ class Pagy
32
32
  module Frontend
33
33
 
34
34
  # Generic responsive pagination: it returns the html with the series of links to the pages
35
- # we build the tags as a json object string and render them with the PagyResponsive javascript
35
+ # rendered by the Pagy.responsive javascript
36
36
  def pagy_nav_responsive(pagy, id=caller(1,1)[0].hash)
37
37
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
38
38
 
39
- tags[:prev] = (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
40
- : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
39
+ tags['prev'] = (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
40
+ : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
41
41
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
42
42
  tags[item.to_s] = if item.is_a?(Integer); %(<span class="page">#{link.call item}</span> ) # page link
43
43
  elsif item.is_a?(String) ; %(<span class="page active">#{item}</span> ) # current page
44
44
  elsif item == :gap ; %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap
45
45
  end
46
46
  end
47
- tags[:next] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
48
- : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
49
- script = %(<script type="application/json" class="pagy-responsive">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
47
+ tags['next'] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
48
+ : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
49
+ script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
50
50
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive pagination" role="navigation" aria-label="pager"></nav>#{script})
51
51
  end
52
52
 
53
53
  # Responsive pagination for bootstrap: it returns the html with the series of links to the pages
54
- # we build the tags as a json object string and render them with the PagyResponsive javascript
55
- def pagy_nav_bootstrap_responsive(pagy, id=caller(1,1)[0].hash)
54
+ # rendered by the Pagy.responsive javascript
55
+ def pagy_nav_responsive_bootstrap(pagy, id=caller(1,1)[0].hash)
56
56
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next, pagy.responsive
57
57
 
58
58
  tags['prev'] = (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
59
59
  : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
60
60
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
61
- tags[item.to_s] = if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
62
- elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
61
+ tags[item.to_s] = if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
62
+ elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
63
63
  elsif item == :gap ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
64
64
  end
65
65
  end
66
66
  tags['next'] = (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
67
67
  : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
68
- script = %(<script type="application/json" class="pagy-responsive">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
69
- %(<nav id="pagy-nav-#{id}" class="pagy-nav-bootstrap-responsive pagination" role="navigation" aria-label="pager"><ul class="pagination"></ul></nav>#{script})
68
+ script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json}, #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
69
+ %(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive-bootstrap pagination" role="navigation" aria-label="pager"><ul class="pagination"></ul></nav>#{script})
70
70
  end
71
71
 
72
72
  end
@@ -11,19 +11,19 @@ class Pagy
11
11
 
12
12
  # Generic pagination: it returns the html with the series of links to the pages
13
13
  def pagy_nav(pagy)
14
- tags, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next
14
+ html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next
15
15
 
16
- tags << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
16
+ html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
17
17
  : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
18
18
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
19
- tags << if item.is_a?(Integer); %(<span class="page">#{link.call item}</span> ) # page link
19
+ html << if item.is_a?(Integer); %(<span class="page">#{link.call item}</span> ) # page link
20
20
  elsif item.is_a?(String) ; %(<span class="page active">#{item}</span> ) # current page
21
21
  elsif item == :gap ; %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap
22
22
  end
23
23
  end
24
- tags << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
24
+ html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
25
25
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
26
- %(<nav class="pagy-nav pagination" role="navigation" aria-label="pager">#{tags}</nav>)
26
+ %(<nav class="pagy-nav pagination" role="navigation" aria-label="pager">#{html}</nav>)
27
27
  end
28
28
 
29
29
  # Return examples: "Displaying items 41-60 of 324 in total" or "Displaying Products 41-60 of 324 in total"
@@ -33,7 +33,7 @@ class Pagy
33
33
  pagy_t(path, item_name: name, count: pagy.count, from: pagy.from, to: pagy.to)
34
34
  end
35
35
 
36
- # this works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
36
+ # This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
37
37
  def pagy_url_for(page, pagy)
38
38
  p_vars = pagy.vars; params = request.GET.merge(p_vars[:page_param] => page, **p_vars[:params])
39
39
  "#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
@@ -54,19 +54,20 @@ class Pagy
54
54
  else '' end } #{extra}>#{text}</a>" }
55
55
  end
56
56
 
57
- # Pagy::Frontend::I18N constant
58
- I18N_DATA = YAML.load_file(Pagy.root.join('locales', 'pagy.yml')).first[1]
59
- zero_one = ['zero', 'one']; I18N = { plurals: -> (c) {zero_one[c] || 'other'}}
60
- def I18N.load_file(file) I18N_DATA.replace(YAML.load_file(file).first[1]) end
57
+ # Pagy::Frontend::I18N
58
+ def (I18N = {data:{}}).load(file:Pagy.root.join('locales', 'pagy.yml'), language: 'en')
59
+ self[:data] = YAML.load_file(file)[language]
60
+ self[:plural] = eval(Pagy.root.join('locales', 'plurals.rb').read)[language] #rubocop:disable Security/Eval
61
+ end; I18N.load
61
62
 
62
- # Similar to I18n.t for interpolation and pluralization but without translation
63
- # Use only for single-language apps: it is specialized for Pagy and 5x faster than I18n.t
64
- # See also https://ddnexus.github.io/pagy/extras/i18n to use the standard I18n gem instead
63
+ # Similar to I18n.t for streamlined interpolation and pluralization but without dynamic translation.
64
+ # It is specialized for Pagy and 5x faster than I18n.t (see https://ddnexus.github.io/pagy/api/frontend#i18n)
65
+ # See also https://ddnexus.github.io/pagy/extras/i18n if you need to use the standard I18n gem instead
65
66
  def pagy_t(path, vars={})
66
- value = I18N_DATA.dig(*path.to_s.split('.')) or return %(translation missing: "#{path}")
67
+ value = I18N[:data].dig(*path.split('.')) or return %(translation missing: "#{path}")
67
68
  if value.is_a?(Hash)
68
69
  vars.key?(:count) or return value
69
- plural = I18N[:plurals].call(vars[:count])
70
+ plural = I18N[:plural].call(vars[:count])
70
71
  value.key?(plural) or return %(invalid pluralization data: "#{path}" cannot be used with count: #{vars[:count]}; key "#{plural}" is missing.)
71
72
  value = value[plural] or return %(translation missing: "#{path}")
72
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domizio Demichelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-23 00:00:00.000000000 Z
11
+ date: 2018-06-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Agnostic pagination in plain ruby: it works with any framework, ORM
14
14
  and DB type, with all kinds of collections, even pre-paginated, scopes, Arrays,
@@ -22,6 +22,7 @@ extra_rdoc_files: []
22
22
  files:
23
23
  - LICENSE.txt
24
24
  - lib/locales/pagy.yml
25
+ - lib/locales/plurals.rb
25
26
  - lib/pagy.rb
26
27
  - lib/pagy/backend.rb
27
28
  - lib/pagy/extras/array.rb