Sha256: dd22abafb053d5f0a0c9642c5dd7ff7b296f6e812b10fd4bc886bc25aec742e6

Contents?: true

Size: 1.67 KB

Versions: 29

Compression:

Stored size: 1.67 KB

Contents

module MongoMapper
  module Plugins
    module Callbacks
      def self.configure(model)
        model.class_eval do
          include ActiveSupport::Callbacks

          define_callbacks(
            :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
          )
        end
      end
      
      module InstanceMethods
        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

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

        private
          def create_or_update(*args)
            run_callbacks(:before_save)
            if result = super
              run_callbacks(:after_save)
            end
            result
          end

          def create(*args)
            run_callbacks(:before_create)
            result = super
            run_callbacks(:after_create)
            result
          end

          def update(*args)
            run_callbacks(:before_update)
            result = super
            run_callbacks(:after_update)
            result
          end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.23 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.22 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.19 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.18 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.17 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.16 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.15 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.12 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.11 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.10 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-0.7.0 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.9 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.8 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.5 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.4 lib/mongo_mapper/plugins/callbacks.rb
mongo_mapper-unstable-2010.2.3 lib/mongo_mapper/plugins/callbacks.rb