module ActiveModel::Validations::Callbacks::ClassMethods
def after_validation(*args, &block)
person.valid? # => true
person.name = 'bob'
person.status # => false
person.valid? # => false
person.name = ''
person = Person.new
end
end
self.status = errors.empty?
def set_status
private
after_validation :set_status
validates_presence_of :name
attr_accessor :name, :status
include ActiveModel::Validations::Callbacks
include ActiveModel::Validations
class Person
Defines a callback that will get called right after validation.
def after_validation(*args, &block) options = args.extract_options! options = options.dup options[:prepend] = true set_options_for_callback(options) set_callback(:validation, :after, *args, options, &block) end
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.extract_options! set_options_for_callback(options) set_callback(:validation, :before, *args, options, &block) end
def set_options_for_callback(options)
def set_options_for_callback(options) if options.key?(:on) options[:on] = Array(options[:on]) options[:if] = [ ->(o) { options[:on].intersect?(Array(o.validation_context)) }, *options[:if] ] end end