pagy 3.11.0 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pagy
2
4
 
5
+ # generic variable error
3
6
  class VariableError < ArgumentError
4
7
  attr_reader :pagy
5
8
 
6
9
  def initialize(pagy)
10
+ super
7
11
  @pagy = pagy
8
12
  end
9
13
 
10
14
  def variable
11
- message =~ /expected :([\w]+)/
12
- $1.to_sym if $1
15
+ message =~ /expected :(\w+)/
16
+ Regexp.last_match(1)&.to_sym
13
17
  end
14
18
 
15
- def value
16
- pagy.vars[variable]
17
- end
19
+ def value = pagy.vars[variable]
18
20
  end
19
21
 
22
+ # specific overflow error
20
23
  class OverflowError < VariableError; end
21
24
 
22
25
  end
@@ -1,13 +1,13 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/arel
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
- module Backend ; private
5
+ module Backend
6
+ private
7
7
 
8
8
  def pagy_arel(collection, vars={})
9
9
  pagy = Pagy.new(pagy_arel_get_vars(collection, vars))
10
- return pagy, pagy_get_items(collection, pagy)
10
+ [ pagy, pagy_get_items(collection, pagy) ]
11
11
  end
12
12
 
13
13
  def pagy_arel_get_vars(collection, vars)
@@ -1,15 +1,15 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/array
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  class Pagy
6
5
  # Add specialized backend methods to paginate array collections
7
- module Backend ; private
6
+ module Backend
7
+ private
8
8
 
9
9
  # Return Pagy object and items
10
10
  def pagy_array(array, vars={})
11
11
  pagy = Pagy.new(pagy_array_get_vars(array, vars))
12
- return pagy, array[pagy.offset, pagy.items]
12
+ [ pagy, array[pagy.offset, pagy.items] ]
13
13
  end
14
14
 
15
15
  # Sub-method called only by #pagy_array: here for easy customization of variables by overriding
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bootstrap
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -9,47 +8,75 @@ class Pagy
9
8
 
10
9
  # Pagination for bootstrap: it returns the html with the series of links to the pages
11
10
  def pagy_bootstrap_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
11
+ link = pagy_link_proc(pagy, 'class="page-link"')
13
12
 
14
- html = EMPTY + (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
15
- : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
13
+ html = +%(<nav class="pagy-bootstrap-nav" role="navigation" aria-label="pager"><ul class="pagination">)
14
+ html << pagy_bootstrap_prev_html(pagy, link)
16
15
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
- html << if item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>) # page link
18
- elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>) # active page
19
- elsif item == :gap ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
16
+ html << case item
17
+ when Integer then %(<li class="page-item">#{link.call item}</li>)
18
+ when String then %(<li class="page-item active">#{link.call item}</li>)
19
+ when :gap then %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>)
20
20
  end
21
21
  end
22
- html << (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
23
- : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
24
- %(<nav class="pagy-bootstrap-nav" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
22
+ html << pagy_bootstrap_next_html(pagy, link)
23
+ html << %(</ul></nav>)
25
24
  end
26
25
 
27
26
  # Javascript pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.nav javascript
28
27
  def pagy_bootstrap_nav_js(pagy, id=pagy_id)
29
- link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next
30
- tags = { 'before' => p_prev ? %(<ul class="pagination"><li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
31
- : %(<ul class="pagination"><li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>),
28
+ link = pagy_link_proc(pagy, 'class="page-link"')
29
+ tags = { 'before' => %(<ul class="pagination">#{pagy_bootstrap_prev_html pagy, link}),
32
30
  'link' => %(<li class="page-item">#{mark = link.call(PAGE_PLACEHOLDER)}</li>),
33
31
  'active' => %(<li class="page-item active">#{mark}</li>),
34
- 'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>),
35
- 'after' => p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li></ul>)
36
- : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li></ul>) }
37
- %(<nav id="#{id}" class="pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
32
+ 'gap' => %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.gap'}</a></li>),
33
+ 'after' => %(#{pagy_bootstrap_next_html pagy, link}</ul>) }
34
+
35
+ html = %(<nav id="#{id}" class="pagy-bootstrap-nav-js" role="navigation" aria-label="pager"></nav>)
36
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
38
37
  end
39
38
 
40
39
  # Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
41
40
  def pagy_bootstrap_combo_nav_js(pagy, id=pagy_id)
42
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
43
-
44
- html = %(<nav id="#{id}" class="pagy-bootstrap-combo-nav-js pagination" role="navigation" aria-label="pager">) + %(<div class="btn-group" role="group">)
45
- html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
46
- : %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
47
- input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="text-primary" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
48
- html << %(<div class="pagy-combo-input btn btn-primary disabled" style="white-space: nowrap;">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div>)
49
- html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
50
- : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
51
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
41
+ link = pagy_link_proc(pagy)
42
+ p_page = pagy.page
43
+ p_pages = pagy.pages
44
+ input = %(<input type="number" min="1" max="#{p_pages}" value="#{p_page}" class="text-primary" style="padding: 0; border: none; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
45
+
46
+ %(<nav id="#{id}" class="pagy-bootstrap-combo-nav-js pagination" role="navigation" aria-label="pager"><div class="btn-group" role="group">#{
47
+ if (p_prev = pagy.prev)
48
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"'
49
+ else
50
+ %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>)
51
+ end
52
+ }<div class="pagy-combo-input btn btn-primary disabled" style="white-space: nowrap;">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</div>#{
53
+ if (p_next = pagy.next)
54
+ link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"'
55
+ else
56
+ %(<a class="next btn btn-primary disabled" href="#">#{pagy_t 'pagy.nav.next' }</a>)
57
+ end
58
+ }</div></nav>#{
59
+ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
60
+ })
52
61
  end
53
62
 
63
+ private
64
+
65
+ def pagy_bootstrap_prev_html(pagy, link)
66
+ if (p_prev = pagy.prev)
67
+ %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
68
+ else
69
+ %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.prev'}</a></li>)
70
+ end
71
+ end
72
+
73
+ def pagy_bootstrap_next_html(pagy, link)
74
+ if (p_next = pagy.next)
75
+ %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
76
+ else
77
+ %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t 'pagy.nav.next'}</a></li>)
78
+ end
79
+ end
80
+
54
81
  end
55
82
  end
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -9,52 +8,71 @@ class Pagy
9
8
 
10
9
  # Pagination for Bulma: it returns the html with the series of links to the pages
11
10
  def pagy_bulma_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
11
+ link = pagy_link_proc(pagy)
13
12
 
14
- html = (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
15
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>)) \
16
- + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
17
- : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
18
- html << '<ul class="pagination-list">'
13
+ html = +%(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">)
14
+ html << pagy_bulma_prev_next_html(pagy, link)
15
+ html << %(<ul class="pagination-list">)
19
16
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
20
- html << if item.is_a?(Integer); %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
21
- elsif item.is_a?(String) ; %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
22
- elsif item == :gap ; %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>) # page gap
17
+ html << case item
18
+ when Integer then %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>) # page link
19
+ when String then %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-label="page #{item}" aria-current="page")}</li>) # active page
20
+ when :gap then %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap'}</span></li>) # page gap
23
21
  end
24
22
  end
25
- html << '</ul>'
26
- %(<nav class="pagy-bulma-nav pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
23
+ html << %(</ul></nav>)
27
24
  end
28
25
 
29
- # Javascript pagination for bulma: it returns a nav and a JSON tag used by the Pagy.nav javascript
30
26
  def pagy_bulma_nav_js(pagy, id=pagy_id)
31
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
32
- tags = { 'before' => ( (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
33
- : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>)) \
34
- + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
35
- : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>)) \
36
- + '<ul class="pagination-list">' ),
37
- 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}"))}</li>),
38
- 'active' => %(<li>#{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}"))}</li>),
39
- 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>),
27
+ link = pagy_link_proc(pagy)
28
+ tags = { 'before' => %(#{pagy_bulma_prev_next_html(pagy, link)}<ul class="pagination-list">),
29
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link" aria-label="goto page #{PAGE_PLACEHOLDER}")}</li>),
30
+ 'active' => %(<li>#{link.call PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}")}</li>),
31
+ 'gap' => %(<li><span class="pagination-ellipsis">#{pagy_t 'pagy.nav.gap' }</span></li>),
40
32
  'after' => '</ul>' }
41
- %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
33
+
34
+ html = %(<nav id="#{id}" class="pagy-bulma-nav-js pagination is-centered" role="navigation" aria-label="pagination"></nav>)
35
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
42
36
  end
43
37
 
44
38
  # Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
45
39
  def pagy_bulma_combo_nav_js(pagy, id=pagy_id)
46
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
47
-
48
- html = %(<nav id="#{id}" class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination">) \
49
- + %(<div class="field is-grouped is-grouped-centered" role="group">)
50
- html << (p_prev ? %(<p class="control">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"')}</p>)
51
- : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.prev')}</a></p>))
52
- input = %(<input class="input" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin:0 0.3rem;">)
53
- html << %(<div class="pagy-combo-input control level is-mobile">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</div>)
54
- html << (p_next ? %(<p class="control">#{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}</p>)
55
- : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
56
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
40
+ link = pagy_link_proc(pagy)
41
+ p_page = pagy.page
42
+ p_pages = pagy.pages
43
+ input = %(<input class="input" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 0; text-align: center; width: #{p_pages.to_s.length+1}rem; margin:0 0.3rem;">)
44
+
45
+ %(<nav id="#{id}" class="pagy-bulma-combo-nav-js" role="navigation" aria-label="pagination"><div class="field is-grouped is-grouped-centered" role="group">#{
46
+ if (p_prev = pagy.prev)
47
+ %(<p class="control">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"'}</p>)
48
+ else
49
+ %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.prev'}</a></p>)
50
+ end
51
+ }<div class="pagy-combo-input control level is-mobile">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</div>#{
52
+ if (p_next = pagy.next)
53
+ %(<p class="control">#{link.call p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"'}</p>)
54
+ else
55
+ %(<p class="control"><a class="button" disabled>#{pagy_t 'pagy.nav.next'}</a></p>)
56
+ end
57
+ }</div></nav>#{
58
+ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
59
+ })
57
60
  end
58
61
 
62
+ private
63
+
64
+ def pagy_bulma_prev_next_html(pagy, link)
65
+ html = if (p_prev = pagy.prev)
66
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"'
67
+ else
68
+ %(<a class="pagination-previous" disabled>#{pagy_t 'pagy.nav.prev'}</a>)
69
+ end
70
+ html << if (p_next = pagy.next)
71
+ link.call p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"'
72
+ else
73
+ %(<a class="pagination-next" disabled>#{pagy_t 'pagy.nav.next' }</a>)
74
+ end
75
+ end
76
+
59
77
  end
60
78
  end
@@ -1,20 +1,17 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/countless
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/countless'
6
5
 
7
6
  class Pagy
8
7
 
9
- # used by the items extra
10
- COUNTLESS = true
11
-
12
- module Backend ; private # the whole module is private so no problem with including it in a controller
8
+ module Backend
9
+ private # the whole module is private so no problem with including it in a controller
13
10
 
14
11
  # Return Pagy object and items
15
12
  def pagy_countless(collection, vars={})
16
13
  pagy = Pagy::Countless.new(pagy_countless_get_vars(collection, vars))
17
- return pagy, pagy_countless_get_items(collection, pagy)
14
+ [ pagy, pagy_countless_get_items(collection, pagy) ]
18
15
  end
19
16
 
20
17
  # Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
@@ -29,7 +26,8 @@ class Pagy
29
26
  items = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
30
27
  items_size = items.size
31
28
  items.pop if items_size == pagy.items + 1
32
- pagy.finalize(items_size) # finalize may adjust pagy.items, so must be used after checking the size
29
+ # finalize may adjust pagy.items, so must be used after checking the size
30
+ pagy.finalize(items_size)
33
31
  items
34
32
  end
35
33
 
@@ -1,13 +1,21 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/elasticsearch_rails
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
- require 'pagy/extras/pagy_search'
6
-
7
4
  class Pagy
8
5
 
9
- # used by the items extra
10
- ELASTICSEARCH_RAILS = true
6
+ VARS[:elasticsearch_rails_search_method] ||= :pagy_search
7
+
8
+ module ElasticsearchRails
9
+ # returns an array used to delay the call of #search
10
+ # after the pagination variables are merged to the options
11
+ # it also pushes to the same array an eventually called method
12
+ def pagy_elasticsearch_rails(query_or_payload, **options)
13
+ [self, query_or_payload, options].tap do |args|
14
+ args.define_singleton_method(:method_missing){|*a| args += a}
15
+ end
16
+ end
17
+ alias_method VARS[:elasticsearch_rails_search_method], :pagy_elasticsearch_rails
18
+ end
11
19
 
12
20
  # create a Pagy object from an Elasticsearch::Model::Response::Response object
13
21
  def self.new_from_elasticsearch_rails(response, vars={})
@@ -19,23 +27,25 @@ class Pagy
19
27
  end
20
28
 
21
29
  # Add specialized backend methods to paginate ElasticsearchRails searches
22
- module Backend ; private
30
+ module Backend
31
+ private
23
32
 
24
33
  # Return Pagy object and items
25
34
  def pagy_elasticsearch_rails(pagy_search_args, vars={})
26
- model, search_args, _block, *called = pagy_search_args
27
- vars = pagy_elasticsearch_rails_get_vars(nil, vars)
28
- search_args[-1][:size] = vars[:items]
29
- search_args[-1][:from] = vars[:items] * (vars[:page] - 1)
30
- response = model.search(*search_args)
31
- total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
32
- vars[:count] = total.is_a?(Hash) ? total['value'] : total
35
+ model, query_or_payload, options, *called = pagy_search_args
36
+ vars = pagy_elasticsearch_rails_get_vars(nil, vars)
37
+ options[:size] = vars[:items]
38
+ options[:from] = vars[:items] * (vars[:page] - 1)
39
+ response = model.search(query_or_payload, **options)
40
+ total = response.respond_to?(:raw_response) ? response.raw_response['hits']['total'] : response.response['hits']['total']
41
+ vars[:count] = total.is_a?(Hash) ? total['value'] : total
42
+
33
43
  pagy = Pagy.new(vars)
34
44
  # with :last_page overflow we need to re-run the method in order to get the hits
35
- if defined?(OVERFLOW) && pagy.overflow? && pagy.vars[:overflow] == :last_page
36
- return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page))
37
- end
38
- return pagy, called.empty? ? response : response.send(*called)
45
+ return pagy_elasticsearch_rails(pagy_search_args, vars.merge(page: pagy.page)) \
46
+ if defined?(Pagy::UseOverflowExtra) && pagy.overflow? && pagy.vars[:overflow] == :last_page
47
+
48
+ [ pagy, called.empty? ? response : response.send(*called) ]
39
49
  end
40
50
 
41
51
  # Sub-method called only by #pagy_elasticsearch_rails: here for easy customization of variables by overriding
@@ -1,5 +1,4 @@
1
1
  # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/foundation
2
- # encoding: utf-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  require 'pagy/extras/shared'
@@ -9,49 +8,75 @@ class Pagy
9
8
 
10
9
  # Pagination for Foundation: it returns the html with the series of links to the pages
11
10
  def pagy_foundation_nav(pagy)
12
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
11
+ link = pagy_link_proc(pagy)
13
12
 
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>))
13
+ html = +%(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">)
14
+ html << pagy_foundation_prev_html(pagy, link)
16
15
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
17
- html << if item.is_a?(Integer); %(<li>#{link.call item}</li>) # page link
18
- elsif item.is_a?(String) ; %(<li class="current">#{item}</li>) # active page
19
- elsif item == :gap ; %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
16
+ html << case item
17
+ when Integer then %(<li>#{link.call item}</li>) # page link
18
+ when String then %(<li class="current">#{item}</li>) # active page
19
+ when :gap then %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
20
20
  end
21
21
  end
22
- html << (p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
23
- : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
24
- %(<nav class="pagy-foundation-nav" role="navigation" aria-label="Pagination"><ul class="pagination">#{html}</ul></nav>)
22
+ html << pagy_foundation_next_html(pagy, link)
23
+ html << %(</ul></nav>)
25
24
  end
26
25
 
27
26
  # Javascript pagination for foundation: it returns a nav and a JSON tag used by the Pagy.nav javascript
28
27
  def pagy_foundation_nav_js(pagy, id=pagy_id)
29
- link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next
30
- tags = { 'before' => ( '<ul class="pagination">' \
31
- + (p_prev ? %(<li class="prev">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"')}</li>)
32
- : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>)) ),
33
- 'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>),
28
+ link = pagy_link_proc(pagy)
29
+ tags = { 'before' => %(<ul class="pagination">#{pagy_foundation_prev_html pagy, link}),
30
+ 'link' => %(<li>#{link.call PAGE_PLACEHOLDER}</li>),
34
31
  'active' => %(<li class="current">#{pagy.page}</li>),
35
32
  'gap' => %(<li class="ellipsis gap" aria-hidden="true"></li>),
36
- 'after' => ( (p_next ? %(<li class="next">#{link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next"')}</li>)
37
- : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>)) \
38
- + '</ul>' ) }
39
- %(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(TRIM) && pagy.vars[:page_param])})
33
+ 'after' => %(#{pagy_foundation_next_html pagy, link}</ul>) }
34
+
35
+ html = %(<nav id="#{id}" class="pagy-foundation-nav-js" role="navigation" aria-label="Pagination"></nav>)
36
+ html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)
40
37
  end
41
38
 
42
39
  # Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript
43
40
  def pagy_foundation_combo_nav_js(pagy, id=pagy_id)
44
- link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages
45
-
46
- html = %(<nav id="#{id}" class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination">) + %(<div class="input-group">)
47
- html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"')
48
- : %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
49
- input = %(<input class="input-group-field cell shrink" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="width: #{p_pages.to_s.length+1}rem; padding: 0 0.3rem; margin: 0 0.3rem;">)
50
- html << %(<span class="input-group-label">#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}</span>)
51
- html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"')
52
- : %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
53
- html << %(</div></nav>#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(TRIM) && pagy.vars[:page_param])})
41
+ link = pagy_link_proc(pagy)
42
+ p_page = pagy.page
43
+ p_pages = pagy.pages
44
+ input = %(<input class="input-group-field cell shrink" type="number" min="1" max="#{p_pages}" value="#{p_page}" style="width: #{p_pages.to_s.length+1}rem; padding: 0 0.3rem; margin: 0 0.3rem;">)
45
+
46
+ %(<nav id="#{id}" class="pagy-foundation-combo-nav-js" role="navigation" aria-label="Pagination"><div class="input-group">#{
47
+ if (p_prev = pagy.prev)
48
+ link.call p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"'
49
+ else
50
+ %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t 'pagy.nav.prev'}</a>)
51
+ end
52
+ }<span class="input-group-label">#{pagy_t 'pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages}</span>#{
53
+ if (p_next = pagy.next)
54
+ link.call p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"'
55
+ else
56
+ %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t 'pagy.nav.next'}</a>)
57
+ end
58
+ }</div></nav>#{
59
+ pagy_json_tag pagy, :combo_nav, id, p_page, pagy_marked_link(link)
60
+ })
54
61
  end
55
62
 
63
+ private
64
+
65
+ def pagy_foundation_prev_html(pagy, link)
66
+ if (p_prev = pagy.prev)
67
+ %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
68
+ else
69
+ %(<li class="prev disabled">#{pagy_t 'pagy.nav.prev' }</li>)
70
+ end
71
+ end
72
+
73
+ def pagy_foundation_next_html(pagy, link)
74
+ if (p_next = pagy.next)
75
+ %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
76
+ else
77
+ %(<li class="next disabled">#{pagy_t 'pagy.nav.next'}</li>)
78
+ end
79
+ end
80
+
56
81
  end
57
82
  end