Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created December 28, 2009 18:46
Show Gist options
  • Save josevalim/264842 to your computer and use it in GitHub Desktop.
Save josevalim/264842 to your computer and use it in GitHub Desktop.
# Instead of:
module MailForm::Callbacks
def self.extended(base)
base.class_eval do
include ActiveSupport::Callbacks
define_callbacks :create, :terminator => "result == false", :scope => [:kind, :name]
end
end
def before_create(*args, &block)
set_callback(:create, :before, *args, &block)
end
def around_create(*args, &block)
set_callback(:create, :around, *args, &block)
end
def after_create(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array(options[:if]) << "!halted && value != false"
set_callback(:create, :after, *(args << options), &block)
end
end
class MailForm::Base
extend MailForm::Callbacks
end
# I would be able to just do:
class MailForm::Base
include ActiveModel::Callbacks
define_model_callbacks :create
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment