pagy 2.1.5 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,42 +1,45 @@
1
1
  # encoding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'json'
5
4
  require 'digest'
6
5
 
7
6
  class Pagy
8
7
 
9
- # Default :breakpoints
10
- VARS[:breakpoints] = { 0 => [1,4,4,1] }
11
-
12
- # Helper for building the page_nav with javascript. For example:
13
- # with an object like:
14
- # Pagy.new count:1000, page: 20, breakpoints: {0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]}
15
- # it returns something like:
16
- # { :items => [1, :gap, 18, 19, "20", 21, 22, 50, 2, 17, 23, 49, 3, 16, 24, 48],
17
- # :series => { 0 => [1, :gap, 18, 19, "20", 21, 22, :gap, 50],
18
- # 350 => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
19
- # 550 => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] },
20
- # :widths => [550, 350, 0] }
21
- # where :items is the unordered array union of all the page numbers for all sizes (passed to the PagyResponsive javascript function)
22
- # :series is the hash of the series keyed by width (used by the *_responsive helpers to create the JSON string)
23
- # :widths is the desc-ordered array of widths (passed to the PagyResponsive javascript function)
24
- def responsive
25
- @responsive ||= {items: [], series: {}, widths:[]}.tap do |r|
26
- @vars[:breakpoints].key?(0) || raise(ArgumentError, "expected :breakpoints to contain the 0 size; got #{@vars[:breakpoints].inspect}")
27
- @vars[:breakpoints].each {|width, size| r[:items] |= r[:series][width] = series(size)}
28
- r[:widths] = r[:series].keys.sort!{|a,b| b <=> a}
29
- end
8
+ # default :steps: false will use {0 => @vars[:size]}
9
+ VARS[:steps] = false
10
+
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
21
+ steps = @vars[:steps] || {0 => @vars[:size]}
22
+ steps.key?(0) or raise(ArgumentError, "expected :steps to define the 0 width; got #{steps.inspect}")
23
+ sequels = {}; steps.each {|width, size| sequels[width.to_s] = series(size)}; sequels
30
24
  end
31
25
 
32
26
  module Frontend
33
27
 
