Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/capybara/active_admin/selectors/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions spec/lib/selectors/table_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading