Module: MongoMapper::Callbacks

Defined in:
lib/mongo_mapper/callbacks.rb

Overview

This module is mixed into the Document module to provide call-backs before and after the following events:

  • save

  • create

  • update

  • validation

** every validation ** validation when created ** validation when updated

  • destruction

See Also:

  • ActiveSupport::Callbacks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongo_mapper/callbacks.rb', line 16

def self.included(model) #:nodoc:
  model.class_eval do
    extend Observable
    include ActiveSupport::Callbacks

    callbacks = %w(
      before_save
      after_save
      before_create
      after_create
      before_update
      after_update
      before_validation
      after_validation
      before_validation_on_create
      after_validation_on_create
      before_validation_on_update
      after_validation_on_update
      before_destroy
      after_destroy
    )

    define_callbacks(*callbacks)

    callbacks.each do |callback|
      define_method(callback.to_sym) {}
    end
  end
end

Instance Method Details

#destroyObject

Here we override the destroy method to allow for the before_destroy and after_destroy call-backs. Note that the destroy call is aborted if the before_destroy call-back returns false.

Returns:

  • the result of calling destroy on the document



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

def destroy #:nodoc:
  return false if callback(:before_destroy) == false
  result = super
  callback(:after_destroy)
  result
end

#valid?Boolean

:nodoc:

Returns:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongo_mapper/callbacks.rb', line 46

def valid? #:nodoc:
  return false if callback(:before_validation) == false
  result = new? ? callback(:before_validation_on_create) : callback(:before_validation_on_update)
  return false if false == result

  result = super
  callback(:after_validation)

  new? ? callback(:after_validation_on_create) : callback(:after_validation_on_update)
  return result
end