petities/petitions.eu

View on GitHub
app/models/concerns/strip_whitespace.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# Strip whitespace from ActiveRecord attributes before validation
module StripWhitespace
  extend ActiveSupport::Concern

  included do
    class_attribute :stripped_attributes, instance_writer: false
    before_validation :strip_whitespace
  end

  class_methods do
    def strip_whitespace(*fields)
      self.stripped_attributes = fields
    end
  end

  private

  def strip_whitespace
    stripped_attributes.each do |field|
      value = send(field)
      send("#{field}=", value.strip) if value
    end
  end
end