Skip to content

Instantly share code, notes, and snippets.

@bensheldon
Created June 18, 2026 15:52
Show Gist options
  • Select an option

  • Save bensheldon/08ab41e893217c557b9f548e01a77da0 to your computer and use it in GitHub Desktop.

Select an option

Save bensheldon/08ab41e893217c557b9f548e01a77da0 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
RSpec.shared_examples "strip normalizes text columns" do
it "strip normalizes text column values" do
text_columns = described_class.columns.filter_map do |col|
next unless [:text, :string].include?(col.sql_type_metadata.type)
next if described_class.defined_enums.key?(col.name)
attr_type = described_class.attribute_types[col.name]
next col.name if attr_type.instance_of?(ActiveRecord::Type::Text)
if attr_type.instance_of?(ActiveModel::Attributes::Normalization::NormalizedValueType) && attr_type.cast_type.instance_of?(ActiveRecord::Type::Text)
next col.name
end
end
record = described_class.new
aggregate_failures do
text_columns.each do |col_name|
record.public_send(:"#{col_name}=", " hello ")
expect(record.public_send(col_name)).to eq("hello"),
"Expected #{described_class}##{col_name} to strip leading/trailing whitespace"
record.public_send(:"#{col_name}=", " ")
expect(record.public_send(col_name)).to be_nil,
"Expected #{described_class}##{col_name} to convert whitespace-only strings to nil"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment