lib/validatable_instance_methods.rb in jnunemaker-validatable-1.7.0 vs lib/validatable_instance_methods.rb in jnunemaker-validatable-1.7.1
- old
+ new
@@ -1,9 +1,13 @@
module Validatable
def self.included(klass) #:nodoc:
klass.extend Validatable::ClassMethods
klass.extend Validatable::Macros
+ klass.class_eval do
+ include ActiveSupport::Callbacks
+ define_callbacks :validate, :validate_on_create, :validate_on_update
+ end
end
# call-seq: valid?
#
# Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
@@ -19,12 +23,18 @@
end
def valid_for_group?(group) #:nodoc:
run_before_validations
errors.clear
+ run_callbacks(:validate)
+
+ if respond_to?(:new?)
+ new? ? run_callbacks(:validate_on_create) : run_callbacks(:validate_on_update)
+ end
+
self.class.validate_children(self, group)
- self.validate(group)
+ self.validate_group(group)
errors.empty?
end
def times_validated(key) #:nodoc:
times_validated_hash[key] || 0
@@ -59,10 +69,10 @@
protected
def times_validated_hash #:nodoc:
@times_validated_hash ||= {}
end
- def validate(group) #:nodoc:
+ def validate_group(group) #:nodoc:
validation_levels.each do |level|
validations_for_level_and_group(level, group).each do |validation|
run_validation(validation) if validation.should_validate?(self)
end
return unless self.errors.empty?
\ No newline at end of file