8000 Support sortable argument in id_column · activeadmin/activeadmin@6d53e98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d53e98

Browse files
committed
Support sortable argument in id_column
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. Backport #8639
1 parent 73b88e1 commit 6d53e98

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
@@ -291,9 +291,9 @@ def index_column(start_value = 1)
291291
end
292292

293293
# Display a column for the id
294-
def id_column
294+
def id_column(sortable: resource_class.primary_key)
295295
raise "#{resource_class.name} has no primary_key!" unless resource_class.primary_key
296-
column(resource_class.human_attribute_name(resource_class.primary_key), sortable: resource_class.primary_key) do |resource|
296+
column(resource_class.human_attribute_name(resource_class.primary_key), sortable: sortable) do |resource|
297297
if controller.action_methods.include?("show")
298298
link_to resource.id, resource_path(resource), class: "resource_id_link"
299299
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 8000
let(:helpers) { mock_action_view }
@@ -44,6 +45,36 @@
4445
end
4546
end
4647

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[:class]).to include("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[:class]).not_to include("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[:class]).to include("sortable")
75+
end
76+
end
77+
4778
context "when creating an index column" do
4879
let(:base_collection) do
4980
posts = [

0 commit comments

Comments
 (0)
0