Skip to content

Instantly share code, notes, and snippets.

@xifengzhu
Created December 25, 2014 02:32
Show Gist options
  • Save xifengzhu/56f236a1cf5a8405b4c0 to your computer and use it in GitHub Desktop.
Save xifengzhu/56f236a1cf5a8405b4c0 to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'active_support/callbacks'
class Record
include ActiveSupport::Callbacks
define_callbacks :save
# define_callbacks :kill
def save
run_callbacks :save do
puts "- save"
end
end
set_callback :save, :before, :saving_message
def saving_message
puts "saving..."
return true
end
set_callback :save, :after do |object|
puts object
puts "saved"
end
# def kill
# run_callbacks :kill do
# puts "- kill"
# end
# end
# set_callback :kill, :before, :kill_message
# def kill_message
# puts "before kill....."
# end
end
person = Record.new
person.save
person.kill
# ---output---
# saving...
# - save
# saved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment