-
Notifications
You must be signed in to change notification settings - Fork 65
[Bug Fix] Fix DataTable pagination with array params and adapter naming #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b986053
5959464
a626a9d
4940ef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyUI | ||
| class DataTableKaminariAdapter | ||
| def initialize(collection) | ||
| @collection = collection | ||
| end | ||
|
|
||
| def current_page = @collection.current_page | ||
|
|
||
| def total_pages = @collection.total_pages | ||
|
|
||
| def total_count = @collection.total_count | ||
|
|
||
| def per_page = @collection.limit_value | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyUI | ||
| class DataTableManualAdapter | ||
| attr_reader :current_page, :per_page, :total_count | ||
|
|
||
| def initialize(page:, per_page:, total_count:) | ||
| @current_page = page.to_i | ||
| @per_page = [per_page.to_i, 1].max | ||
| @total_count = total_count.to_i | ||
| end | ||
|
|
||
| def total_pages | ||
| [(@total_count.to_f / @per_page).ceil, 1].max | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "cgi" | ||
| require_relative "../data_table_pagination_adapters/manual" | ||
| require_relative "../data_table_pagination_adapters/pagy" | ||
| require_relative "../data_table_pagination_adapters/kaminari" | ||
| require_relative "data_table_manual_adapter" | ||
| require_relative "data_table_pagy_adapter" | ||
| require_relative "data_table_kaminari_adapter" | ||
|
|
||
| module RubyUI | ||
| class DataTablePagination < Base | ||
| def initialize(with: nil, pagy: nil, kaminari: nil, page: nil, per_page: nil, total_count: nil, page_param: "page", path: "", query: {}, window: 1, **attrs) | ||
| def initialize(with: nil, pagy: nil, kaminari: nil, page: nil, per_page: nil, total_count: nil, page_param: "page", path: "", query: {}, window: 1, prev_label: "<", next_label: ">", **attrs) | ||
| @adapter = resolve_adapter(with:, pagy:, kaminari:, page:, per_page:, total_count:) | ||
| @page_param = page_param | ||
| @path = path | ||
| @query = query.to_h.transform_keys(&:to_s) | ||
| @window = window | ||
| @prev_label = prev_label | ||
| @next_label = next_label | ||
| super(**attrs) | ||
| end | ||
|
|
||
| def view_template | ||
| return if total <= 1 | ||
|
|
||
| render RubyUI::Pagination.new(class: "mx-0 w-auto justify-end", **attrs) do | ||
| render RubyUI::PaginationContent.new do | ||
| prev_item | ||
|
|
@@ -30,10 +34,10 @@ def view_template | |
|
|
||
| def resolve_adapter(with:, pagy:, kaminari:, page:, per_page:, total_count:) | ||
| return with if with | ||
| return RubyUI::DataTablePaginationAdapters::Pagy.new(pagy) if pagy | ||
| return RubyUI::DataTablePaginationAdapters::Kaminari.new(kaminari) if kaminari | ||
| return RubyUI::DataTablePagyAdapter.new(pagy) if pagy | ||
| return RubyUI::DataTableKaminariAdapter.new(kaminari) if kaminari | ||
| if page && per_page && total_count | ||
| return RubyUI::DataTablePaginationAdapters::Manual.new(page:, per_page:, total_count:) | ||
| return RubyUI::DataTableManualAdapter.new(page:, per_page:, total_count:) | ||
| end | ||
| raise ArgumentError, "DataTablePagination requires one of: with:, pagy:, kaminari:, or page:+per_page:+total_count:" | ||
| end | ||
|
|
@@ -48,26 +52,28 @@ def page_href(p) | |
| end | ||
|
|
||
| def build_query(hash) | ||
| hash.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") | ||
| hash.flat_map { |k, v| | ||
| Array(v).map { |val| "#{CGI.escape(k.to_s)}=#{CGI.escape(val.to_s)}" } | ||
| }.join("&") | ||
| end | ||
|
|
||
| def prev_item | ||
| if current <= 1 | ||
| li do | ||
| span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain "Previous" } | ||
| span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain @prev_label } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One line plain is unnecessary. span(class: "...") { @prev_label } # it works
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, fixed in 4940ef2. |
||
| end | ||
| else | ||
| render RubyUI::PaginationItem.new(href: page_href(current - 1)) { plain "Previous" } | ||
| render RubyUI::PaginationItem.new(href: page_href(current - 1)) { plain @prev_label } | ||
| end | ||
| end | ||
|
|
||
| def next_item | ||
| if current >= total | ||
| li do | ||
| span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain "Next" } | ||
| span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain @next_label } | ||
| end | ||
| else | ||
| render RubyUI::PaginationItem.new(href: page_href(current + 1)) { plain "Next" } | ||
| render RubyUI::PaginationItem.new(href: page_href(current + 1)) { plain @next_label } | ||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyUI | ||
| class DataTablePagyAdapter | ||
| def initialize(pagy) | ||
| @pagy = pagy | ||
| end | ||
|
|
||
| def current_page = @pagy.page | ||
|
|
||
| def total_pages = @pagy.pages | ||
|
|
||
| def total_count = @pagy.count | ||
|
|
||
| def per_page = @pagy.items | ||
| end | ||
| end |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@djalmaaraujo I believe it's not necessary anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gem doesn't use Zeitwerk, so require_relative is needed here. When the generator copies the files into a Rails app, the app's autoloader handles it — but in the gem context these are required.