Class: GovukComponent::PaginationComponent

Inherits:
Base
  • Object
show all
Includes:
Pagy::UrlHelpers
Defined in:
app/components/govuk_component/pagination_component.rb

Defined Under Namespace

Classes: AdjacentPage, Item, NextPage, PreviousPage

Instance Attribute Summary collapse

Attributes inherited from Base

#html_attributes

Instance Method Summary collapse

Methods inherited from Base

#brand, #class_prefix

Constructor Details

#initialize(pagy: nil, next_text: nil, previous_text: nil, block_mode: false, landmark_label: config.default_pagination_landmark_label, classes: [], html_attributes: {}) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/components/govuk_component/pagination_component.rb', line 39

def initialize(pagy: nil,
               next_text: nil,
               previous_text: nil,
               block_mode: false,
               landmark_label: config.default_pagination_landmark_label,
               classes: [],
               html_attributes: {})
  @pagy                          = pagy
  @next_text                     = next_text
  @previous_text                 = previous_text
  @block_mode                    = block_mode
  @landmark_label                = landmark_label

  super(classes:, html_attributes:)
end

Instance Attribute Details

#block_modeObject (readonly) Also known as: block_mode?

Returns the value of attribute block_mode.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def block_mode
  @block_mode
end

#landmark_labelObject (readonly)

Returns the value of attribute landmark_label.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def landmark_label
  @landmark_label
end

#next_contentObject (readonly)

Returns the value of attribute next_content.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def next_content
  @next_content
end

#next_textObject (readonly)

Returns the value of attribute next_text.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def next_text
  @next_text
end

#page_itemsObject (readonly)

Returns the value of attribute page_items.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def page_items
  @page_items
end

#pagyObject (readonly)

Returns the value of attribute pagy.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def pagy
  @pagy
end

#previous_contentObject (readonly)

Returns the value of attribute previous_content.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def previous_content
  @previous_content
end

#previous_textObject (readonly)

Returns the value of attribute previous_text.



4
5
6
# File 'app/components/govuk_component/pagination_component.rb', line 4

def previous_text
  @previous_text
end

Instance Method Details

#before_renderObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/components/govuk_component/pagination_component.rb', line 55

def before_render
  @page_items = if pagy.present?
                  build_items
                elsif items.any?
                  items
                else
                  []
                end

  @previous_content = previous_page || build_previous
  @next_content     = next_page || build_next
end

#callObject



68
69
70
71
72
73
74
75
76
77
78
# File 'app/components/govuk_component/pagination_component.rb', line 68

def call
  attributes = html_attributes.tap { |ha| (ha[:class] << "#{brand}-pagination--block") if items.empty? }

  tag.nav(**attributes) do
    safe_join([
      previous_content,
      tag.ul(class: "#{brand}-pagination__list") { safe_join(page_items) },
      next_content
    ])
  end
end

#render?Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'app/components/govuk_component/pagination_component.rb', line 80

def render?
  # probably isn't any point rendering if there's only one page
  (pagy.present? && pagy.series.size > 1) || @previous_content.present? || @next_content.present?
end