Class: Uchi::Ui::Pagination
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Uchi::Ui::Pagination
- Defined in:
- app/components/uchi/ui/pagination.rb,
app/components/uchi/ui/pagination/gap.rb,
app/components/uchi/ui/pagination/item.rb,
app/components/uchi/ui/pagination/link.rb,
app/components/uchi/ui/pagination/next_link.rb,
app/components/uchi/ui/pagination/current_link.rb,
app/components/uchi/ui/pagination/previous_link.rb
Defined Under Namespace
Classes: CurrentLink, Gap, Item, Link, NextLink, PreviousLink
Instance Attribute Summary collapse
-
#paginator ⇒ Object
readonly
Returns the value of attribute paginator.
Instance Method Summary collapse
- #aria_label ⇒ Object
-
#initialize(paginator:) ⇒ Pagination
constructor
A new instance of Pagination.
-
#page_url(page) ⇒ Object
Returns the URL for a given page.
-
#pages ⇒ Object
Returns an array of page numbers to display in the pagination component.
- #render? ⇒ Boolean
Constructor Details
#initialize(paginator:) ⇒ Pagination
Returns a new instance of Pagination.
13 14 15 16 |
# File 'app/components/uchi/ui/pagination.rb', line 13 def initialize(paginator:) super() @paginator = paginator end |
Instance Attribute Details
#paginator ⇒ Object (readonly)
Returns the value of attribute paginator.
6 7 8 |
# File 'app/components/uchi/ui/pagination.rb', line 6 def paginator @paginator end |
Instance Method Details
#aria_label ⇒ Object
8 9 10 |
# File 'app/components/uchi/ui/pagination.rb', line 8 def aria_label "Page navigation" end |
#page_url(page) ⇒ Object
Returns the URL for a given page. page can be a number, of :first, :last, :previous, :next, :current.
20 21 22 |
# File 'app/components/uchi/ui/pagination.rb', line 20 def page_url(page) paginator.page_url(page) end |
#pages ⇒ Object
Returns an array of page numbers to display in the pagination component
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/components/uchi/ui/pagination.rb', line 25 def pages pages = [] pages += [1] pages += (paginator.page - 2..paginator.page + 2).to_a pages += [paginator.last] pages = pages.select { |page| page.between?(1, paginator.last) }.uniq.sort result = [] pages.each_with_index do |page, index| result << page next_page = pages[index + 1] if index < pages.size - 1 && next_page - page > 1 result << nil # Add a gap end end result end |
#render? ⇒ Boolean
43 44 45 |
# File 'app/components/uchi/ui/pagination.rb', line 43 def render? paginator.present? && paginator.last > 1 end |