lib/mongo_mapper/plugins/callbacks.rb in mongo_mapper-unstable-2010.3.8 vs lib/mongo_mapper/plugins/callbacks.rb in mongo_mapper-unstable-2010.06.23

- old
+ new

@@ -1,5 +1,6 @@ +# encoding: UTF-8 # Almost all of this callback stuff is pulled directly from ActiveSupport # in the interest of support rails 2 and 3 at the same time and is the # same copyright as rails. module MongoMapper module Plugins @@ -74,12 +75,23 @@ result = super run_callbacks(:after_destroy) result end - def run_callbacks(kind, options = {}, &block) - self.class.send("#{kind}_callback_chain").run(self, options, &block) + 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 private def create_or_update(*args) run_callbacks(:before_save) @@ -109,11 +121,11 @@ methods, options = extract_options(*methods, &block) methods.map! { |method| Callback.new(kind, method, options) } new(methods) end - def run(object, options = {}, &terminator) + def run(object, options={}, &terminator) enumerator = options[:enumerator] || :each unless block_given? send(enumerator) { |callback| callback.call(object) } else @@ -156,11 +168,11 @@ end class Callback attr_reader :kind, :method, :identifier, :options - def initialize(kind, method, options = {}) + def initialize(kind, method, options={}) @kind = kind @method = method @identifier = options[:identifier] @options = options end @@ -224,6 +236,6 @@ ![options[:unless]].flatten.compact.any? { |a| evaluate_method(a, *args) } end end end end -end \ No newline at end of file +end