8000 Support sortable argument in id_column (#8639) · activeadmin/activeadmin@bb1bade · GitHub
[go: up one dir, main page]

Skip to content

Commit bb1bade

Browse files
authored
Support sortable argument in id_column (#8639)
This is mostly useful to prevent sorting on tables with uuid PKs. Sorting by uuid isn't very useful, and it can have problematic performance if the table is very large, even if there is an index.
1 parent be13072 commit bb1bade

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/active_admin/views/index_as_table.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ def selectable_column(**options)
256256
end
257257

258258
# Display a column for the id
259-
def id_column
259+
def id_column(sortable: resource_class.primary_key)
260260
raise "#{resource_class.name} has no primary_key!" unless resource_class.primary_key
261-
column(resource_class.human_attribute_name(resource_class.primary_key), sortable: resource_class.primary_key) do |resource|
261+
column(resource_class.human_attribute_name(resource_class.primary_key), sortable: sortable) do |resource|
262262
if controller.action_methods.include?("show")
263263
link_to resource.id, resource_path(resource)
264264
elsif controller.action_methods.include?("edit")

spec/unit/views/components/index_table_for_spec.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
let(:assigns) do
1616
{
1717
collection: collection,
18-
active_admin_config: active_admin_config
18+
active_admin_config: active_admin_config,
19+
resource_class: User,
1920
}
2021
end
2122
let(:helpers) { mock_action_view }
@@ -43,5 +44,35 @@
4344
end
4445
end
4546
end
47+
48+
context "when creating an id column" do
49+
before { allow(helpers).to receive(:url_target) { 'routing_stub' } }
50+
51+
def build_index_table(&block)
52+
render_arbre_component assigns, helpers do
53+
insert_tag(ActiveAdmin::Views::IndexAsTable::IndexTableFor, collection, { sortable: true }) do
54+
instance_exec(&block)
55+
end
56+
end
57+
end
58+
59+
it "is sortable by default" do
60+
table = build_index_table { id_column }
61+
header = table.find_by_tag("th").first
62+
expect(header.attributes).to include("data-sortable": "")
63+
end
64+
65+
it "supports sortable: false" do
66+
table = build_index_table { id_column sortable: false }
67+
header = table.find_by_tag("th").first
68+
expect(header.attributes).not_to include("data-sortable": "")
69+
end
70+
71+
it "supports sortable column names" do
72+
table = build_index_table { id_column sortable: :created_at }
73+
header = table.find_by_tag("th").first
74+
expect(header.attributes).to include("data-sortable": "")
75+
end
76+
end
4677
end
4778
end

0 commit comments

Comments
 (0)
0