aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/acts/touch.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-16 16:23:59 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-16 16:23:59 +0000
commit6860db61f516b813af624d38b47fef0bf983539c (patch)
treeaf6fa07944f5af223b96351a629fc38323870137 /activerecord/lib/active_record/acts/touch.rb
parent12f10f6baf7e38c74db24585d901063cd094867b (diff)
downloadrails-6860db61f516b813af624d38b47fef0bf983539c.tar.gz
rails-6860db61f516b813af624d38b47fef0bf983539c.tar.bz2
rails-6860db61f516b813af624d38b47fef0bf983539c.zip
Renamed Mixins to Acts to resemble the change from include ActiveRecord::Mixins::List to acts_as_list and renamed @@default_error_messagess to @@default_error_messages
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@190 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/acts/touch.rb')
-rw-r--r--activerecord/lib/active_record/acts/touch.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/acts/touch.rb b/activerecord/lib/active_record/acts/touch.rb
new file mode 100644
index 0000000000..baf217542a
--- /dev/null
+++ b/activerecord/lib/active_record/acts/touch.rb
@@ -0,0 +1,34 @@
+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 \ No newline at end of file