pagy 1.3.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/pagy.rb +32 -10
  3. data/lib/javascripts/pagy.js +0 -7
  4. data/lib/locales/utils/i18n.rb +18 -0
  5. data/lib/locales/utils/loader.rb +29 -0
  6. data/lib/locales/{plurals.rb → utils/p11n.rb} +20 -18
  7. data/lib/pagy.rb +3 -2
  8. data/lib/pagy/backend.rb +2 -0
  9. data/lib/pagy/countless.rb +4 -1
  10. data/lib/pagy/extras/array.rb +2 -0
  11. data/lib/pagy/extras/bootstrap.rb +12 -14
  12. data/lib/pagy/extras/bulma.rb +10 -12
  13. data/lib/pagy/extras/countless.rb +2 -0
  14. data/lib/pagy/extras/elasticsearch_rails.rb +31 -7
  15. data/lib/pagy/extras/foundation.rb +11 -13
  16. data/lib/pagy/extras/i18n.rb +7 -4
  17. data/lib/pagy/extras/items.rb +16 -17
  18. data/lib/pagy/extras/materialize.rb +11 -13
  19. data/lib/pagy/extras/overflow.rb +36 -34
  20. data/lib/pagy/extras/pagy_search.rb +16 -0
  21. data/lib/pagy/extras/plain.rb +4 -5
  22. data/lib/pagy/extras/searchkick.rb +31 -8
  23. data/lib/pagy/extras/semantic.rb +8 -10
  24. data/lib/pagy/extras/shared.rb +1 -7
  25. data/lib/pagy/extras/support.rb +2 -1
  26. data/lib/pagy/extras/trim.rb +2 -1
  27. data/lib/pagy/frontend.rb +17 -26
  28. data/lib/templates/bootstrap_nav.html.erb +1 -1
  29. data/lib/templates/bootstrap_nav.html.haml +1 -1
  30. data/lib/templates/bootstrap_nav.html.slim +1 -1
  31. data/lib/templates/bulma_nav.html.erb +1 -1
  32. data/lib/templates/bulma_nav.html.haml +1 -1
  33. data/lib/templates/bulma_nav.html.slim +1 -1
  34. data/lib/templates/foundation_nav.html.erb +1 -1
  35. data/lib/templates/foundation_nav.html.haml +1 -1
  36. data/lib/templates/foundation_nav.html.slim +1 -1
  37. data/pagy.gemspec +2 -5
  38. metadata +10 -10
  39. data/lib/pagy/extras/navs.rb +0 -4
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/foundation
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -8,10 +9,10 @@ class Pagy
8
9
 
9
10
  # Pagination for Foundation: it returns the html with the series of links to the pages
10
11
  def pagy_foundation_nav(pagy)
11
- html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next
12
+ link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
12
13
 
