From 6321b2edefc5f7561c5467f686f4e8d1f912668c Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 8 Oct 2021 09:26:18 +0200 Subject: [PATCH] trim whitespace from beginning and end of string attributes --- app/models/application_record.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 71fbba5b..43c3e9aa 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -2,4 +2,15 @@ class ApplicationRecord < ActiveRecord::Base self.abstract_class = true + + before_validation :strip_strings + + def strip_strings + # trim whitespace from beginning and end of string attributes + attribute_names.each do |name| + if send(name.to_sym).respond_to?(:strip) + send("#{name}=".to_sym, send(name).strip) + end + end + end end