Module: MaquinaComponents::PaginationHelper

Defined in:
app/helpers/maquina_components/pagination_helper.rb

Overview

Pagination Helper

Provides convenient methods for creating pagination components with Pagy integration.

Examples:

Using the helper with Pagy

<%%= pagination_nav(@pagy, :users_path) %>

With additional params

<%%= pagination_nav(@pagy, :search_users_path, params: { q: params[:q] }) %>

With Turbo options

<%%= pagination_nav(@pagy, :users_path, turbo: { action: :replace, frame: "users" }) %>

Using partials directly

<%%= render "components/pagination" do %>
  <%%= render "components/pagination/content" do %>
    <%%= render "components/pagination/item" do %>
      <%%= render "components/pagination/previous", href: prev_path %>
    <%% end %>
    ...
  <%% end %>
<%% end %>

Instance Method Summary collapse

Instance Method Details

#paginated_path(route_helper, pagy, page, extra_params = {}) ⇒ String

Build paginated path with page param

Parameters:

  • route_helper (Symbol)

    Route helper method name

  • pagy (Pagy)

    The Pagy pagination object

  • page (Integer)

    Page number

  • extra_params (Hash) (defaults to: {})

    Additional params

Returns:

  • (String)

    URL path



78
79
80
81
82
83
84
# File 'app/helpers/maquina_components/pagination_helper.rb', line 78

def paginated_path(route_helper, pagy, page, extra_params = {})
  page_param = pagy.vars[:page_param] || Pagy::DEFAULT[:page_param]
  query_params = request.query_parameters.except(page_param.to_s).merge(extra_params)
  query_params[page_param] = page

  send(route_helper, query_params)
end

#pagination_nav(pagy, route_helper, params: {}, turbo: {action: :replace}, show_labels: true, css_classes: "", **html_options) ⇒ String

Renders a complete pagination navigation from a Pagy object

Parameters:

  • pagy (Pagy)

    The Pagy pagination object

  • route_helper (Symbol)

    Route helper method name (e.g., :users_path)

  • params (Hash) (defaults to: {})

    Additional params to pass to route helper

  • turbo (Hash) (defaults to: {action: :replace})

    Turbo-specific data attributes

  • show_labels (Boolean) (defaults to: true)

    Whether to show Previous/Next text labels

  • css_classes (String) (defaults to: "")

    Additional CSS classes for the nav

Returns:

  • (String)

    Rendered HTML



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/maquina_components/pagination_helper.rb', line 37

def pagination_nav(pagy, route_helper, params: {}, turbo: {action: :replace}, show_labels: true, css_classes: "", **html_options)
  return if pagy.pages <= 1

  render "components/pagination", css_classes: css_classes, **html_options do
    render "components/pagination/content" do
      safe_join([
        pagination_previous_item(pagy, route_helper, params, turbo, show_labels),
        pagination_page_items(pagy, route_helper, params, turbo),
        pagination_next_item(pagy, route_helper, params, turbo, show_labels)
      ])
    end
  end
end

#pagination_simple(pagy, route_helper, params: {}, turbo: {action: :replace}, css_classes: "", **html_options) ⇒ String

Simpler pagination with just Previous/Next (no page numbers)

Parameters:

  • pagy (Pagy)

    The Pagy pagination object

  • route_helper (Symbol)

    Route helper method name

  • params (Hash) (defaults to: {})

    Additional params to pass to route helper

  • turbo (Hash) (defaults to: {action: :replace})

    Turbo-specific data attributes

Returns:

  • (String)

    Rendered HTML



58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/maquina_components/pagination_helper.rb', line 58

def pagination_simple(pagy, route_helper, params: {}, turbo: {action: :replace}, css_classes: "", **html_options)
  return if pagy.pages <= 1

  render "components/pagination", css_classes: css_classes, **html_options do
    render "components/pagination/content" do
      safe_join([
        pagination_previous_item(pagy, route_helper, params, turbo, true),
        pagination_next_item(pagy, route_helper, params, turbo, true)
      ])
    end
  end
end