Module: MongoMapper::Plugins::Callbacks::InstanceMethods

Defined in:
lib/mongo_mapper/plugins/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject



72
73
74
75
76
77
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 72

def destroy
  run_callbacks(:before_destroy)
  result = super
  run_callbacks(:after_destroy)
  result
end

#run_callbacks(kind, options = {}, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 79

def run_callbacks(kind, options = {}, &block)
  callback_chain_method = "#{kind}_callback_chain"
  return unless self.class.respond_to?(callback_chain_method)
  self.class.send(callback_chain_method).run(self, options, &block)
  self.embedded_associations.each do |association|
    self.send(association.name).each do |document|
      document.run_callbacks(kind, options, &block)
    end
  end
end

#valid?Boolean

Returns:



51
52
53
54
55
56
57
58
59
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 51

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

#valid_for_group?(group) ⇒ Boolean

Overriding validatable’s valid_for_group? to integrate validation callbacks

Returns:



62
63
64
65
66
67
68
69
70
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 62

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