Skip to content

Instantly share code, notes, and snippets.

@tonywok
Created September 15, 2011 01:33
Show Gist options
  • Save tonywok/1218304 to your computer and use it in GitHub Desktop.
Save tonywok/1218304 to your computer and use it in GitHub Desktop.
Rails 3.1 Callbacks
s = Test.new(:name => "foo")

# ActiveRecord::Test after_initialize
# ActiveRecord::Test after_initialize, :on => :update
# ActiveRecord::Test after_initialize, :on => :create
# ActiveRecord::TestObserver after_initialize

# ===> #<Test id: nil, name: "foo", created_at: nil, updated_at: nil>

s = Test.create(:name => "foo")

# ActiveRecord::Test after_initialize
# ActiveRecord::Test after_initialize, :on => :update
# ActiveRecord::Test after_initialize, :on => :create
# ActiveRecord::TestObserver after_initialize
# ActiveRecord::Test before_validation
# ActiveRecord::Test before_validation :on => :create
# ActiveRecord::TestObserver before_validation
# ActiveRecord::Test after_validation
# ActiveRecord::Test after_validation :on => :create
# ActiveRecord::TestObserver after_validation
# ActiveRecord::Test before_save :on => :update
# ActiveRecord::Test before_save :on => :create
# ActiveRecord::TestObserver before_save
# ActiveRecord::Test before_create
# ActiveRecord::Test after_create
# ActiveRecord::Test after_save :on => :update
# ActiveRecord::Test after_save :on => :create
# ActiveRecord::TestObserver after_save

# ===> #<Test id: 8, name: "foo", created_at: "2011-09-15 01:23:55", updated_at: "2011-09-15 01:23:55">

s.update_attribute(:name, "foo")

# ActiveRecord::Test before_save :on => :update
# ActiveRecord::Test before_save :on => :create
# ActiveRecord::TestObserver before_save
# ActiveRecord::Test before_update
# ActiveRecord::TestObserver before_update
# ActiveRecord::Test after_update
# ActiveRecord::TestObserver after_update
# ActiveRecord::Test after_save :on => :update
# ActiveRecord::Test after_save :on => :create
# ActiveRecord::TestObserver after_save

# ===> true

s.update_attributes(:name => "bar")

# ActiveRecord::Test before_validation
# ActiveRecord::Test before_validation, :on => :update
# ActiveRecord::TestObserver before_validation
# ActiveRecord::Test after_validation
# ActiveRecord::Test after_validation, :on => :update
# ActiveRecord::TestObserver after_validation
# ActiveRecord::Test before_save :on => :update
# ActiveRecord::Test before_save :on => :create
# ActiveRecord::TestObserver before_save
# ActiveRecord::Test before_update
# ActiveRecord::TestObserver before_update
# ActiveRecord::Test after_update
# ActiveRecord::TestObserver after_update
# ActiveRecord::Test after_save :on => :update
# ActiveRecord::Test after_save :on => :create
# ActiveRecord::TestObserver after_save

#   ===> true

s.save

# ActiveRecord::Test before_validation
# ActiveRecord::Test before_validation, :on => :update
# ActiveRecord::TestObserver before_validation
# ActiveRecord::Test after_validation
# ActiveRecord::Test after_validation, :on => :update
# ActiveRecord::TestObserver after_validation
# ActiveRecord::Test before_save :on => :update
# ActiveRecord::Test before_save :on => :create
# ActiveRecord::TestObserver before_save
# ActiveRecord::Test before_update
# ActiveRecord::TestObserver before_update
# ActiveRecord::Test after_update
# ActiveRecord::TestObserver after_update
# ActiveRecord::Test after_save :on => :update
# ActiveRecord::Test after_save :on => :create
# ActiveRecord::TestObserver after_save

# ===> true
class Test < ActiveRecord::Base
after_initialize :test_after_initialize
after_initialize :test_after_initialize_on_update, :on => :update
after_initialize :test_after_initialize_on_create, :on => :create
before_validation :test_before_validation
after_validation :test_after_validation
before_validation :test_before_validation_on_update, :on => :update
after_validation :test_after_validation_on_update, :on => :update
before_validation :test_before_validation_on_create, :on => :create
after_validation :test_after_validation_on_create, :on => :create
before_update :test_before_update
after_update :test_after_update
before_create :test_before_create
after_create :test_after_create
before_save :test_before_save_on_update, :on => :update
after_save :test_after_save_on_update, :on => :update
before_save :test_before_save_on_create, :on => :create
after_save :test_after_save_on_create, :on => :create
def test_after_initialize
puts "ActiveRecord::Test after_initialize"
end
def test_after_initialize_on_update
puts "ActiveRecord::Test after_initialize, :on => :update"
end
def test_after_initialize_on_create
puts "ActiveRecord::Test after_initialize, :on => :create"
end
def test_before_validation
puts "ActiveRecord::Test before_validation"
end
def test_after_validation
puts "ActiveRecord::Test after_validation"
end
def test_before_validation_on_update
puts "ActiveRecord::Test before_validation, :on => :update"
end
def test_after_validation_on_update
puts "ActiveRecord::Test after_validation, :on => :update"
end
def test_before_update
puts "ActiveRecord::Test before_update"
end
def test_after_update
puts "ActiveRecord::Test after_update"
end
def test_before_create
puts "ActiveRecord::Test before_create"
end
def test_after_create
puts "ActiveRecord::Test after_create"
end
def test_before_save_on_update
puts "ActiveRecord::Test before_save :on => :update"
end
def test_after_save_on_update
puts "ActiveRecord::Test after_save :on => :update"
end
def test_before_save_on_create
puts "ActiveRecord::Test before_save :on => :create"
end
def test_after_save_on_create
puts "ActiveRecord::Test after_save :on => :create"
end
def test_before_validation_on_create
puts "ActiveRecord::Test before_validation :on => :create"
end
def test_after_validation_on_create
puts "ActiveRecord::Test after_validation :on => :create"
end
end
class TestObserver < ActiveRecord::TestObserver
def after_initialize(section)
puts "ActiveRecord::TestObserver after_initialize"
end
def before_save(section)
puts "ActiveRecord::TestObserver before_save"
end
def after_save(section)
puts "ActiveRecord::TestObserver after_save"
end
def before_update(section)
puts "ActiveRecord::TestObserver before_update"
end
def after_update(section)
puts "ActiveRecord::TestObserver after_update"
end
def before_validation(section)
puts "ActiveRecord::TestObserver before_validation"
end
def after_validation(section)
puts "ActiveRecord::TestObserver after_validation"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment