Class: BulmaPhlex::Pagination
- Defined in:
- lib/bulma_phlex/pagination.rb
Overview
Renders the [Bulma pagination](bulma.io/documentation/components/pagination/) component.
Accepts a pager object and a path builder callable. Automatically renders previous/next links, numbered page links with ellipses for skipped ranges, and a summary of items being displayed. Only renders when there is more than one page of results.
## Example
# In a controller action:
@products = Product.page(params[:page]).per(20)
# In the view:
render BulmaPhlex::Pagination.new(@products, ->(page) { products_path(page: page) })
Instance Attribute Summary collapse
-
#pager ⇒ Object
readonly
Returns the value of attribute pager.
-
#path_builder ⇒ Object
readonly
Returns the value of attribute path_builder.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(pager, path_builder, **html_attributes) ⇒ Pagination
constructor
A new instance of Pagination.
- #view_template ⇒ Object
Constructor Details
#initialize(pager, path_builder, **html_attributes) ⇒ Pagination
Returns a new instance of Pagination.
31 32 33 34 35 |
# File 'lib/bulma_phlex/pagination.rb', line 31 def initialize(pager, path_builder, **html_attributes) @pager = pager @path_builder = path_builder @html_attributes = html_attributes end |
Instance Attribute Details
#pager ⇒ Object (readonly)
Returns the value of attribute pager.
19 20 21 |
# File 'lib/bulma_phlex/pagination.rb', line 19 def pager @pager end |
#path_builder ⇒ Object (readonly)
Returns the value of attribute path_builder.
19 20 21 |
# File 'lib/bulma_phlex/pagination.rb', line 19 def path_builder @path_builder end |
Class Method Details
.new(pager, path_builder, **html_attributes) ⇒ Object
Parameters
-
‘pager` — A page object responding to `current_page`, `total_pages`, `per_page`, `total_count`, `previous_page`, and `next_page`
-
‘path_builder` — A callable that takes a page number and returns a URL string
-
‘**html_attributes` — Additional HTML attributes for the pagination container
27 28 29 |
# File 'lib/bulma_phlex/pagination.rb', line 27 def self.new(pager, path_builder, **html_attributes) super end |
Instance Method Details
#view_template ⇒ Object
37 38 39 40 41 42 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 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bulma_phlex/pagination.rb', line 37 def view_template return unless pager.total_pages > 1 div(**mix({ class: "pagination-container" }, @html_attributes)) do nav(class: "pagination", role: "navigation", aria_label: "pagination") do # Previous page link if pager.previous_page a(class: "pagination-previous", href: page_url(pager.previous_page)) { "Previous" } else a(class: "pagination-previous", disabled: true) { "Previous" } end # Next page link if pager.next_page a(class: "pagination-next", href: page_url(pager.next_page)) { "Next" } else a(class: "pagination-next", disabled: true) { "Next" } end # Page number links ul(class: "pagination-list") do # First page and ellipsis if needed if pager.current_page > 3 render_page_item(1) render_ellipsis if pager.current_page > 4 end # Pages around current page page_window.each do |page_number| render_page_item(page_number) end # Ellipsis and last page if needed if pager.current_page < pager.total_pages - 2 render_ellipsis if pager.current_page < pager.total_pages - 3 render_page_item(pager.total_pages) end end end div(class: "has-text-centered mt-2 is-size-7") do start_item = ((pager.current_page - 1) * pager.per_page) + 1 end_item = [start_item + pager.per_page - 1, pager.total_count].min plain "Showing #{start_item}-#{end_item} of #{pager.total_count} items" end end end |