From cb673530eca0916771d9f4eebe56899f9eac9ad8 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Thu, 8 Apr 2021 20:13:10 +0700 Subject: [PATCH] code-restyling: extras --- lib/pagy.rb | 28 ++++----- lib/pagy/countless.rb | 1 + lib/pagy/extras/arel.rb | 6 +- lib/pagy/extras/array.rb | 6 +- lib/pagy/extras/bootstrap.rb | 81 ++++++++++++++++-------- lib/pagy/extras/bulma.rb | 86 +++++++++++++++---------- lib/pagy/extras/countless.rb | 9 +-- lib/pagy/extras/elasticsearch_rails.rb | 13 ++-- lib/pagy/extras/foundation.rb | 81 +++++++++++++++--------- lib/pagy/extras/headers.rb | 22 ++++--- lib/pagy/extras/i18n.rb | 7 ++- lib/pagy/extras/items.rb | 29 +++++---- lib/pagy/extras/materialize.rb | 80 ++++++++++++++--------- lib/pagy/extras/metadata.rb | 45 +++++++------ lib/pagy/extras/navs.rb | 55 +++++++++++----- lib/pagy/extras/overflow.rb | 13 ++-- lib/pagy/extras/searchkick.rb | 15 ++--- lib/pagy/extras/semantic.rb | 75 ++++++++++++++-------- lib/pagy/extras/shared.rb | 18 +++--- lib/pagy/extras/support.rb | 15 +++-- lib/pagy/extras/trim.rb | 14 +++-- lib/pagy/extras/uikit.rb | 87 +++++++++++++++++--------- lib/pagy/frontend.rb | 23 ++++--- test/pagy/extras/semantic_test.rb | 1 + 24 files changed, 499 insertions(+), 311 deletions(-) diff --git a/lib/pagy.rb b/lib/pagy.rb index 6084658a9..5c7d79b0d 100644 --- a/lib/pagy.rb +++ b/lib/pagy.rb @@ -45,22 +45,22 @@ def series(size=@vars[:size]) raise VariableError.new(self), "expected 4 items >= 0 in :size; got #{size.inspect}" \ unless size.size == 4 && size.all?{ |num| num >= 0 rescue false } # rubocop:disable Style/RescueModifier - [].tap do |series| - [ *0..size[0], # initial pages from 0 - *@page-size[1]..@page+size[2], # around current page - *@last-size[3]+1..@last+1 # final pages till @last+1 - ].sort!.each_cons(2) do |left, right| # sort and loop by 2 - next if left.negative? || left == right # skip out of range and duplicates - break if left > @last # break if out of @last boundary - case right - when left+1 then series.push(left) # no gap -> no additions - when left+2 then series.push(left, left+1) # 1 page gap -> fill with missing page - else series.push(left, :gap) # n page gap -> add gap - end + series = [] + [ *0..size[0], # initial pages from 0 + *@page-size[1]..@page+size[2], # around current page + *@last-size[3]+1..@last+1 # final pages till @last+1 + ].sort!.each_cons(2) do |left, right| # sort and loop by 2 + next if left.negative? || left == right # skip out of range and duplicates + break if left > @last # break if out of @last boundary + case right + when left+1 then series.push(left) # no gap -> no additions + when left+2 then series.push(left, left+1) # 1 page gap -> fill with missing page + else series.push(left, :gap) # n page gap -> add gap end - series.shift # shift the start boundary (0) - series[series.index(@page)] = @page.to_s # convert the current page to String end + series.shift # shift the start boundary (0) + series[series.index(@page)] = @page.to_s # convert the current page to String + series end end diff --git a/lib/pagy/countless.rb b/lib/pagy/countless.rb index fa7277bfc..3ea12c46d 100644 --- a/lib/pagy/countless.rb +++ b/lib/pagy/countless.rb @@ -22,6 +22,7 @@ def initialize(vars={}) # rubocop:disable Lint/MissingSuper def finalize(fetched) raise OverflowError.new(self), "page #{@page} got no items" \ if fetched.zero? && @page > 1 + @pages = @last = (fetched > @items ? @page + 1 : @page) # set the @pages and @last @items = fetched if fetched < @items && fetched.positive? # adjust items for last non-empty page @from = fetched.zero? ? 0 : @offset + 1 - @outset # page begins from item diff --git a/lib/pagy/extras/arel.rb b/lib/pagy/extras/arel.rb index 3e6dfaef9..b92fd2233 100644 --- a/lib/pagy/extras/arel.rb +++ b/lib/pagy/extras/arel.rb @@ -1,13 +1,13 @@ # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/arel -# encoding: utf-8 # frozen_string_literal: true class Pagy - module Backend ; private + module Backend + private def pagy_arel(collection, vars={}) pagy = Pagy.new(pagy_arel_get_vars(collection, vars)) - return pagy, pagy_get_items(collection, pagy) + [ pagy, pagy_get_items(collection, pagy) ] end def pagy_arel_get_vars(collection, vars) diff --git a/lib/pagy/extras/array.rb b/lib/pagy/extras/array.rb index 35870cb93..3dc08621d 100644 --- a/lib/pagy/extras/array.rb +++ b/lib/pagy/extras/array.rb @@ -1,15 +1,15 @@ # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/array -# encoding: utf-8 # frozen_string_literal: true class Pagy # Add specialized backend methods to paginate array collections - module Backend ; private + module Backend + private # Return Pagy object and items def pagy_array(array, vars={}) pagy = Pagy.new(pagy_array_get_vars(array, vars)) - return pagy, array[pagy.offset, pagy.items] + [ pagy, array[pagy.offset, pagy.items] ] end # Sub-method called only by #pagy_array: here for easy customization of variables by overriding diff --git a/lib/pagy/extras/bootstrap.rb b/lib/pagy/extras/bootstrap.rb index d5c3b8da8..89a008414 100644 --- a/lib/pagy/extras/bootstrap.rb +++ b/lib/pagy/extras/bootstrap.rb @@ -1,5 +1,4 @@ # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bootstrap -# encoding: utf-8 # frozen_string_literal: true require 'pagy/extras/shared' @@ -9,47 +8,75 @@ module Frontend # Pagination for bootstrap: it returns the html with the series of links to the pages def pagy_bootstrap_nav(pagy) - link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next + link = pagy_link_proc(pagy, 'class="page-link"') - html = (p_prev ? %() - : %()) + html = +%() end # Javascript pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.nav javascript def pagy_bootstrap_nav_js(pagy, id=pagy_id) - link, p_prev, p_next = pagy_link_proc(pagy, 'class="page-link"'), pagy.prev, pagy.next - tags = { 'before' => p_prev ? %() } + + html = %() + html << pagy_json_tag(pagy, :nav, id, tags, pagy.sequels) end # Javascript combo pagination for bootstrap: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript def pagy_bootstrap_combo_nav_js(pagy, id=pagy_id) - link, p_prev, p_next, p_page, p_pages = pagy_link_proc(pagy), pagy.prev, pagy.next, pagy.page, pagy.pages - - html = %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) + link = pagy_link_proc(pagy) + p_page = pagy.page + p_pages = pagy.pages + input = %() + + %(#{ + pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link)) + }) end + private + + def pagy_bootstrap_prev_html(pagy, link) + if (p_prev = pagy.prev) + %() + else + %() + end + end + + def pagy_bootstrap_next_html(pagy, link) + if (p_next = pagy.next) + %() + else + %() + end + end + end end diff --git a/lib/pagy/extras/bulma.rb b/lib/pagy/extras/bulma.rb index ccbd793a4..51f03da3b 100644 --- a/lib/pagy/extras/bulma.rb +++ b/lib/pagy/extras/bulma.rb @@ -1,5 +1,4 @@ # See the Pagy documentation: https://ddnexus.github.io/pagy/extras/bulma -# encoding: utf-8 # frozen_string_literal: true require 'pagy/extras/shared' @@ -9,52 +8,71 @@ module Frontend # Pagination for Bulma: it returns the html with the series of links to the pages def pagy_bulma_nav(pagy) - link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next + link = pagy_link_proc(pagy) - html = (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"') - : %(#{pagy_t('pagy.nav.prev')})) \ - + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"') - : %(#{pagy_t('pagy.nav.next')})) - html << ') end - # Javascript pagination for bulma: it returns a nav and a JSON tag used by the Pagy.nav javascript def pagy_bulma_nav_js(pagy, id=pagy_id) - link, p_prev, p_next = pagy_link_proc(pagy), pagy.prev, pagy.next - tags = { 'before' => ( (p_prev ? link.call(p_prev, pagy_t('pagy.nav.prev'), 'class="pagination-previous" aria-label="previous page"') - : %(#{pagy_t('pagy.nav.prev')})) \ - + (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="pagination-next" aria-label="next page"') - : %(#{pagy_t('pagy.nav.next')})) \ - + '