diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-08 16:13:04 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-08 16:13:04 +0000 |
commit | 7359bccece7e629b102dde40a7ac5807dd85e519 (patch) | |
tree | a7a36d30a49a8801f62d6e41dc6128bd8d6edf3c /activerecord | |
parent | 1cf7eb1ca84a65fead68104572bc2cbac1fe95a0 (diff) | |
download | rails-7359bccece7e629b102dde40a7ac5807dd85e519.tar.gz rails-7359bccece7e629b102dde40a7ac5807dd85e519.tar.bz2 rails-7359bccece7e629b102dde40a7ac5807dd85e519.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 16 | ||||
-rw-r--r-- | activerecord/lib/active_record/mixins/touch.rb | 35 |
2 files changed, 50 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 1d73dde70a..eaaa2027b4 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,19 @@ *CVS* +* Added ActiveRecord::Mixins::Touch that will record creation and update times of objects [xal]. Example: + + 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 + * Added ActiveRecord::Mixins::List that can decorates an existing class with methods like move_higher/lower, move_to_top/bottom. Example: class TodoItem < ActiveRecord::Base @@ -11,7 +25,7 @@ "todo_list_id = #{todo_list_id}" end end - + * Added the option for sanitizing find_by_sql and the offset parts in regular finds [Sam Stephenson]. Examples: Project.find_all ["category = ?", category_name], "created ASC", ["? OFFSET ?", 15, 20] 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 |