2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
# == Strip Attribute module
|
|
|
|
#
|
|
|
|
# Contains functionality to clean attributes before validation
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# class Milestone < ActiveRecord::Base
|
|
|
|
# strip_attributes :title
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
#
|
|
|
|
module StripAttribute
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
class_methods do
|
2015-12-23 02:04:40 +05:30
|
|
|
def strip_attributes(*attrs)
|
|
|
|
strip_attrs.concat(attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_attrs
|
|
|
|
@strip_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_validation :strip_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_attributes
|
|
|
|
self.class.strip_attrs.each do |attr|
|
|
|
|
self[attr].strip! if self[attr] && self[attr].respond_to?(:strip!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|