Skip to content

Instantly share code, notes, and snippets.

@tsmango
Created September 2, 2011 00:55
Show Gist options
  • Select an option

  • Save tsmango/1187700 to your computer and use it in GitHub Desktop.

Select an option

Save tsmango/1187700 to your computer and use it in GitHub Desktop.
def self.extended(base)
base.class_eval do
include ActiveSupport::Callbacks
end
end
require 'active_support/core_ext/array/wrap'
require 'active_support/callbacks'
module ActiveModel
module Callbacks
def self.extended(base)
base.class_eval do
include ActiveSupport::Callbacks
end
end
def define_model_callbacks(*callbacks)
options = callbacks.extract_options!
options = {
:terminator => "result == false",
:scope => [:kind, :name],
:only => [:before, :around, :after]
}.merge(options)
types = Array.wrap(options.delete(:only))
callbacks.each do |callback|
define_callbacks(callback, options)
types.each do |type|
send("_define_#{type}_model_callback", self, callback)
end
end
end
def _define_before_model_callback(klass, callback) #:nodoc:
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
def self.before_#{callback}(*args, &block)
set_callback(:#{callback}, :before, *args, &block)
end
CALLBACK
end
def _define_around_model_callback(klass, callback) #:nodoc:
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
def self.around_#{callback}(*args, &block)
set_callback(:#{callback}, :around, *args, &block)
end
CALLBACK
end
def _define_after_model_callback(klass, callback) #:nodoc:
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
def self.after_#{callback}(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array.wrap(options[:if]) << "!halted && value != false"
set_callback(:#{callback}, :after, *(args << options), &block)
end
CALLBACK
end
end
end
def create_or_update
run_callbacks(:save) { super }
end
require 'active_support/core_ext/array/wrap'
module ActiveRecord
module Callbacks
extend ActiveSupport::Concern
CALLBACKS = [
:after_initialize, :after_find, :after_touch, :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, :after_commit, :after_rollback
]
included do
extend ActiveModel::Callbacks
include ActiveModel::Validations::Callbacks
define_model_callbacks :initialize, :find, :touch, :only => :after
define_model_callbacks :save, :create, :update, :destroy
end
def destroy #:nodoc:
run_callbacks(:destroy) { super }
end
def touch(*) #:nodoc:
run_callbacks(:touch) { super }
end
private
def create_or_update #:nodoc:
run_callbacks(:save) { super }
end
def create #:nodoc:
run_callbacks(:create) { super }
end
def update(*) #:nodoc:
run_callbacks(:update) { super }
end
end
end
configure do
option :sort do
default 'alphabetical'
end
end
class Option
def default(value)
@default_value = (value.is_a?(Symbol) ? value.to_s : value)
end
def format(value)
@allowed_format = (value.is_a?(Symbol) ? value.to_s : value)
end
def validate!
...
end
end
class Option
include ActiveSupport::Callbacks
...
end
class Option
include ActiveSupport::Callbacks
define_callbacks :validate
...
end
class Option
include ActiveSupport::Callbacks
define_callbacks :validate
set_callback :validate, :after, :validate!
...
end
class Option
include ActiveSupport::Callbacks
define_callbacks :validate
set_callback :validate, :after, :validate!
def default(value)
run_callbacks :validate do
@default_value = (value.is_a?(Symbol) ? value.to_s : value)
end
end
def format(value)
run_callbacks :validate do
@allowed_format = (value.is_a?(Symbol) ? value.to_s : value)
end
end
...
def validate!
...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment