mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@312 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
ce58e5cd06
commit
a18b3bf5c0
1 changed files with 0 additions and 34 deletions
|
@ -1,34 +0,0 @@
|
|||
module ActiveRecord
|
||||
module Mixins
|
||||
# Including this mixins will record when objects of the class are created in a datetime column called "created_at"
|
||||
# and when its updated in another datetime column called "updated_at".
|
||||
#
|
||||
# class Bill < ActiveRecord::Base
|
||||
# include ActiveRecord::Mixins::Touch
|
||||
# end
|
||||
#
|
||||
# bill = Bill.create("amount" => 100)
|
||||
# bill.created_at # => Time.now at the moment of Bill.create
|
||||
# bill.updated_at # => Time.now at the moment of Bill.create
|
||||
#
|
||||
# bill.update_attribute("amount", 150)
|
||||
# bill.created_at # => Time.now at the moment of Bill.create
|
||||
# bill.updated_at # => Time.now at the moment of bill.update_attribute
|
||||
module Touch
|
||||
def self.append_features(base)
|
||||
super
|
||||
|
||||
base.before_create :touch_on_create
|
||||
base.before_update :touch_on_update
|
||||
end
|
||||
|
||||
def touch_on_create
|
||||
self.updated_at = (self.created_at ||= Time.now)
|
||||
end
|
||||
|
||||
def touch_on_update
|
||||
self.updated_at = Time.now
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue