Skip to content

Instantly share code, notes, and snippets.

@nicholasf
Created August 19, 2009 04:03
Show Gist options
  • Save nicholasf/170152 to your computer and use it in GitHub Desktop.
Save nicholasf/170152 to your computer and use it in GitHub Desktop.
# callbacks based on http://snippets.dzone.com/posts/show/5226 and
# http://github.com/benhoskings/hammock/tree/master/lib/hammock/callbacks.rb (ben_h)
module Trashable
include ActiveSupport::Callbacks
define_callbacks :before_trashing
def trash!
run_callbacks(:before_trashing) {|result, object| result == false}
self.trashed = true
self.save!
end
def recycle!
self.trashed = false
self.save!
end
def trashed?
self.trashed
end
def self.included(base)
base.class_eval {
include ActiveSupport::Callbacks
class_eval <<-"end_eval"
def self.before_trashing(*methods, &block)
callbacks = CallbackChain.build(:before_trashing, *methods, &block)
@before_trashing_callbacks ||= CallbackChain.new
@before_trashing_callbacks.concat callbacks
end
def self.before_trashing_callback_chain
@before_trashing_callbacks ||= CallbackChain.new
if superclass.respond_to?(:before_trashing_callback_chain)
CallbackChain.new(superclass.before_trashing_callback_chain + @before_trashing_callbacks)
else
@before_trashing_callbacks
end
end
end_eval
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment