diff --git a/lib/capybara/active_admin/selectors/table.rb b/lib/capybara/active_admin/selectors/table.rb index 10b84da..170bfa4 100644 --- a/lib/capybara/active_admin/selectors/table.rb +++ b/lib/capybara/active_admin/selectors/table.rb @@ -32,7 +32,8 @@ def table_header_selector def table_cell_selector(column = nil) return 'td.col' if column.nil? - column = column.to_s.gsub(' ', '_').downcase + # Downcase, strip non-alphanumeric chars (e.g. '/'), convert spaces to '_', deduplicate '_' + column = column.to_s.downcase.gsub(/[^a-z0-9\s]/, '').gsub(/\s+/, '_').squeeze('_') "td.col.col-#{column}" end diff --git a/spec/lib/selectors/table_spec.rb b/spec/lib/selectors/table_spec.rb new file mode 100644 index 0000000..8001bdd --- /dev/null +++ b/spec/lib/selectors/table_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +RSpec.describe Capybara::ActiveAdmin::Selectors::Table do + subject(:helper) do + Class.new { include Capybara::ActiveAdmin::Selectors::Table }.new + end + + describe '#table_cell_selector' do + it 'returns generic selector when column is nil' do + expect(helper.table_cell_selector).to eq('td.col') + end + + it 'converts spaces to underscores' do + expect(helper.table_cell_selector('Full Name')).to eq('td.col.col-full_name') + end + + it 'downcases the column name' do + expect(helper.table_cell_selector('ID')).to eq('td.col.col-id') + end + + it 'strips slashes from column name' do + expect(helper.table_cell_selector('Country / Region')).to eq('td.col.col-country_region') + end + + it 'strips other special characters from column name' do + expect(helper.table_cell_selector('Price (USD)')).to eq('td.col.col-price_usd') + end + end +end