From 4d48d3322b879197af3a6862c3e1e98c3c6555cf Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Wed, 10 Mar 2021 14:03:26 +0700 Subject: [PATCH] refactoring of pagy_json_tag --- lib/pagy/extras/bootstrap.rb | 4 ++-- lib/pagy/extras/bulma.rb | 4 ++-- lib/pagy/extras/foundation.rb | 4 ++-- lib/pagy/extras/items.rb | 2 +- lib/pagy/extras/materialize.rb | 4 ++-- lib/pagy/extras/navs.rb | 4 ++-- lib/pagy/extras/semantic.rb | 4 ++-- lib/pagy/extras/shared.rb | 10 ++++++++-- lib/pagy/extras/uikit.rb | 4 ++-- test/pagy/extras/shared_test.rb | 4 ++-- 10 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/pagy/extras/bootstrap.rb b/lib/pagy/extras/bootstrap.rb index 72123abf9..d5c3b8da8 100644 --- a/lib/pagy/extras/bootstrap.rb +++ b/lib/pagy/extras/bootstrap.rb @@ -34,7 +34,7 @@ def pagy_bootstrap_nav_js(pagy, id=pagy_id) 'gap' => %(
  • #{pagy_t('pagy.nav.gap')}
  • ), 'after' => p_next ? %() : %() } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{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 @@ -48,7 +48,7 @@ def pagy_bootstrap_combo_nav_js(pagy, id=pagy_id) html << %(
    #{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}
    ) html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'aria-label="next" class="next btn btn-primary"') : %()) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end diff --git a/lib/pagy/extras/bulma.rb b/lib/pagy/extras/bulma.rb index 0253c5b2c..ccbd793a4 100644 --- a/lib/pagy/extras/bulma.rb +++ b/lib/pagy/extras/bulma.rb @@ -38,7 +38,7 @@ def pagy_bulma_nav_js(pagy, id=pagy_id) 'active' => %(
  • #{link.call(PAGE_PLACEHOLDER, PAGE_PLACEHOLDER, %(class="pagination-link is-current" aria-current="page" aria-label="page #{PAGE_PLACEHOLDER}"))}
  • ), 'gap' => %(
  • #{pagy_t('pagy.nav.gap')}
  • ), 'after' => '' } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)}) end # Javascript combo pagination for Bulma: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript @@ -53,7 +53,7 @@ def pagy_bulma_combo_nav_js(pagy, id=pagy_id) html << %(
    #{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}
    ) html << (p_next ? %(

    #{link.call(p_next, pagy_t('pagy.nav.next'), 'class="button" aria-label="next page"')}

    ) : %(

    #{pagy_t('pagy.nav.next')}

    )) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end diff --git a/lib/pagy/extras/foundation.rb b/lib/pagy/extras/foundation.rb index 9b9fed36c..dcefc3d09 100644 --- a/lib/pagy/extras/foundation.rb +++ b/lib/pagy/extras/foundation.rb @@ -36,7 +36,7 @@ def pagy_foundation_nav_js(pagy, id=pagy_id) 'after' => ( (p_next ? %() : %()) \ + '' ) } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)}) end # Javascript combo pagination for Foundation: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript @@ -50,7 +50,7 @@ def pagy_foundation_combo_nav_js(pagy, id=pagy_id) html << %(#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}) html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'style="margin-bottom: 0px;" aria-label="next" class="next button primary"') : %()) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end diff --git a/lib/pagy/extras/items.rb b/lib/pagy/extras/items.rb index 139fcf5f5..e262a5270 100644 --- a/lib/pagy/extras/items.rb +++ b/lib/pagy/extras/items.rb @@ -50,7 +50,7 @@ def pagy_items_selector_js(pagy, id=pagy_id) html = %() input = %() html << %(#{pagy_t('pagy.items_selector_js', item_name: pagy_t(p_vars[:i18n_key], count: p_items), items_input: input, count: p_items)}) - html << %(#{pagy_json_tag(:items_selector, id, pagy.from, link, defined?(Trim) && p_vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :items_selector, id, pagy.from, link)}) end end diff --git a/lib/pagy/extras/materialize.rb b/lib/pagy/extras/materialize.rb index 50a70ba29..a9ccfb532 100644 --- a/lib/pagy/extras/materialize.rb +++ b/lib/pagy/extras/materialize.rb @@ -35,7 +35,7 @@ def pagy_materialize_nav_js(pagy, id=pagy_id) 'after' => ( (p_next ? %() : %()) \ + '' ) } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)}) end # Javascript combo pagination for materialize: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript @@ -52,7 +52,7 @@ def pagy_materialize_combo_nav_js(pagy, id=pagy_id) html << %(
    #{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}
    ) html << (p_next ? %() : %()) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end diff --git a/lib/pagy/extras/navs.rb b/lib/pagy/extras/navs.rb index 9e905a2e1..35c8612c7 100644 --- a/lib/pagy/extras/navs.rb +++ b/lib/pagy/extras/navs.rb @@ -17,7 +17,7 @@ def pagy_nav_js(pagy, id=pagy_id) 'gap' => %(#{pagy_t('pagy.nav.gap')} ), 'after' => p_next ? %(#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}) : %(#{pagy_t('pagy.nav.next')}) } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)}) end # Javascript combo pagination: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript @@ -31,7 +31,7 @@ def pagy_combo_nav_js(pagy, id=pagy_id) html << %(#{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)} ) html << (p_next ? %(#{link.call p_next, pagy_t('pagy.nav.next'), 'aria-label="next"'}) : %(#{pagy_t('pagy.nav.next')})) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end diff --git a/lib/pagy/extras/semantic.rb b/lib/pagy/extras/semantic.rb index ce6f5bf2c..55685be51 100644 --- a/lib/pagy/extras/semantic.rb +++ b/lib/pagy/extras/semantic.rb @@ -34,7 +34,7 @@ def pagy_semantic_nav_js(pagy, id=pagy_id) 'gap' => %(
    #{pagy_t('pagy.nav.gap')}
    ), 'after' => (p_next ? %(#{link.call(p_next, '', 'aria-label="next"')}) : %(
    )) } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)}) end # Combo pagination for semantic: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript @@ -48,7 +48,7 @@ def pagy_semantic_combo_nav_js(pagy, id=pagy_id) html << %(
    #{pagy_t('pagy.combo_nav_js', page_input: input, count: p_page, pages: p_pages)}
    ) html << (p_next ? %(#{link.call p_next, '', 'aria-label="next"'}) : %(
    )) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end diff --git a/lib/pagy/extras/shared.rb b/lib/pagy/extras/shared.rb index d071f726f..57c1d3b76 100644 --- a/lib/pagy/extras/shared.rb +++ b/lib/pagy/extras/shared.rb @@ -27,11 +27,17 @@ module Frontend if defined?(Oj) # it returns a script tag with the JSON-serialized args generated with the faster oj gem - def pagy_json_tag(*args) = %() + def pagy_json_tag(pagy, *args) + args << ( defined?(Trim) && pagy.vars[:page_param] ) + %() + end else require 'json' # it returns a script tag with the JSON-serialized args generated with the slower to_json - def pagy_json_tag(*args) = %() + def pagy_json_tag(pagy, *args) + args << ( defined?(Trim) && pagy.vars[:page_param] ) + %() + end end # it returns the SHA1 (fastest on modern ruby) string used as default `id` attribute by all the `*_js` tags diff --git a/lib/pagy/extras/uikit.rb b/lib/pagy/extras/uikit.rb index 3c69a16b6..12b762567 100644 --- a/lib/pagy/extras/uikit.rb +++ b/lib/pagy/extras/uikit.rb @@ -38,7 +38,7 @@ def pagy_uikit_nav_js(pagy, id=pagy_id) 'gap' => %(
  • #{pagy_t('pagy.nav.gap')}
  • ), 'after' => p_next ? %(
  • #{link.call p_next, next_span}
  • ) : %(
  • #{next_span}
  • ) } - %(#{pagy_json_tag(:nav, id, tags, pagy.sequels, defined?(Trim) && pagy.vars[:page_param])}) + %(#{pagy_json_tag(pagy, :nav, id, tags, pagy.sequels)}) end # Javascript combo pagination for uikit: it returns a nav and a JSON tag used by the Pagy.combo_nav javascript @@ -56,7 +56,7 @@ def pagy_uikit_combo_nav_js(pagy, id=pagy_id) html << (p_next ? link.call(p_next, pagy_t('pagy.nav.next'), 'class="uk-button uk-button-default"') : %()) - html << %(#{pagy_json_tag(:combo_nav, id, p_page, pagy_marked_link(link), defined?(Trim) && pagy.vars[:page_param])}) + html << %(#{pagy_json_tag(pagy, :combo_nav, id, p_page, pagy_marked_link(link))}) end end end diff --git a/test/pagy/extras/shared_test.rb b/test/pagy/extras/shared_test.rb index 7d33e011e..adbf62ab8 100644 --- a/test/pagy/extras/shared_test.rb +++ b/test/pagy/extras/shared_test.rb @@ -17,8 +17,8 @@ describe "#pagy_json_tag" do it 'should use oj' do - _(view.pagy_json_tag(:test_function, 'some-id', 'some-string', 123, true)).must_equal \ - "" + _(view.pagy_json_tag(Pagy.new(count: 10), :test_function, 'some-id', 'some-string', 123, true)).must_equal \ + "" end end