Module: Pagy::Frontend
- Includes:
- BootstrapExtra, BulmaExtra, CalendarExtra::UrlHelperAddOn, FoundationExtra, I18nExtra::FrontendOverride, ItemsExtra::FrontendAddOn, JSTools::FrontendAddOn, MaterializeExtra, PagyExtra, SemanticExtra, TrimExtra, UikitExtra, UrlHelpers
- Defined in:
- lib/pagy/frontend.rb
Overview
Frontend modules are specially optimized for performance. The resulting code may not look very elegant, but produces the best benchmarks
Constant Summary
Constants included from ItemsExtra::FrontendAddOn
ItemsExtra::FrontendAddOn::ITEMS_TOKEN
Instance Method Summary collapse
-
#pagy_anchor(pagy) ⇒ Object
Return a performance optimized lambda to generate the HtML anchor element (a tag) Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails’ link_to.
-
#pagy_info(pagy, id: nil, item_name: nil) ⇒ Object
Return examples: “Displaying items 41-60 of 324 in total” or “Displaying Products 41-60 of 324 in total”.
-
#pagy_nav(pagy, id: nil, aria_label: nil, **vars) ⇒ Object
Generic pagination: it returns the html with the series of links to the pages.
-
#pagy_t(key, opts = {}) ⇒ Object
Similar to I18n.t: just ~18x faster using ~10x less memory (@pagy_locale explicitly initialized in order to avoid warning).
Methods included from UikitExtra
#pagy_uikit_combo_nav_js, #pagy_uikit_nav, #pagy_uikit_nav_js
Methods included from UrlHelpers
#pagy_set_query_params, #pagy_url_for
Methods included from JsonApiExtra::UrlHelperOverride
Methods included from StandaloneExtra
Methods included from PagyExtra
#pagy_combo_nav_js, #pagy_nav_js, #pagy_next_a, #pagy_next_link, #pagy_next_url, #pagy_prev_a, #pagy_prev_link, #pagy_prev_url
Methods included from TrimExtra
Methods included from BulmaExtra
#pagy_bulma_combo_nav_js, #pagy_bulma_nav, #pagy_bulma_nav_js
Methods included from ItemsExtra::FrontendAddOn
Methods included from CalendarExtra::UrlHelperAddOn
Methods included from JSTools::FrontendAddOn
Methods included from SemanticExtra
#pagy_semantic_combo_nav_js, #pagy_semantic_nav, #pagy_semantic_nav_js
Methods included from BootstrapExtra
#pagy_bootstrap_combo_nav_js, #pagy_bootstrap_nav, #pagy_bootstrap_nav_js
Methods included from FoundationExtra
#pagy_foundation_combo_nav_js, #pagy_foundation_nav, #pagy_foundation_nav_js
Methods included from MaterializeExtra
#pagy_materialize_combo_nav_js, #pagy_materialize_nav, #pagy_materialize_nav_js
Instance Method Details
#pagy_anchor(pagy) ⇒ Object
Return a performance optimized lambda to generate the HtML anchor element (a tag) Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails’ link_to
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pagy/frontend.rb', line 56 def pagy_anchor(pagy) a_string = pagy.vars[:anchor_string] a_string = %( #{a_string}) if a_string left, right = %(<a#{a_string} href="#{pagy_url_for(pagy, PAGE_TOKEN)}").split(PAGE_TOKEN, 2) # lambda used by all the helpers lambda do |page, text = pagy.label_for(page), classes: nil, aria_label: nil| classes = %( class="#{classes}") if classes aria_label = %( aria-label="#{aria_label}") if aria_label %(#{left}#{page}#{right}#{classes}#{aria_label}>#{text}</a>) end end |
#pagy_info(pagy, id: nil, item_name: nil) ⇒ Object
Return examples: “Displaying items 41-60 of 324 in total” or “Displaying Products 41-60 of 324 in total”
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pagy/frontend.rb', line 40 def pagy_info(pagy, id: nil, item_name: nil) id = %( id="#{id}") if id p_count = pagy.count key = if p_count.zero? then 'pagy.info.no_items' elsif pagy.pages == 1 then 'pagy.info.single_page' else 'pagy.info.multiple_pages' # rubocop:disable Lint/ElseLayout end %(<span#{id} class="pagy info">#{ pagy_t key, item_name: item_name || pagy_t('pagy.item_name', count: p_count), count: p_count, from: pagy.from, to: pagy.to }</span>) end |
#pagy_nav(pagy, id: nil, aria_label: nil, **vars) ⇒ Object
Generic pagination: it returns the html with the series of links to the pages
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pagy/frontend.rb', line 18 def pagy_nav(pagy, id: nil, aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy) html = %(<nav#{id} class="pagy nav" #{nav_aria_label(pagy, aria_label:)}>#{ prev_a(pagy, a)}) pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] html << case item when Integer a.(item) when String %(<a role="link" aria-disabled="true" aria-current="page" class="current">#{pagy.label_for(item)}</a>) when :gap %(<a role="link" aria-disabled="true" class="gap">#{pagy_t('pagy.gap')}</a>) else raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}" end end html << %(#{next_a(pagy, a)}</nav>) end |