13
- html << (p_prev ? %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
14
- : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>))
14
+ html = EMPTY + (p_prev ? %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
15
+ : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>))
15
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
16
17
  html << if item.is_a?(Integer); %(<li>#{link.call item}</li>) # page link
17
18
  elsif item.is_a?(String) ; %(<li class="current">#{item}</li>) # active page
@@ -20,16 +21,15 @@ class Pagy
20
21
  end
21
22
  html << (p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
22
23
  : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
23
- %(<nav class="pagy-nav-foundation pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">#{html}</ul></nav>)
24
+ %(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">#{html}</ul></nav>)
24
25
  end
25
- deprecate :pagy_nav_foundation, :pagy_foundation_nav
26
26
 
27
27
  # Compact pagination for Foundation: it returns the html with the series of links to the pages
28
28
  # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
29
29
  def pagy_foundation_compact_nav(pagy, id=pagy_id)
30
- html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
30
+ link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
31
31
 
32
- html << %(<nav id="#{id}" class="pagy-nav-compact-foundation pagy-foundation-compact-nav" role="navigation" aria-label="Pagination">)
32
+ html = EMPTY + %(<nav id="#{id}" class="pagy-foundation-compact-nav" role="navigation" aria-label="Pagination">)
33
33
  html << link.call(MARKER, '', %(style="display: none;" ))
34
34
  (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
35
35
  html << %(<div class="input-group">)
@@ -41,14 +41,13 @@ class Pagy
41
41
  : %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
42
42
  html << %(</div></nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
43
43
  end
44
- deprecate :pagy_nav_compact_foundation, :pagy_foundation_compact_nav
45
44
 
46
45
  # Responsive pagination for Foundation: it returns the html with the series of links to the pages
47
46
  # rendered by the Pagy.responsive javascript
48
47
  def pagy_foundation_responsive_nav(pagy, id=pagy_id)
49
48
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
50
49
 
51
- tags['before'] = +'<ul class="pagination">'
50
+ tags['before'] = EMPTY + '<ul class="pagination">'
52
51
  tags['before'] << (p_prev ? %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
53
52
  : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>))
54
53
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
@@ -57,13 +56,12 @@ class Pagy
57
56
  elsif item == :gap ; %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
58
57
  end
59
58
  end
60
- tags['after'] = +(p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
61
- : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
59
+ tags['after'] = EMPTY + (p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
60
+ : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
62
61
  tags['after'] << '</ul>'
63
62
  script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
64
- %(<nav id="#{id}" class="pagy-nav-responsive-foundation pagy-foundation-responsive-nav" aria-label="Pagination"></nav>#{script})
63
+ %(<nav id="#{id}" class="pagy-foundation-responsive-nav" aria-label="Pagination"></nav>#{script})
65
64
  end
66
- deprecate :pagy_nav_responsive_foundation, :pagy_foundation_responsive_nav
67
65
 
68
66
  end
69
67
  end
@@ -1,4 +1,6 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/i18n
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
2
4
 
3
5
  class Pagy
4
6
  # Use ::I18n gem
@@ -6,10 +8,11 @@ class Pagy
6
8
 
7
9
  ::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')]
8
10
 
9
- # Override the built-in pagy_t
10
- def pagy_t(*args)
11
- ::I18n.t(*args)
12
- end
11
+ Pagy::I18n.clear.instance_eval { undef :load; undef :t } # unload the pagy default constant for efficiency
12
+
13
+ # no :pagy_without_i18n alias with the i18n gem
14
+ def pagy_t_with_i18n(*args) ::I18n.t(*args) end
15
+ alias :pagy_t :pagy_t_with_i18n
13
16
 
14
17
  end
15
18
  end
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/items
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -14,24 +15,22 @@ class Pagy
14
15
 
15
16
  def pagy_with_items(vars)
16
17
  vars[:items] ||= (items = params[vars[:items_param] || VARS[:items_param]]) && # :items from :items_param
17
- [items&.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
18
+ [items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
18
19
  end
19
20
 
20
- alias_method :pagy_get_vars_without_items, :pagy_get_vars
21
- def pagy_get_vars_with_items(collection, vars)
22
- pagy_with_items(vars)
23
- pagy_get_vars_without_items(collection, vars)
24
- end
25
- alias_method :pagy_get_vars, :pagy_get_vars_with_items
26
-
27
- # support for countless extra
28
- if defined?(Pagy::COUNTLESS) # defined in the countless extra
29
- alias_method :pagy_countless_get_vars_without_items, :pagy_countless_get_vars
30
- def pagy_countless_get_vars_with_items(collection, vars)
31
- pagy_with_items(vars)
32
- pagy_countless_get_vars_without_items(collection, vars)
33
- end
34
- alias_method :pagy_countless_get_vars, :pagy_countless_get_vars_with_items
21
+ # add the pagy*_get_vars alias-chained methods for frontend, and defined/required extras
22
+ [nil, 'countless', 'elasticsearch_rails', 'searchkick'].each do |name|
23
+ prefix, if_start, if_end = "_#{name}", "if defined?(Pagy::#{name.upcase})", "end" if name
24
+ module_eval <<-RUBY
25
+ #{if_start}
26
+ alias_method :pagy#{prefix}_get_vars_without_items, :pagy#{prefix}_get_vars
27
+ def pagy#{prefix}_get_vars_with_items(collection, vars)
28
+ pagy_with_items(vars)
29
+ pagy#{prefix}_get_vars_without_items(collection, vars)
30
+ end
31
+ alias_method :pagy#{prefix}_get_vars, :pagy#{prefix}_get_vars_with_items
32
+ #{if_end}
33
+ RUBY
35
34
  end
36
35
 
37
36
  end
@@ -49,7 +48,7 @@ class Pagy
49
48
  def pagy_items_selector(pagy, id=pagy_id)
50
49
  p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-"
51
50
 
52
- html = +%(<span id="#{id}">)
51
+ html = EMPTY + %(<span id="#{id}">)
53
52
  html << %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
54
53
  p_vars[:items] = p_items # restore the items
55
54
  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;">)
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/materialize
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -8,9 +9,9 @@ class Pagy
8
9
 
9
10
  # Pagination for materialize: it returns the html with the series of links to the pages
10
11
  def pagy_materialize_nav(pagy)
11
- html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next
12
- html << (p_prev ? %(<li class="waves-effect prev">#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
13
- : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>))
12
+ link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
13
+ html = EMPTY + (p_prev ? %(<li class="waves-effect prev">#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
14
+ : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>))
14
15
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
15
16
  html << if item.is_a?(Integer); %(<li class="waves-effect">#{link.call item}</li>) # page link
16
17
  elsif item.is_a?(String) ; %(<li class="active">#{link.call item}</li>) # active page
@@ -19,16 +20,15 @@ class Pagy
19
20
  end
20
21
  html << (p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
21
22
  : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
22
- %(<div class="pagy-nav-materialize pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></div>)
23
+ %(<div class="pagy-materialize-nav pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></div>)
23
24
  end
24
- deprecate :pagy_nav_materialize, :pagy_materialize_nav
25
25
 
26
26
  # Compact pagination for materialize: it returns the html with the series of links to the pages
27
27
  # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
28
28
  def pagy_materialize_compact_nav(pagy, id=pagy_id)
29
- html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
29
+ link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
30
30
 
31
- html << %(<div id="#{id}" class="pagy-nav-compact-materialize pagy-materialize-compact-nav pagination" role="navigation" aria-label="pager">)
31
+ html = EMPTY + %(<div id="#{id}" class="pagy-materialize-compact-nav pagination" role="navigation" aria-label="pager">)
32
32
  html << link.call(MARKER, '', %(style="display: none;" ))
33
33
  (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
34
34
  html << %(<div class="pagy-compact-chip role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;">)
@@ -42,14 +42,13 @@ class Pagy
42
42
  : %(<li class="next disabled" #{li_style}><a href="#"><i class="material-icons">chevron_right</i></a></li>))
43
43
  html << %(</ul></div>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
44
44
  end
45
- deprecate :pagy_nav_compact_materialize, :pagy_materialize_compact_nav
46
45
 
47
46
  # Responsive pagination for Materialize: it returns the html with the series of links to the pages
48
47
  # rendered by the Pagy.responsive javascript
49
48
  def pagy_materialize_responsive_nav(pagy, id=pagy_id)
50
49
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive
51
50
 
52
- tags['before'] = +'<ul class="pagination">'
51
+ tags['before'] = EMPTY + '<ul class="pagination">'
53
52
  tags['before'] << (p_prev ? %(<li class="waves-effect prev">#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
54
53
  : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>))
55
54
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
@@ -58,13 +57,12 @@ class Pagy
58
57
  elsif item == :gap ; %(<li class="gap disabled"><a href="#">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
59
58
  end
60
59
  end
61
- tags['after'] = +(p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
62
- : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
60
+ tags['after'] = EMPTY + (p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
61
+ : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
63
62
  tags['after'] << '</ul>'
64
63
  script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
65
- %(<div id="#{id}" class="pagy-nav-responsive-materialize pagy-materialize-responsive-nav pagination" role="navigation" aria-label="pager"></div>#{script})
64
+ %(<div id="#{id}" class="pagy-materialize-responsive-nav pagination" role="navigation" aria-label="pager"></div>#{script})
66
65
  end
67
- deprecate :pagy_nav_responsive_materialize, :pagy_materialize_responsive_nav
68
66
 
69
67
  end
70
68
  end
@@ -1,53 +1,55 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/overflow
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  class Pagy
5
6
 
6
7
  VARS[:overflow] = :empty_page
7
8
 
8
- def overflow?; !!@overflow end
9
+ def overflow?; @overflow end
9
10
 
10
- module Overflow
11
-
12
- def initialize(vars)
13
- super
14
- rescue OverflowError
15
- @overflow = true # add the overflow flag
16
- case @vars[:overflow]
17
- when :exception
18
- raise # same as without the extra
19
- when :last_page
20
- initial_page = @vars[:page] # save the very initial page (even after re-run)
21
- super(vars.merge!(page: @last)) # re-run with the last page
22
- @vars[:page] = initial_page # restore the inital page
23
- when :empty_page
24
- @offset = @items = @from = @to = 0 # vars relative to the actual page
25
- @prev = @last # prev relative to the actual page
26
- extend(Series) # special series for :empty_page
27
- else
28
- raise ArgumentError, "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
29
- end
11
+ alias :initialize_without_overflow :initialize
12
+ def initialize_with_overflow(vars)
13
+ @overflow ||= false # don't override if :last_page re-run the method after an overflow
14
+ initialize_without_overflow(vars)
15
+ rescue OverflowError
16
+ @overflow = true # add the overflow flag
17
+ case @vars[:overflow]
18
+ when :exception
19
+ raise # same as without the extra
20
+ when :last_page
21
+ initial_page = @vars[:page] # save the very initial page (even after re-run)
22
+ initialize(vars.merge!(page: @last)) # re-run with the last page
23
+ @vars[:page] = initial_page # restore the inital page
24
+ when :empty_page
25
+ @offset = @items = @from = @to = 0 # vars relative to the actual page
26
+ @prev = @last # prev relative to the actual page
27
+ extend(Series) # special series for :empty_page
28
+ else
29
+ raise ArgumentError, "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
30
30
  end
31
+ end
32
+ alias :initialize :initialize_with_overflow
31
33
 
32
- module Series
33
- def series(size=@vars[:size])
34
- @page = @last # series for last page
35
- super(size).tap do |s| # call original series
36
- s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
37
- @page = @vars[:page] # restore the actual page
38
- end
34
+ module Series
35
+ def series(size=@vars[:size])
36
+ @page = @last # series for last page
37
+ super(size).tap do |s| # call original series
38
+ s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
39
+ @page = @vars[:page] # restore the actual page
39
40
  end
40
41
  end
41
-
42
42
  end
43
- prepend Overflow
43
+
44
44
 
45
45
  # support for Pagy::Countless
46
46
  if defined?(Pagy::Countless)
47
- module CountlessOverflow
47
+ class Countless
48
48
 
49
- def finalize(items)
50
- super
49
+ alias :finalize_without_overflow :finalize
50
+ def finalize_with_overflow(items)
51
+ @overflow = false
52
+ finalize_without_overflow(items)
51
53
  rescue OverflowError
52
54
  @overflow = true # add the overflow flag
53
55
  case @vars[:overflow]
@@ -61,9 +63,9 @@ class Pagy
61
63
  raise ArgumentError, "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
62
64
  end
63
65
  end
66
+ alias :finalize :finalize_with_overflow
64
67
 
65
68
  end
66
- Countless.prepend CountlessOverflow
67
69
  end
68
70
 
69
71
  end
@@ -0,0 +1,16 @@
1
+ # Support module to capture search calls
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
4
+
5
+ class Pagy
6
+ module Search
7
+ # returns an array used to delay the call of #search
8
+ # after the pagination variables are merged to the options
9
+ # it also pushes to the same array an eventually called method and arguments
10
+ def pagy_search(arg, options={}, &block)
11
+ [self, arg, options, block].tap do |args|
12
+ args.define_singleton_method(:method_missing){|*a| args += a}
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/plain
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -12,9 +13,9 @@ class Pagy
12
13
  # Plain compact pagination: it returns the html with the series of links to the pages
13
14
  # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
14
15
  def pagy_plain_compact_nav(pagy, id=pagy_id)
15
- html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
16
+ link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
16
17
 
17
- html << %(<nav id="#{id}" class="pagy-nav-compact pagy-plain-compact-nav pagination" role="navigation" aria-label="pager">)
18
+ html = EMPTY + %(<nav id="#{id}" class="pagy-plain-compact-nav pagination" role="navigation" aria-label="pager">)
18
19
  html << link.call(MARKER, '', %(style="display: none;" ))
19
20
  (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
20
21
  html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
@@ -25,7 +26,6 @@ class Pagy
25
26
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
26
27
  html << %(</nav>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
27
28
  end
28
- deprecate :pagy_nav_compact, :pagy_plain_compact_nav
29
29
 
30
30
  # Plain responsive pagination: it returns the html with the series of links to the pages
31
31
  # rendered by the Pagy.responsive javascript
@@ -43,9 +43,8 @@ class Pagy
43
43
  tags['after'] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
44
44
  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
45
45
  script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
46
- %(<nav id="#{id}" class="pagy-nav-responsive pagy-plain-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
46
+ %(<nav id="#{id}" class="pagy-plain-responsive-nav pagination" role="navigation" aria-label="pager"></nav>#{script})
47
47
  end
48
- deprecate :pagy_nav_responsive, :pagy_plain_responsive_nav
49
48
 
50
49
  end
51
50
  end
@@ -1,20 +1,43 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/searchkick
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
4
+
5
+ require 'pagy/extras/pagy_search'
2
6
 
3
7
  class Pagy
8
+
9
+ # used by the items extra
10
+ SEARCHKICK = true
11
+
12
+ # create a Pagy object from a Searchkick::Results object
13
+ def self.new_from_searchkick(results, vars={})
14
+ vars[:items] = results.options[:per_page]
15
+ vars[:page] = results.options[:page]
16
+ vars[:count] = results.total_count
17
+ new(vars)
18
+ end
19
+
4
20
  # Add specialized backend methods to paginate Searchkick::Results
5
21
  module Backend ; private
6
- # Return Pagy object and items
7
- def pagy_searchkick(results, vars={})
8
- pagy = Pagy.new(pagy_searchkick_get_vars(results, vars))
9
- return pagy, results
22
+
23
+ # Return Pagy object and results
24
+ def pagy_searchkick(search_args, vars={})
25
+ model, term, options, block, *called = search_args
26
+ vars = pagy_searchkick_get_vars(nil, vars)
27
+ options[:per_page] = vars[:items]
28
+ options[:page] = vars[:page]
29
+ results = model.search(term, options, &block)
30
+ vars[:count] = results.total_count
31
+ return Pagy.new(vars), called.empty? ? results : results.send(*called)
10
32
  end
11
33
 
12
34
  # Sub-method called only by #pagy_searchkick: here for easy customization of variables by overriding
13
- def pagy_searchkick_get_vars(results, vars)
14
- vars[:count] ||= results.total_count
15
- vars[:page] ||= results.options[:page]
16
- vars[:items] ||= results.options[:per_page]
35
+ # the _collection argument is not available when the method is called
36
+ def pagy_searchkick_get_vars(_collection, vars)
37
+ vars[:items] ||= VARS[:items]
38
+ vars[:page] ||= (params[ vars[:page_param] || VARS[:page_param] ] || 1).to_i
17
39
  vars
18
40
  end
41
+
19
42
  end
20
43
  end
@@ -1,4 +1,5 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/semantic
2
+ # encoding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  require 'pagy/extras/shared'
@@ -8,10 +9,10 @@ class Pagy
8
9
 
9
10
  # Pagination for semantic-ui: it returns the html with the series of links to the pages
10
11
  def pagy_semantic_nav(pagy)
11
- html, link, p_prev, p_next = +'', pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next
12
+ link, p_prev, p_next = pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next
12
13
 
13
- html << (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
14
- : %(<div class="item disabled"><i class="left small chevron icon"></i></div>))
14
+ html = EMPTY + (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
15
+ : %(<div class="item disabled"><i class="left small chevron icon"></i></div>))
15
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
16
17
  html << if item.is_a?(Integer); %(#{link.call item}) # page link
17
18
  elsif item.is_a?(String) ; %(<a class="item active">#{item}</a>) # current page
@@ -20,16 +21,15 @@ class Pagy
20
21
  end
21
22
  html << (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
22
23
  : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
23
- %(<div class="pagy-nav-semantic pagy-semantic-nav ui pagination menu" aria-label="pager">#{html}</div>)
24
+ %(<div class="pagy-semantic-nav ui pagination menu" aria-label="pager">#{html}</div>)
24
25
  end
25
- deprecate :pagy_nav_semantic, :pagy_semantic_nav
26
26
 
27
27
  # Compact pagination for semantic: it returns the html with the series of links to the pages
28
28
  # we use a numeric input tag to set the page and the Pagy.compact javascript to navigate
29
29
  def pagy_semantic_compact_nav(pagy, id=pagy_id)
30
- html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.page, pagy.pages
30
+ link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.page, pagy.pages
31
31
 
32
- html << %(<div id="#{id}" class="pagy-nav-compact-semantic pagy-semantic-compact-nav ui compact menu" role="navigation" aria-label="pager">)
32
+ html = EMPTY + %(<div id="#{id}" class="pagy-semantic-compact-nav ui compact menu" role="navigation" aria-label="pager">)
33
33
  html << link.call(MARKER, '', %(style="display: none;" ))
34
34
  (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
35
35
  html << (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
@@ -40,7 +40,6 @@ class Pagy
40
40
  : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
41
41
  html << %(</div>#{pagy_json_tag(:compact, id, MARKER, p_page, !!defined?(TRIM))})
42
42
  end
43
- deprecate :pagy_nav_compact_semantic, :pagy_semantic_compact_nav
44
43
 
45
44
  # Responsive pagination for semantic: it returns the html with the series of links to the pages
46
45
  # rendered by the Pagy.responsive javascript
@@ -58,9 +57,8 @@ class Pagy
58
57
  tags['after'] = (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
59
58
  : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
60
59
  script = pagy_json_tag(:responsive, id, tags, responsive[:widths], responsive[:series])
61
- %(<div id="#{id}" class="pagy-nav-responsive-semantic pagy-semantic-responsive-nav ui pagination menu" role="navigation" aria-label="pager"></div>#{script})
60
+ %(<div id="#{id}" class="pagy-semantic-responsive-nav ui pagination menu" role="navigation" aria-label="pager"></div>#{script})
62
61
  end
63
- deprecate :pagy_nav_responsive_semantic, :pagy_semantic_responsive_nav
64
62
 
65
63
  end
66
64
  end