debian-mirror-gitlab/app/models/concerns/strip_attribute.rb

38 lines
698 B
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
# == Strip Attribute module
#
2021-12-11 22:18:48 +05:30
# Contains functionality to remove leading and trailing
# whitespace from the attribute before validation
2015-12-23 02:04:40 +05:30
#
# Usage:
#
2019-07-07 11:18:12 +05:30
# class Milestone < ApplicationRecord
2021-10-27 15:23:28 +05:30
# strip_attributes! :title
2015-12-23 02:04:40 +05:30
# end
#
#
module StripAttribute
extend ActiveSupport::Concern
2018-11-20 20:47:30 +05:30
class_methods do
2021-10-27 15:23:28 +05:30
def strip_attributes!(*attrs)
2015-12-23 02:04:40 +05:30
strip_attrs.concat(attrs)
end
def strip_attrs
@strip_attrs ||= []
end
end
included do
2021-10-27 15:23:28 +05:30
before_validation :strip_attributes!
2015-12-23 02:04:40 +05:30
end
2021-10-27 15:23:28 +05:30
def strip_attributes!
2015-12-23 02:04:40 +05:30
self.class.strip_attrs.each do |attr|
self[attr].strip! if self[attr] && self[attr].respond_to?(:strip!)
end
end
end