Skip to content

Instantly share code, notes, and snippets.

@neerajdotname
Created Jun 16, 2010
Embed
What would you like to do?
class Dog
include ActiveModel::Validations
extend ActiveModel::Callbacks
define_model_callbacks :custom
set_callback(:custom, :before, :set_default_name)
attr_accessor :name
def set_default_name
@name = 'super_cool_dog'
end
validates_presence_of :name
# can't use valid? directly since before_validation might want to
# add something to errors
def valid?(context = nil)
current_context, self.validation_context = validation_context, context
errors.clear
_run_custom_callbacks { _run_validate_callbacks }
errors.empty?
ensure
self.validation_context = current_context
end
end
Dog.new.valid?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment