module ActiveModel::Validations::Callbacks::ClassMethods
def before_validation(*args, &block)
person.valid? # => true
person.name = ' bob '
person = Person.new
end
end
name.strip!
def remove_whitespaces
private
before_validation :remove_whitespaces
validates_length_of :name, maximum: 6
attr_accessor :name
include ActiveModel::Validations::Callbacks
include ActiveModel::Validations
class Person
Defines a callback that will get called right before validation.
def before_validation(*args, &block) options = args.last if options.is_a?(Hash) && options[:on] options[:if] = Array(options[:if]) options[:on] = Array(options[:on]) options[:if].unshift ->(o) { options[:on].include? o.validation_context } end set_callback(:validation, :before, *args, &block) end