1# frozen_string_literal: true 2 3# == Strip Attribute module 4# 5# Contains functionality to remove leading and trailing 6# whitespace from the attribute before validation 7# 8# Usage: 9# 10# class Milestone < ApplicationRecord 11# strip_attributes! :title 12# end 13# 14# 15module StripAttribute 16 extend ActiveSupport::Concern 17 18 class_methods do 19 def strip_attributes!(*attrs) 20 strip_attrs.concat(attrs) 21 end 22 23 def strip_attrs 24 @strip_attrs ||= [] 25 end 26 end 27 28 included do 29 before_validation :strip_attributes! 30 end 31 32 def strip_attributes! 33 self.class.strip_attrs.each do |attr| 34 self[attr].strip! if self[attr] && self[attr].respond_to?(:strip!) 35 end 36 end 37end 38