Skip to content

Instantly share code, notes, and snippets.

@kevinvaldek
Created January 14, 2010 15:03
Show Gist options
  • Save kevinvaldek/277226 to your computer and use it in GitHub Desktop.
Save kevinvaldek/277226 to your computer and use it in GitHub Desktop.
# lib/mongo_mapper/plugins/callbacks.rb
module MongoMapper
module Plugins
module Callbacks
module InstanceMethods
def self.included(model)
model.class_eval do
extend ActiveModel::Callbacks
define_model_callbacks(
:save,
:create,
:update,
:validation,
:validation_on_create,
:validation_on_update,
:destroy
)
end
end
def valid?
_run_validation_callbacks do
send(:"_run_validation_on_#{new? ? 'create' : 'update'}_callbacks") do
super
end
end
end
def destroy
_run_destroy_callbacks { super }
end
private
def create_or_update(*args)
_run_save_callbacks { super }
end
def create(*args)
_run_create_callbacks { super }
end
def update(*args)
_run_update_callbacks { super }
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment