Module: Pagy::Frontend

Defined in:
lib/pagy/frontend.rb,
lib/pagy/extras/i18n.rb,
lib/pagy/extras/navs.rb,
lib/pagy/extras/trim.rb,
lib/pagy/extras/bulma.rb,
lib/pagy/extras/items.rb,
lib/pagy/extras/semantic.rb,
lib/pagy/extras/bootstrap.rb,
lib/pagy/extras/foundation.rb,
lib/pagy/extras/materialize.rb

Overview

Use ::I18n gem

Constant Summary collapse

MARKER =
"-pagy-#{'pagy'.hash}-"
TRIM =

boolean used by the compact navs

true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(file: Pagy.root.join('locales', 'en.yml'), language: 'en') ⇒ Object

Pagy::Frontend::I18N



58
59
60
61
# File 'lib/pagy/frontend.rb', line 58

def (I18N = {data:{}}).load(file:Pagy.root.join('locales', 'en.yml'), language: 'en')
  self[:data]   = YAML.load_file(file)[language]
  self[:plural] = eval(Pagy.root.join('locales', 'plurals.rb').read)[language] #rubocop:disable Security/Eval
end

Instance Method Details

#pagy_get_params(params) ⇒ Object

Sub-method called only by #pagy_url_for: here for easy customization of params by overriding



43
# File 'lib/pagy/frontend.rb', line 43

def pagy_get_params(params) params end

#pagy_info(pagy) ⇒ Object

Return examples: “Displaying items 41-60 of 324 in total” or “Displaying Products 41-60 of 324 in total”



30
31
32
33
34
# File 'lib/pagy/frontend.rb', line 30

def pagy_info(pagy)
  name = pagy_t(pagy.vars[:item_path], count: pagy.count)
  path = pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages'
  pagy_t(path, item_name: name, count: pagy.count, from: pagy.from, to: pagy.to)
end

#pagy_items_selector(pagy, id = caller(1,1)[0].hash) ⇒ Object

Return the items selector HTML. For example “Show [20] items per page”



33
34
35
36
37
38
39
40
41
# File 'lib/pagy/extras/items.rb', line 33

