Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/ruby_ui/data_table/data_table_kaminari_adapter.rb
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
17 changes: 17 additions & 0 deletions lib/ruby_ui/data_table/data_table_manual_adapter.rb
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
30 changes: 18 additions & 12 deletions lib/ruby_ui/data_table/data_table_pagination.rb
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"
Comment on lines +4 to +6
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.


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
Expand All @@ -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
Expand All @@ -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") { @prev_label }
end
else
render RubyUI::PaginationItem.new(href: page_href(current - 1)) { plain "Previous" }
render RubyUI::PaginationItem.new(href: page_href(current - 1)) { @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") { @next_label }
end
else
render RubyUI::PaginationItem.new(href: page_href(current + 1)) { plain "Next" }
render RubyUI::PaginationItem.new(href: page_href(current + 1)) { @next_label }
end
end

Expand Down
17 changes: 17 additions & 0 deletions lib/ruby_ui/data_table/data_table_pagy_adapter.rb
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
4 changes: 3 additions & 1 deletion lib/ruby_ui/data_table/data_table_sort_head.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def sort_href
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 sort_icon
Expand Down
19 changes: 0 additions & 19 deletions lib/ruby_ui/data_table_pagination_adapters/kaminari.rb

This file was deleted.

19 changes: 0 additions & 19 deletions lib/ruby_ui/data_table_pagination_adapters/manual.rb

This file was deleted.

19 changes: 0 additions & 19 deletions lib/ruby_ui/data_table_pagination_adapters/pagy.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require "test_helper"
require "ruby_ui/data_table_pagination_adapters/kaminari"
require "ruby_ui/data_table/data_table_kaminari_adapter"

class RubyUI::DataTablePaginationAdapters::KaminariTest < ComponentTest
class RubyUI::DataTableKaminariAdapterTest < ComponentTest
CollectionDouble = Data.define(:current_page, :total_pages, :total_count, :limit_value)

def test_reads_current_page_total_pages_total_count_limit_value
coll = CollectionDouble.new(current_page: 3, total_pages: 7, total_count: 61, limit_value: 10)
adapter = RubyUI::DataTablePaginationAdapters::Kaminari.new(coll)
adapter = RubyUI::DataTableKaminariAdapter.new(coll)
assert_equal 3, adapter.current_page
assert_equal 7, adapter.total_pages
assert_equal 61, adapter.total_count
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal: true

require "test_helper"
require "ruby_ui/data_table_pagination_adapters/manual"
require "ruby_ui/data_table/data_table_manual_adapter"

class RubyUI::DataTablePaginationAdapters::ManualTest < ComponentTest
class RubyUI::DataTableManualAdapterTest < ComponentTest
def test_computes_total_pages_from_total_count_and_per_page
adapter = RubyUI::DataTablePaginationAdapters::Manual.new(page: 2, per_page: 10, total_count: 25)
adapter = RubyUI::DataTableManualAdapter.new(page: 2, per_page: 10, total_count: 25)
assert_equal 2, adapter.current_page
assert_equal 10, adapter.per_page
assert_equal 25, adapter.total_count
assert_equal 3, adapter.total_pages
end

def test_total_pages_is_at_least_1_for_empty_total
adapter = RubyUI::DataTablePaginationAdapters::Manual.new(page: 1, per_page: 10, total_count: 0)
adapter = RubyUI::DataTableManualAdapter.new(page: 1, per_page: 10, total_count: 0)
assert_equal 1, adapter.total_pages
end

def test_coerces_integer_inputs
adapter = RubyUI::DataTablePaginationAdapters::Manual.new(page: "3", per_page: "5", total_count: "12")
adapter = RubyUI::DataTableManualAdapter.new(page: "3", per_page: "5", total_count: "12")
assert_equal 3, adapter.current_page
assert_equal 3, adapter.total_pages
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require "test_helper"
require "ruby_ui/data_table_pagination_adapters/pagy"
require "ruby_ui/data_table/data_table_pagy_adapter"

class RubyUI::DataTablePaginationAdapters::PagyTest < ComponentTest
class RubyUI::DataTablePagyAdapterTest < ComponentTest
PagyDouble = Data.define(:page, :pages, :count, :items)

def test_reads_page_pages_count_items
pagy = PagyDouble.new(page: 2, pages: 5, count: 47, items: 10)
adapter = RubyUI::DataTablePaginationAdapters::Pagy.new(pagy)
adapter = RubyUI::DataTablePagyAdapter.new(pagy)
assert_equal 2, adapter.current_page
assert_equal 5, adapter.total_pages
assert_equal 47, adapter.total_count
Expand Down
Loading