From 7359bccece7e629b102dde40a7ac5807dd85e519 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Dec 2004 16:13:04 +0000 Subject: Added ActiveRecord::Mixins::Touch that will record creation and update times of objects [xal] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@85 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/mixins/touch.rb | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 activerecord/lib/active_record/mixins/touch.rb (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/mixins/touch.rb b/activerecord/lib/active_record/mixins/touch.rb new file mode 100644 index 0000000000..74a1737cab --- /dev/null +++ b/activerecord/lib/active_record/mixins/touch.rb @@ -0,0 +1,35 @@ +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.class_eval do + before_create :touch_on_create + before_update :touch_on_update + + def touch_on_create + self.created_at = self.updated_at = Time.now + end + + def touch_on_update + self.updated_at = Time.now + end + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3