lib/mongomodel/concerns/callbacks.rb in mongomodel-0.5.5 vs lib/mongomodel/concerns/callbacks.rb in mongomodel-0.5.6
- old
+ new
@@ -18,11 +18,11 @@
# * (-) <tt>create</tt>
# * (5) <tt>after_create</tt>
# * (6) <tt>after_save</tt>
#
# That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the
- # MongoModel lifecycle. The sequence for calling <tt>Document#save</tt> for an existing record is similar, except that each
+ # MongoModel lifecycle. The sequence for calling <tt>Document#save</tt> for an existing record is similar, except that each
# <tt>_on_create</tt> callback is replaced by the corresponding <tt>_on_update</tt> callback.
#
# Examples:
# class CreditCard < MongoModel::Document
# # Strip everything but digits, so the user can specify "555 234 34" or
@@ -197,36 +197,36 @@
:after_initialize, :after_find, :before_validation, :after_validation,
:before_save, :around_save, :after_save, :before_create, :around_create,
:after_create, :before_update, :around_update, :after_update,
:before_destroy, :around_destroy, :after_destroy
]
-
+
module ClassMethods
include ActiveModel::Callbacks
end
-
+
included do
include ActiveModel::Validations::Callbacks
-
+
define_model_callbacks :initialize, :find, :only => :after
define_model_callbacks :save, :create, :update, :destroy
end
-
+
def initialize(*args, &block) #:nodoc:
super
run_callbacks_with_embedded(:initialize)
end
-
+
def run_callbacks_with_embedded(kind, *args, &block)
if block_given?
embedded_callbacks = nest_embedded_callbacks(kind, *args, &block)
run_callbacks(kind, *args, &embedded_callbacks)
else
run_callbacks(kind, *args)
embedded_documents.each { |doc| doc.run_callbacks(kind, *args) } unless kind == :initialize
end
end
-
+
private
def nest_embedded_callbacks(kind, *args, &block)
embedded_documents.inject(block) do |callback, doc|
Proc.new { doc.run_callbacks_with_embedded(kind, *args, &callback) }
end