def pagy_items_selector(pagy, id=caller(1,1)[0].hash)
  pagy = pagy.clone; p_vars = pagy.vars; p_items = p_vars[:items]; p_vars[:items] = "#{MARKER}-items-"

  html = +%(<span id="pagy-items-#{id}">)
    html << %(<a href="#{pagy_url_for("#{MARKER}-page-", pagy)}"></a>)
    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;">)
    html << %(#{pagy_t('pagy.items', items_input: input, count: p_items)})
  html << %(</span><script type="application/json" class="pagy-items-json">["#{id}", "#{MARKER}", #{pagy.from}]</script>)
end


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pagy/extras/trim.rb', line 12

def pagy_link_proc_with_trim(pagy, link_extra='')
  p_prev, p_next, p_vars = pagy.prev, pagy.next, pagy.vars
  marker_url = pagy_url_for(MARKER, pagy)
  page1_url  = pagy_trim_url(marker_url, "#{p_vars[:page_param]}=#{MARKER}")
  page1_link = %(<a href="#{page1_url}" #{p_vars[:link_extra]} #{link_extra})
  a, b = %(<a href="#{marker_url}" #{p_vars[:link_extra]} #{link_extra}).split(MARKER, 2)
  -> (n, text=n, extra='') { start = n.to_i == 1 ? page1_link : "#{a}#{n}#{b}"
                             "#{start}#{ if    n == p_prev ; ' rel="prev"'
                                         elsif n == p_next ; ' rel="next"'
                                         else                           '' end } #{extra}>#{text}</a>" }
end

#pagy_nav(pagy) ⇒ Object

Generic pagination: it returns the html with the series of links to the pages



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pagy/frontend.rb', line 13

def pagy_nav(pagy)
  html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next

  html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
                  : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
  pagy.series.each do |item|  # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    html << if    item.is_a?(Integer); %(<span class="page">#{link.call item}</span> )               # page link
            elsif item.is_a?(String) ; %(<span class="page active">#{item}</span> )                  # current page
            elsif item == :gap       ; %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> )   # page gap
            end
  end
  html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
                  : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
  %(<nav class="pagy-nav pagination" role="navigation" aria-label="pager">#{html}</nav>)
end

#pagy_nav_bootstrap(pagy) ⇒ Object

Pagination for bootstrap: it returns the html with the series of links to the pages



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pagy/extras/bootstrap.rb', line 10

def pagy_nav_bootstrap(pagy)
  html, link, p_prev, p_next = +'', pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next

  html << (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
                  : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    html << if    item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>)                                                               # page link
            elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>)                                                        # active page
            elsif item == :gap       ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
            end
  end
  html << (p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
                  : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
  %(<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></nav>)
end

#pagy_nav_bulma(pagy) ⇒ Object

Pagination for Bulma: it returns the html with the series of links to the pages



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pagy/extras/bulma.rb', line 10

def pagy_nav_bulma(pagy)
  html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next

  html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
                  : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>))
  html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
                  : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
  html << '<ul class="pagination-list">'
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    html << if    item.is_a?(Integer); %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}") }</li>)                           # page link
            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
            elsif item == :gap       ; %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>)                                            # page gap
            end
  end
  html << '</ul>'
  %(<nav class="pagy-nav-bulma pagination is-centered" role="navigation" aria-label="pagination">#{html}</nav>)
end

#pagy_nav_compact(pagy, id = caller(1,1)[0].hash) ⇒ Object

Generic compact pagination: it returns the html with the series of links to the pages we use a numeric input tag to set the page and the Pagy.compact javascript to navigate



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pagy/extras/navs.rb', line 11

def pagy_nav_compact(pagy, id=caller(1,1)[0].hash)
  html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

  html << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact pagination" role="navigation" aria-label="pager">)
    html << link.call(MARKER, '', %(style="display: none;" ))
    (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
    html << (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
                    : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
    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;">)
    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> )
    html << (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
                    : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
  html << %(</nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
end

#pagy_nav_compact_bootstrap(pagy, id = caller(1,1)[0].hash) ⇒ Object

Compact pagination for bootstrap: it returns the html with the series of links to the pages we use a numeric input tag to set the page and the Pagy.compact javascript to navigate



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pagy/extras/bootstrap.rb', line 28

def pagy_nav_compact_bootstrap(pagy, id=caller(1,1)[0].hash)
  html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

  html << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-bootstrap pagination" role="navigation" aria-label="pager">)
    html << link.call(MARKER, '', %(style="display: none;" ))
    (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
    html << %(<div class="btn-group" role="group">)
    html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous" class="prev btn btn-primary"')
                    : %(<a class="prev btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
    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;">)
    html << %(<div class="pagy-compact-input btn btn-primary disabled">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
    html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"')
                    : %(<a class="next btn btn-primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
  html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
end

#pagy_nav_compact_bulma(pagy, id = caller(1,1)[0].hash) ⇒ Object

Compact pagination for Bulma: it returns the html with the series of links to the pages we use a numeric input tag to set the page and the Pagy.compact javascript to navigate



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pagy/extras/bulma.rb', line 30

def pagy_nav_compact_bulma(pagy, id=caller(1,1)[0].hash)
  html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

  html << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-bulma" role="navigation" aria-label="pagination">)
    html << link.call(MARKER, '', 'style="display: none;"')
    (html << link.call(1, '', %(style="display: none;"))) if defined?(TRIM)
    html << %(<div class="field is-grouped is-grouped-centered" role="group">)
    html << (p_prev ? %(<p class="control">#{link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="button" aria-label="previous page"')}</p>)
                    : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.prev')}</a></p>))
    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;">)
    html << %(<div class="pagy-compact-input control level is-mobile">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
    html << (p_next ? %(<p class="control">#{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}</p>)
                    : %(<p class="control"><a class="button" disabled>#{pagy_t('pagy.nav.next')}</a></p>))
  html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
end

#pagy_nav_compact_foundation(pagy, id = caller(1,1)[0].hash) ⇒ Object

Compact pagination for Foundation: it returns the html with the series of links to the pages we use a numeric input tag to set the page and the Pagy.compact javascript to navigate



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pagy/extras/foundation.rb', line 28

def pagy_nav_compact_foundation(pagy, id=caller(1,1)[0].hash)
  html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

  html << %(<nav id="pagy-nav-#{id}" class="pagy-nav-compact-foundation" role="navigation" aria-label="Pagination">)
    html << link.call(MARKER, '', %(style="display: none;" ))
    (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
    html << %(<div class="input-group">)
    html << (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'style="margin-bottom: 0px;" aria-label="previous" class="prev button primary"')
                    : %(<a style="margin-bottom: 0px;" class="prev button primary disabled" href="#">#{pagy_t('pagy.nav.prev')}</a>))
    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;">)
    html << %(<span class="input-group-label">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</span>)
    html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"')
                    : %(<a style="margin-bottom: 0px;" class="next button primary disabled" href="#">#{pagy_t('pagy.nav.next')}</a>))
  html << %(</div></nav><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
end

#pagy_nav_compact_materialize(pagy, id = caller(1,1)[0].hash) ⇒ Object

Compact pagination for materialize: it returns the html with the series of links to the pages we use a numeric input tag to set the page and the Pagy.compact javascript to navigate



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pagy/extras/materialize.rb', line 27

def pagy_nav_compact_materialize(pagy, id=caller(1,1)[0].hash)
  html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages

  html << %(<div id="pagy-nav-#{id}" class="pagy-nav-compact-materialize pagination" role="navigation" aria-label="pager">)
  html << link.call(MARKER, '', %(style="display: none;" ))
  (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
  html << %(<div class="pagy-compact-chip role="group" style="height: 35px; border-radius: 18px; background: #e4e4e4; display: inline-block;">)
  html << '<ul class="pagination" style="margin: 0px;">'
  li_style = 'style="vertical-align: middle;"'
  html << (p_prev ? %(<li class="waves-effect prev" #{li_style}>#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
           : %(<li class="prev disabled" #{li_style}><a href="#"><i class="material-icons">chevron_left</i></a></li>))
  input = %(<input type="number" class="browser-default" min="1" max="#{p_pages}" value="#{p_page}" style="padding: 2px; border: none; border-radius: 2px; text-align: center; width: #{p_pages.to_s.length+1}rem;">)
  html << %(<div class="pagy-compact-input btn-flat" style="cursor: default; padding: 0px">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div>)
  html << (p_next ? %(<li class="waves-effect next" #{li_style}>#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
           : %(<li class="next disabled" #{li_style}><a href="#"><i class="material-icons">chevron_right</i></a></li>))
  html << %(</ul></div><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
end

#pagy_nav_compact_semantic(pagy, id = caller(1,1)[0].hash) ⇒ Object

Compact pagination for semantic: it returns the html with the series of links to the pages we use a numeric input tag to set the page and the Pagy.compact javascript to navigate



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pagy/extras/semantic.rb', line 28

def pagy_nav_compact_semantic(pagy, id=caller(1,1)[0].hash)
  html, link, p_prev, p_next, p_page, p_pages = +'', pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.page, pagy.pages

  html << %(<div id="pagy-nav-#{id}" class="pagy-nav-compact-semantic ui compact menu" role="navigation" aria-label="pager">)
    html << link.call(MARKER, '', %(style="display: none;" ))
    (html << link.call(1, '', %(style="display: none;" ))) if defined?(TRIM)
    html << (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
                    : %(<div class="item disabled"><i class="left small chevron icon"></i></div>))
    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; margin: 0 0.3rem">)
    html << %(<div class="pagy-compact-input item">#{pagy_t('pagy.compact', page_input: input, count: p_page, pages: p_pages)}</div> )
    html << (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
                    : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
  html << %(</div><script type="application/json" class="pagy-compact-json">["#{id}", "#{MARKER}", "#{p_page}", #{!!defined?(TRIM)}]</script>)
end

#pagy_nav_foundation(pagy) ⇒ Object

Pagination for Foundation: it returns the html with the series of links to the pages



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pagy/extras/foundation.rb', line 10

def pagy_nav_foundation(pagy)
  html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next

  html << (p_prev ? %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
                  : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>))
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    html << if    item.is_a?(Integer); %(<li>#{link.call item}</li>)                        # page link
            elsif item.is_a?(String) ; %(<li class="current">#{item}</li>)                  # active page
            elsif item == :gap       ; %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
            end
  end
  html << (p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
                  : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
  %(<nav class="pagy-nav-foundation" role="navigation" aria-label="Pagination"><ul class="pagination">#{html}</ul></nav>)
end

#pagy_nav_materialize(pagy) ⇒ Object

Pagination for materialize: it returns the html with the series of links to the pages



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pagy/extras/materialize.rb', line 10

def pagy_nav_materialize(pagy)
  html, link, p_prev, p_next = +'', pagy_link_proc(pagy), pagy.prev, pagy.next
  html << (p_prev ? %(<li class="waves-effect prev">#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
                  : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>))
  pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    html << if    item.is_a?(Integer); %(<li class="waves-effect">#{link.call item}</li>)                                             # page link
            elsif item.is_a?(String) ; %(<li class="active">#{link.call item}</li>)                                                   # active page
            elsif item == :gap       ; %(<li class="gap disabled"><a href="#">#{pagy_t('pagy.nav.gap')}</a></li>)   # page gap
            end
  end
  html << (p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
                  : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
  %(<div class="pagy-nav-materialize pagination" role="navigation" aria-label="pager"><ul class="pagination">#{html}</ul></div>)
end

#pagy_nav_responsive(pagy, id = caller(1,1)[0].hash) ⇒ Object

Generic responsive pagination: it returns the html with the series of links to the pages rendered by the Pagy.responsive javascript



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pagy/extras/navs.rb', line 28

def pagy_nav_responsive(pagy, id=caller(1,1)[0].hash)
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

  tags['before'] = (p_prev ? %(<span class="page prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</span> )
                           : %(<span class="page prev disabled">#{pagy_t('pagy.nav.prev')}</span> ))
  responsive[:items].each do |item|  # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    tags[item.to_s] = if    item.is_a?(Integer); %(<span class="page">#{link.call item}</span> )             # page link
                      elsif item.is_a?(String) ; %(<span class="page active">#{item}</span> )                # current page
                      elsif item == :gap       ; %(<span class="page gap">#{pagy_t('pagy.nav.gap')}</span> ) # page gap
                      end
  end
  tags['after'] = (p_next ? %(<span class="page next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</span>)
                          : %(<span class="page next disabled">#{pagy_t('pagy.nav.next')}</span>))
  script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json},  #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive pagination" role="navigation" aria-label="pager"></nav>#{script})
end

#pagy_nav_responsive_bootstrap(pagy, id = caller(1,1)[0].hash) ⇒ Object

Responsive pagination for bootstrap: it returns the html with the series of links to the pages rendered by the Pagy.responsive javascript



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pagy/extras/bootstrap.rb', line 46

def pagy_nav_responsive_bootstrap(pagy, id=caller(1,1)[0].hash)
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next, pagy.responsive

  tags['before'] = +'<ul class="pagination">'
  tags['before'] << (p_prev ? %(<li class="page-item prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
                            : %(<li class="page-item prev disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.prev')}</a></li>))
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    tags[item.to_s] = if    item.is_a?(Integer); %(<li class="page-item">#{link.call item}</li>)                                                        # page link
                      elsif item.is_a?(String) ; %(<li class="page-item active">#{link.call item}</li>)                                                 # active page
                      elsif item == :gap       ; %(<li class="page-item gap disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.gap')}</a></li>) # page gap
                      end
  end
  tags['after'] = +(p_next ? %(<li class="page-item next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
                           : %(<li class="page-item next disabled"><a href="#" class="page-link">#{pagy_t('pagy.nav.next')}</a></li>))
  tags['after'] << '</ul>'
  script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json},  #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive-bootstrap pagination" role="navigation" aria-label="pager"></nav>#{script})
end

#pagy_nav_responsive_bulma(pagy, id = caller(1,1)[0].hash) ⇒ Object

Responsive pagination for Bulma: it returns the html with the series of links to the pages rendered by the Pagy.responsive javascript



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pagy/extras/bulma.rb', line 48

def pagy_nav_responsive_bulma(pagy, id=caller(1,1)[0].hash)
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

  tags['before'] = +(p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"')
                            : %(<a class="pagination-previous" disabled>#{pagy_t('pagy.nav.prev')}</a>))
  tags['before'] << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"')
                            : %(<a class="pagination-next" disabled>#{pagy_t('pagy.nav.next')}</a>))
  tags['before'] << '<ul class="pagination-list">'
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    tags[item.to_s] = if    item.is_a?(Integer); %(<li>#{link.call item, item, %(class="pagination-link" aria-label="goto page #{item}")}</li>)
                      elsif item.is_a?(String) ; %(<li>#{link.call item, item, %(class="pagination-link is-current" aria-current="page" aria-label="page #{item}")}</li>)
                      elsif item == :gap       ; %(<li><span class="pagination-ellipsis">#{pagy_t('pagy.nav.gap')}</span></li>)
                      end
  end
  tags['after'] = '</ul>'
  script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json},  #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-bulma pagination is-centered" role="navigation" aria-label="pagination"></nav>#{script})
end

#pagy_nav_responsive_foundation(pagy, id = caller(1,1)[0].hash) ⇒ Object

Responsive pagination for Foundation: it returns the html with the series of links to the pages rendered by the Pagy.responsive javascript



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pagy/extras/foundation.rb', line 46

def pagy_nav_responsive_foundation(pagy, id=caller(1,1)[0].hash)
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

  tags['before'] = +'<ul class="pagination">'
  tags['before'] << (p_prev ? %(<li class="prev">#{link.call p_prev, pagy_t('pagy.nav.prev'), 'aria-label="previous"'}</li>)
                            : %(<li class="prev disabled">#{pagy_t('pagy.nav.prev')}</li>))
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    tags[item.to_s] = if    item.is_a?(Integer); %(<li>#{link.call item}</li>)                        # page link
                      elsif item.is_a?(String) ; %(<li class="current">#{item}</li>)                  # active page
                      elsif item == :gap       ; %(<li class="ellipsis gap" aria-hidden="true"></li>) # page gap
                      end
  end
  tags['after'] = +(p_next ? %(<li class="next">#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}</li>)
                           : %(<li class="next disabled">#{pagy_t('pagy.nav.next')}</li>))
  tags['after'] << '</ul>'
  script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json},  #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
  %(<nav id="pagy-nav-#{id}" class="pagy-nav-responsive-foundation" aria-label="Pagination"></nav>#{script})
end

#pagy_nav_responsive_materialize(pagy, id = caller(1,1)[0].hash) ⇒ Object

Responsive pagination for Materialize: it returns the html with the series of links to the pages rendered by the Pagy.responsive javascript



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pagy/extras/materialize.rb', line 47

def pagy_nav_responsive_materialize(pagy, id=caller(1,1)[0].hash)
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.responsive

  tags['before'] = +'<ul class="pagination">'
  tags['before'] << (p_prev  ? %(<li class="waves-effect prev">#{link.call p_prev, '<i class="material-icons">chevron_left</i>', 'aria-label="previous"'}</li>)
                             : %(<li class="prev disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li>))
  responsive[:items].each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    tags[item.to_s] = if    item.is_a?(Integer); %(<li class="waves-effect">#{link.call item}</li>)                           # page link
                      elsif item.is_a?(String) ; %(<li class="active">#{link.call item}</li>)                                 # active page
                      elsif item == :gap       ; %(<li class="gap disabled"><a href="#">#{pagy_t('pagy.nav.gap')}</a></li>)   # page gap
                      end
  end
  tags['after'] = +(p_next ? %(<li class="waves-effect next">#{link.call p_next, '<i class="material-icons">chevron_right</i>', 'aria-label="next"'}</li>)
                           : %(<li class="next disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li>))
  tags['after'] << '</ul>'
  script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json},  #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
  %(<div id="pagy-nav-#{id}" class="pagy-nav-responsive-materialize pagination" role="navigation" aria-label="pager"></div>#{script})
end

#pagy_nav_responsive_semantic(pagy, id = caller(1,1)[0].hash) ⇒ Object

Responsive pagination for semantic: it returns the html with the series of links to the pages rendered by the Pagy.responsive javascript



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pagy/extras/semantic.rb', line 45

def pagy_nav_responsive_semantic(pagy, id=caller(1,1)[0].hash)
  tags, link, p_prev, p_next, responsive = {}, pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next, pagy.responsive

  tags['before'] = (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
                           : %(<div class="item disabled"><i class="left small chevron icon"></i></div>))
  responsive[:items].each do |item|  # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    tags[item.to_s] = if    item.is_a?(Integer); %(#{link.call item})                      # page link
                      elsif item.is_a?(String) ; %(<a class="item active">#{item}</a>)     # current page
                      elsif item == :gap       ; %(<div class="disabled item">...</div>)   # page gap
                      end
  end
  tags['after'] = (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
                          : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
  script = %(<script type="application/json" class="pagy-responsive-json">["#{id}", #{tags.to_json},  #{responsive[:widths].to_json}, #{responsive[:series].to_json}]</script>)
  %(<div id="pagy-nav-#{id}" class="pagy-nav-responsive-semantic ui pagination menu" role="navigation" aria-label="pager"></div>#{script})
end

#pagy_nav_semantic(pagy) ⇒ Object

Pagination for semantic-ui: it returns the html with the series of links to the pages



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pagy/extras/semantic.rb', line 10

def pagy_nav_semantic(pagy)
  html, link, p_prev, p_next = +'', pagy_link_proc(pagy, 'class="item"'), pagy.prev, pagy.next

  html << (p_prev ? %(#{link.call p_prev, '<i class="left small chevron icon"></i>', 'aria-label="previous"'})
                  : %(<div class="item disabled"><i class="left small chevron icon"></i></div>))
  pagy.series.each do |item|  # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
    html << if    item.is_a?(Integer); %(#{link.call item})                      # page link
            elsif item.is_a?(String) ; %(<a class="item active">#{item}</a>)     # current page
            elsif item == :gap       ; %(<div class="disabled item">...</div>)   # page gap
            end
  end
  html << (p_next ? %(#{link.call p_next, '<i class="right small chevron icon"></i>', 'aria-label="next"'})
                  : %(<div class="item disabled"><i class="right small chevron icon"></i></div>))
  %(<div class="ui pagination menu" aria-label="pager">#{html}</div>)
end

#pagy_t(*args) ⇒ Object

Override the built-in pagy_t



66
67
68
69
70
71
72
73
74
75
# File 'lib/pagy/frontend.rb', line 66

def pagy_t(path, vars={})
  value = I18N[:data].dig(*path.split('.')) or return %(translation missing: "#{path}")
  if value.is_a?(Hash)
    vars.key?(:count) or return value
    plural = I18N[:plural].call(vars[:count])
    value.key?(plural) or return %(invalid pluralization data: "#{path}" cannot be used with count: #{vars[:count]}; key "#{plural}" is missing.)
    value = value[plural] or return %(translation missing: "#{path}")
  end
  sprintf value, Hash.new{|_,k| "%{#{k}}"}.merge!(vars)    # interpolation
end

#pagy_url_for_with_items(page, pagy) ⇒ Object Also known as: pagy_url_for



26
27
28
29
# File 'lib/pagy/extras/items.rb', line 26

def pagy_url_for_with_items(page, pagy)
  p_vars = pagy.vars; params = request.GET.merge(p_vars[:page_param] => page, p_vars[:items_param] => p_vars[:items]).merge!(p_vars[:params])
  "#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
end