class ActiveRecord::Attr::Stripper::AttrStripper

Public Instance Methods

before_validation(record) click to toggle source
# File lib/activerecord/attr/stripper/attr_stripper.rb, line 10
def before_validation(record)
  strip_targets = record.class.class_variable_get :@@strip_targets

  return if strip_targets.blank?

  strip_targets.each do |target|
    target.attrs.each do |attr|
      attr_value = record.send(attr)

      if attr_value.kind_of?(String)
        stripped_value = strip(attr_value, target.opts)

        record.send("#{attr.to_s}=", stripped_value)
      end
    end
  end
end

Private Instance Methods

strip(attr_value, opts) click to toggle source
# File lib/activerecord/attr/stripper/attr_stripper.rb, line 30
def strip(attr_value, opts)
  stripped_value = attr_value.gsub(/\A(\s| )+|(\s| )+\z/, '')

  if opts[:blank_to_nil]
    stripped_value.presence
  else
    stripped_value
  end
end