module StripStringFieldsTrait
as_trait do
before_validation :strip_string_fields
private
def strip_string_fields
string_columns = self.class.columns.select { |column| column.type == :string }
string_columns.each do |string_column|
attribute = string_column.name
value = send(attribute)
if value.present?
send("#{attribute}=", value.strip)
end
end
true
end
end
end
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter