Skip to content

Commit 1f6dc9b

Browse files
Copilotsenid231
andcommitted
Fix table_cell_selector to strip special characters like slashes from column names
Agent-Logs-Url: https://github.com/activeadmin-plugins/capybara_active_admin/sessions/7498bf2c-1012-4f45-a573-e5f8a7124f08 Co-authored-by: senid231 <8393857+senid231@users.noreply.github.com>
1 parent 432c58e commit 1f6dc9b

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/capybara/active_admin/selectors/table.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def table_header_selector
3232
def table_cell_selector(column = nil)
3333
return 'td.col' if column.nil?
3434

35-
column = column.to_s.gsub(' ', '_').downcase
35+
column = column.to_s.downcase.gsub(/[^a-z0-9\s]/, '').gsub(/\s+/, '_').squeeze('_')
3636
"td.col.col-#{column}"
3737
end
3838

spec/lib/selectors/table_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Capybara::ActiveAdmin::Selectors::Table do
4+
subject(:helper) do
5+
Class.new { include Capybara::ActiveAdmin::Selectors::Table }.new
6+
end
7+
8+
describe '#table_cell_selector' do
9+
it 'returns generic selector when column is nil' do
10+
expect(helper.table_cell_selector).to eq('td.col')
11+
end
12+
13+
it 'converts spaces to underscores' do
14+
expect(helper.table_cell_selector('Full Name')).to eq('td.col.col-full_name')
15+
end
16+
17+
it 'downcases the column name' do
18+
expect(helper.table_cell_selector('ID')).to eq('td.col.col-id')
19+
end
20+
21+
it 'strips slashes from column name' do
22+
expect(helper.table_cell_selector('Country / Region')).to eq('td.col.col-country_region')
23+
end
24+
25+
it 'strips other special characters from column name' do
26+
expect(helper.table_cell_selector('Price (USD)')).to eq('td.col.col-price_usd')
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)