34
- def pagy_json_tag(*args)
35
- %(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
28
+ if defined?(Oj)
29
+ # returns a script tag with the JSON-serialized args generated with the faster oj gem
30
+ def pagy_json_tag(*args)
31
+ %(<script type="application/json" class="pagy-json">#{Oj.dump(args, mode: :strict)}</script>)
32
+ end
33
+ else
34
+ require 'json'
35
+ # returns a script tag with the JSON-serialized args generated with the slower to_json
36
+ def pagy_json_tag(*args)
37
+ %(<script type="application/json" class="pagy-json">#{args.to_json}</script>)
38
+ end
36
39
  end
37
40
 
41
+ # returns the SHA1 (fastest on modern ruby) string used as default `id` attribute by all the `*_js` tags
38
42
  def pagy_id
39
- # SHA1 is the fastest on modern ruby
40
43
  "pagy-#{Digest::SHA1.hexdigest(caller(2..2)[0].split(':in')[0])}"
41
44
  end
42
45
 
@@ -2,25 +2,8 @@
2
2
  # encoding: utf-8
3
3
  # frozen_string_literal: true
4
4
 
5
- require 'pagy/extras/shared'
6
-
7
5
  class Pagy
8
6
 
9
- def to_h
10
- { count: defined?(@count) && @count,
11
- page: @page,
12
- items: @items,
13
- pages: @pages,
14
- last: @last,
15
- offset: @offset,
16
- from: @from,
17
- to: @to,
18
- prev: @prev,
19
- next: @next,
20
- vars: @vars,
21
- series: series }
22
- end
23
-
24
7
  module Frontend
25
8
 
26
9
  def pagy_prev_url(pagy)
@@ -41,15 +24,6 @@ class Pagy
41
24
  : %(<span class="page next disabled">#{text}</span>)
42
25
  end
43
26
 
44
- def pagy_serialized(pagy)
45
- pagy.to_h.merge(prev_url: pagy_prev_url(pagy), next_url: pagy_next_url(pagy))
46
- end
47
-
48
- # Multi purpose JSON tag for custom javascript initialization
49
- def pagy_apply_init_tag(pagy, function, payload=pagy_serialized(pagy))
50
- pagy_json_tag(:applyInit, function, payload)
51
- end
52
-
53
27
  end
54
28
 
55
29
  end
@@ -47,11 +47,12 @@ class Pagy
47
47
  %(<nav class="pagy-nav pagination" role="navigation" aria-label="pager">#{html}</nav>)
48
48
  end
49
49
 
50
- # Return examples: "Displaying items 41-60 of 324 in total" or "Displaying Products 41-60 of 324 in total"
50
+ # Return examples: "Displaying items 41-60 of 324 in total" of "Displaying Products 41-60 of 324 in total"
51
51
  def pagy_info(pagy)
52
- name = pagy_t(pagy.vars[:item_path], count: pagy.count)
53
- path = pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages'
54
- pagy_t(path, item_name: name, count: pagy.count, from: pagy.from, to: pagy.to)
52
+ path = if (count = pagy.count) == 0 ; 'pagy.info.no_items'
53
+ else pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages'
54
+ end
55
+ pagy_t(path, item_name: pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to)
55
56
  end
56
57
 
57
58
  MARKER = "-pagy-#{'pagy'.hash}-"
@@ -67,7 +68,7 @@ class Pagy
67
68
  end
68
69
 
69
70
  # Similar to I18n.t: just ~18x faster using ~10x less memory
70
- def pagy_t(path, vars={}) Pagy::I18n.t(@pagy_locale, path, vars) end
71
+ def pagy_t(path, vars={}) Pagy::I18n.t(@pagy_locale||=nil, path, vars) end
71
72
 
72
73
  end
73
74
  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: 2.1.5
4
+ version: 3.0.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: 2019-03-29 00:00:00.000000000 Z
11
+ date: 2019-04-28 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,
@@ -26,10 +26,12 @@ files:
26
26
  - lib/locales/de.yml
27
27
  - lib/locales/en.yml
28
28
  - lib/locales/es.yml
29
+ - lib/locales/fr.yml
29
30
  - lib/locales/id.yml
30
31
  - lib/locales/ja.yml
31
32
  - lib/locales/nb.yml
32
33
  - lib/locales/nl.yml
34
+ - lib/locales/pl.yml
33
35
  - lib/locales/pt-br.yml
34
36
  - lib/locales/ru.yml
35
37
  - lib/locales/se.yml
@@ -53,9 +55,9 @@ files:
53
55
  - lib/pagy/extras/i18n.rb
54
56
  - lib/pagy/extras/items.rb
55
57
  - lib/pagy/extras/materialize.rb
58
+ - lib/pagy/extras/navs.rb
56
59
  - lib/pagy/extras/overflow.rb
57
60
  - lib/pagy/extras/pagy_search.rb
58
- - lib/pagy/extras/plain.rb
59
61
  - lib/pagy/extras/searchkick.rb
60
62
  - lib/pagy/extras/semantic.rb
61
63
  - lib/pagy/extras/shared.rb
@@ -94,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  - !ruby/object:Gem::Version
95
97
  version: '0'
96
98
  requirements: []
97
- rubygems_version: 3.0.1
99
+ rubygems_version: 3.0.3
98
100
  signing_key:
99
101
  specification_version: 4
100
102
  summary: The Ultimate Pagination Ruby Gem
@@ -1,50 +0,0 @@
1
- # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/plain
2
- # encoding: utf-8
3
- # frozen_string_literal: true
4
-
5
- require 'pagy/extras/shared'
6
-
7
- class Pagy
8
- module Frontend
9
-
10
- # added alias for symmetry
11
- alias :pagy_plain_nav :pagy_nav
12
-
13
- # Plain compact pagination: it returns the html with the series of links to the pages
14
- # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
15
- def pagy_plain_compact_nav(pagy, id=pagy_id)
16
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
17
-
18
- html = EMPTY + %(<nav id="#{id}" class="pagy-plain-compact-nav pagination" role="navigation" aria-label="pager">)
19
- html << link.call(MARKER, '', %(style="display: none;" ))
20
- (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
21
- html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
22
- : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
23
- 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;">)
24
- html << %(<span class="pagy-compact-input" style="margin: 0 0.6rem;">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</span> )
25
- html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
26
- : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
27
- html << %(</nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
28
- end
29
-
30
- # Plain responsive pagination: it returns the html with the series of links to the pages
31
- # rendered by the Pagy.responsive javascript
32
- def pagy_plain_responsive_nav(pagy, id=pagy_id)
33
- tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
34
-
35
- tags['before'] = (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
36
- : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
37
- responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
38
- tags[item.to_s] = if item.is_a?(Integer); %(<span class="page">#{link.call item}</span> ) # page link
39
- elsif item.is_a?(String) ; %(<span class="page active">#{item}</span> ) # current page
40
- elsif item == :gap ; %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap
41
- end
42
- end
43
- tags['after'] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
44
- : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
45
- script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
46
- %(<nav id="#{id}" class="pagy-plain-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
47
- end
48
-
49
- end
50
- end