Skip to content

Commit

Permalink
Updated callbacks to include validate, validate_on_create and validat…
Browse files Browse the repository at this point in the history
…e_on_update as these are gone from validatable now.
  • Loading branch information
jnunemaker committed Mar 1, 2010
1 parent 05c618e commit de0d320
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/mongo_mapper/plugins/callbacks.rb
Expand Up @@ -13,7 +13,9 @@ def self.configure(model)
:before_validation, :after_validation,
:before_validation_on_create, :after_validation_on_create,
:before_validation_on_update, :after_validation_on_update,
:before_destroy, :after_destroy
:before_destroy, :after_destroy,
:validate_on_create, :validate_on_update,
:validate
)
end
end
Expand Down Expand Up @@ -48,16 +50,25 @@ def self.#{callback}_callback_chain
module InstanceMethods
def valid?
action = new? ? 'create' : 'update'

run_callbacks(:before_validation)
run_callbacks("before_validation_on_#{action}".to_sym)
result = super
run_callbacks("after_validation_on_#{action}".to_sym)
run_callbacks(:after_validation)

result
end

# Overriding validatable's valid_for_group? to integrate validation callbacks
def valid_for_group?(group) #:nodoc:
errors.clear
run_before_validations
run_callbacks(:validate)
new? ? run_callbacks(:validate_on_create) : run_callbacks(:validate_on_update)
self.class.validate_children(self, group)
self.validate_group(group)
errors.empty?
end

def destroy
run_callbacks(:before_destroy)
result = super
Expand Down

0 comments on commit de0d320

Please sign in to comment.