Skip to content

Commit 5959464

Browse files
committed
Move pagination adapters into data_table/ and rename to avoid conflicts
Rename DataTablePaginationAdapters::{Pagy,Kaminari,Manual} to DataTable{Pagy,Kaminari,Manual}Adapter under RubyUI namespace. Fixes class name collision when host app includes RubyUI module (e.g. Pagy clashing with the pagy gem). Also fixes generator only copying data_table/ folder, missing the separate adapters dir.
1 parent b986053 commit 5959464

10 files changed

Lines changed: 68 additions & 74 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class DataTableKaminariAdapter
5+
def initialize(collection)
6+
@collection = collection
7+
end
8+
9+
def current_page = @collection.current_page
10+
11+
def total_pages = @collection.total_pages
12+
13+
def total_count = @collection.total_count
14+
15+
def per_page = @collection.limit_value
16+
end
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class DataTableManualAdapter
5+
attr_reader :current_page, :per_page, :total_count
6+
7+
def initialize(page:, per_page:, total_count:)
8+
@current_page = page.to_i
9+
@per_page = [per_page.to_i, 1].max
10+
@total_count = total_count.to_i
11+
end
12+
13+
def total_pages
14+
[(@total_count.to_f / @per_page).ceil, 1].max
15+
end
16+
end
17+
end

lib/ruby_ui/data_table/data_table_pagination.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

33
require "cgi"
4-
require_relative "../data_table_pagination_adapters/manual"
5-
require_relative "../data_table_pagination_adapters/pagy"
6-
require_relative "../data_table_pagination_adapters/kaminari"
4+
require_relative "data_table_manual_adapter"
5+
require_relative "data_table_pagy_adapter"
6+
require_relative "data_table_kaminari_adapter"
77

88
module RubyUI
99
class DataTablePagination < Base
@@ -30,10 +30,10 @@ def view_template
3030

3131
def resolve_adapter(with:, pagy:, kaminari:, page:, per_page:, total_count:)
3232
return with if with
33-
return RubyUI::DataTablePaginationAdapters::Pagy.new(pagy) if pagy
34-
return RubyUI::DataTablePaginationAdapters::Kaminari.new(kaminari) if kaminari
33+
return RubyUI::DataTablePagyAdapter.new(pagy) if pagy
34+
return RubyUI::DataTableKaminariAdapter.new(kaminari) if kaminari
3535
if page && per_page && total_count
36-
return RubyUI::DataTablePaginationAdapters::Manual.new(page:, per_page:, total_count:)
36+
return RubyUI::DataTableManualAdapter.new(page:, per_page:, total_count:)
3737
end
3838
raise ArgumentError, "DataTablePagination requires one of: with:, pagy:, kaminari:, or page:+per_page:+total_count:"
3939
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class DataTablePagyAdapter
5+
def initialize(pagy)
6+
@pagy = pagy
7+
end
8+
9+
def current_page = @pagy.page
10+
11+
def total_pages = @pagy.pages
12+
13+
def total_count = @pagy.count
14+
15+
def per_page = @pagy.items
16+
end
17+
end

lib/ruby_ui/data_table_pagination_adapters/kaminari.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/ruby_ui/data_table_pagination_adapters/manual.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/ruby_ui/data_table_pagination_adapters/pagy.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/ruby_ui/data_table_pagination_adapters/kaminari_test.rb renamed to test/ruby_ui/data_table_kaminari_adapter_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4-
require "ruby_ui/data_table_pagination_adapters/kaminari"
4+
require "ruby_ui/data_table/data_table_kaminari_adapter"
55

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

99
def test_reads_current_page_total_pages_total_count_limit_value
1010
coll = CollectionDouble.new(current_page: 3, total_pages: 7, total_count: 61, limit_value: 10)
11-
adapter = RubyUI::DataTablePaginationAdapters::Kaminari.new(coll)
11+
adapter = RubyUI::DataTableKaminariAdapter.new(coll)
1212
assert_equal 3, adapter.current_page
1313
assert_equal 7, adapter.total_pages
1414
assert_equal 61, adapter.total_count

test/ruby_ui/data_table_pagination_adapters/manual_test.rb renamed to test/ruby_ui/data_table_manual_adapter_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4-
require "ruby_ui/data_table_pagination_adapters/manual"
4+
require "ruby_ui/data_table/data_table_manual_adapter"
55

6-
class RubyUI::DataTablePaginationAdapters::ManualTest < ComponentTest
6+
class RubyUI::DataTableManualAdapterTest < ComponentTest
77
def test_computes_total_pages_from_total_count_and_per_page
8-
adapter = RubyUI::DataTablePaginationAdapters::Manual.new(page: 2, per_page: 10, total_count: 25)
8+
adapter = RubyUI::DataTableManualAdapter.new(page: 2, per_page: 10, total_count: 25)
99
assert_equal 2, adapter.current_page
1010
assert_equal 10, adapter.per_page
1111
assert_equal 25, adapter.total_count
1212
assert_equal 3, adapter.total_pages
1313
end
1414

1515
def test_total_pages_is_at_least_1_for_empty_total
16-
adapter = RubyUI::DataTablePaginationAdapters::Manual.new(page: 1, per_page: 10, total_count: 0)
16+
adapter = RubyUI::DataTableManualAdapter.new(page: 1, per_page: 10, total_count: 0)
1717
assert_equal 1, adapter.total_pages
1818
end
1919

2020
def test_coerces_integer_inputs
21-
adapter = RubyUI::DataTablePaginationAdapters::Manual.new(page: "3", per_page: "5", total_count: "12")
21+
adapter = RubyUI::DataTableManualAdapter.new(page: "3", per_page: "5", total_count: "12")
2222
assert_equal 3, adapter.current_page
2323
assert_equal 3, adapter.total_pages
2424
end

test/ruby_ui/data_table_pagination_adapters/pagy_test.rb renamed to test/ruby_ui/data_table_pagy_adapter_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4-
require "ruby_ui/data_table_pagination_adapters/pagy"
4+
require "ruby_ui/data_table/data_table_pagy_adapter"
55

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

99
def test_reads_page_pages_count_items
1010
pagy = PagyDouble.new(page: 2, pages: 5, count: 47, items: 10)
11-
adapter = RubyUI::DataTablePaginationAdapters::Pagy.new(pagy)
11+
adapter = RubyUI::DataTablePagyAdapter.new(pagy)
1212
assert_equal 2, adapter.current_page
1313
assert_equal 5, adapter.total_pages
1414
assert_equal 47, adapter.total_count

0 commit comments

Comments
 (0)