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

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

Instance Method Summary collapse

Instance Method Details

#destroyObject



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

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 80

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|
    if association.one?
      self.send(association.name).run_callbacks(kind, options, &block)
    else
      self.send(association.name).each do |document|
        document.run_callbacks(kind, options, &block)
      end
    end
  end
end

#valid?Boolean

Returns:



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

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:



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

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