Class: GovukPublishingComponents::Presenters::PaginationHelper
- Inherits:
-
Object
- Object
- GovukPublishingComponents::Presenters::PaginationHelper
- Includes:
- ActionView::Context, ActionView::Helpers
- Defined in:
- lib/govuk_publishing_components/presenters/pagination_helper.rb
Instance Attribute Summary collapse
-
#ga4_type ⇒ Object
readonly
Returns the value of attribute ga4_type.
-
#next_link ⇒ Object
readonly
Returns the value of attribute next_link.
-
#page_links ⇒ Object
readonly
Returns the value of attribute page_links.
-
#prev_link ⇒ Object
readonly
Returns the value of attribute prev_link.
Instance Method Summary collapse
- #arrow_link_helper(direction:) ⇒ Object
- #ga4_link_event(text:, section:) ⇒ Object
- #has_links? ⇒ Boolean
- #has_pages? ⇒ Boolean
-
#initialize(local_assigns) ⇒ PaginationHelper
constructor
A new instance of PaginationHelper.
- #pages_link_helper ⇒ Object
Constructor Details
#initialize(local_assigns) ⇒ PaginationHelper
Returns a new instance of PaginationHelper.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 9 def initialize(local_assigns) @arrow_links = { next: local_assigns[:next_page] || nil, prev: local_assigns[:previous_page] || nil, } @items = local_assigns[:items] || nil @disable_ga4 = local_assigns[:disable_ga4] || nil @ga4_type = has_pages? ? "pagination" : "previous and next" @prev_link = arrow_link_helper(direction: "prev") @next_link = arrow_link_helper(direction: "next") @page_links = pages_link_helper end |
Instance Attribute Details
#ga4_type ⇒ Object (readonly)
Returns the value of attribute ga4_type.
7 8 9 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 7 def ga4_type @ga4_type end |
#next_link ⇒ Object (readonly)
Returns the value of attribute next_link.
7 8 9 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 7 def next_link @next_link end |
#page_links ⇒ Object (readonly)
Returns the value of attribute page_links.
7 8 9 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 7 def page_links @page_links end |
#prev_link ⇒ Object (readonly)
Returns the value of attribute prev_link.
7 8 9 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 7 def prev_link @prev_link end |
Instance Method Details
#arrow_link_helper(direction:) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 73 def arrow_link_helper(direction:) = @arrow_links[direction.to_sym] return if .blank? || !valid_link?() { href: nil, label: nil, title: nil, icon: nil, **.symbolize_keys } => { href:, label:, title:, icon: } title ||= { "prev": t("components.pagination.previous"), "next": t("components.pagination.next"), }[direction.to_sym] section = { prev: "Previous", next: "Next", }[direction.to_sym] icon = { prev: previous_icon, next: next_icon, }[direction.to_sym] ga4_link = ga4_link_event(text: label || title, section:) link_text_classes = %w[govuk-pagination__link-title] link_text_classes << "govuk-pagination__link-title--decorated" if label.blank? content_tag(:div, class: "govuk-pagination__#{direction}") do content_tag(:a, href:, class: "govuk-link govuk-pagination__link", data: { ga4_link: }) do concat icon if direction == "prev" || !has_pages? && direction == "next" concat content_tag(:span, title, class: link_text_classes) concat icon if has_pages? && direction == "next" concat content_tag(:span, ":", class: "govuk-visually-hidden") if label.present? concat content_tag(:span, label, class: "govuk-pagination__link-label") if label.present? end end end |
#ga4_link_event(text:, section:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 24 def ga4_link_event(text:, section:) unless @disable_ga4 { event_name: "navigation", type: @ga4_type, text:, section:, }.to_json end end |
#has_links? ⇒ Boolean
35 36 37 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 35 def has_links? has_pages? || @prev_link.present? || @next_link.present? end |
#has_pages? ⇒ Boolean
39 40 41 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 39 def has_pages? @items.present? end |
#pages_link_helper ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/govuk_publishing_components/presenters/pagination_helper.rb', line 43 def pages_link_helper return unless has_pages? page_links = @items.each_with_index.map do |item, index| { href: nil, ellipsis: nil, current: nil, number: nil, visually_hidden_text: nil, **item.symbolize_keys } => { href:, ellipsis:, current:, number:, visually_hidden_text: } raise ArgumentError, "Number or ellipsis value required for item #{index}" if ellipsis.blank? && number.blank? list_item_classes = %w[govuk-pagination__item] list_item_classes << "govuk-pagination__item--ellipsis" if ellipsis list_item_classes << "govuk-pagination__item--current" if current item_aria_label = visually_hidden_text || "Page #{number}" ga4_link = ga4_link_event(text: number, section: "Pagination list") content_tag(:li, class: list_item_classes) do if ellipsis "⋯".html_safe else content_tag(:a, number, class: "govuk-link govuk-pagination__link", href:, aria: { label: item_aria_label, current: current ? "page" : nil }, data: { ga4_link: }) end end end content_tag(:ul, class: "govuk-pagination__list") do page_links.collect { |page_link| concat(page_link) } end end |