2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-26 10:16:50 -05:00
|
|
|
# == Strip Attribute module
|
|
|
|
#
|
|
|
|
# Contains functionality to clean attributes before validation
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
2019-03-28 09:17:42 -04:00
|
|
|
# class Milestone < ApplicationRecord
|
2021-08-16 20:10:22 -04:00
|
|
|
# strip_attributes! :title
|
2015-11-26 10:16:50 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
#
|
|
|
|
module StripAttribute
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2021-08-16 20:10:22 -04:00
|
|
|
def strip_attributes!(*attrs)
|
2015-11-26 10:16:50 -05:00
|
|
|
strip_attrs.concat(attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_attrs
|
|
|
|
@strip_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
2021-08-16 20:10:22 -04:00
|
|
|
before_validation :strip_attributes!
|
2015-11-26 10:16:50 -05:00
|
|
|
end
|
|
|
|
|
2021-08-16 20:10:22 -04:00
|
|
|
def strip_attributes!
|
2015-11-26 10:16:50 -05:00
|
|
|
self.class.strip_attrs.each do |attr|
|
|
|
|
self[attr].strip! if self[attr] && self[attr].respond_to?(:strip!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|