Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/tags/rel_2-0-2/activerecord/lib/active_record/timestamp.rb

Revision 8217, 1.6 kB (checked in by bitsweat, 1 year ago)

Make the record_timestamps class-inheritable so it can be set per model. References #10004.

Line 
1 module ActiveRecord
2   # Active Record automatically timestamps create and update operations if the table has fields
3   # named created_at/created_on or updated_at/updated_on.
4   #
5   # Timestamping can be turned off by setting
6   #   <tt>ActiveRecord::Base.record_timestamps = false</tt>
7   #
8   # Timestamps are in the local timezone by default but can use UTC by setting
9   #   <tt>ActiveRecord::Base.default_timezone = :utc</tt>
10   module Timestamp
11     def self.included(base) #:nodoc:
12       base.alias_method_chain :create, :timestamps
13       base.alias_method_chain :update, :timestamps
14
15       base.class_inheritable_accessor :record_timestamps, :instance_writer => false
16       base.record_timestamps = true
17     end
18
19     private
20       def create_with_timestamps #:nodoc:
21         if record_timestamps
22           t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
23           write_attribute('created_at', t) if respond_to?(:created_at) && created_at.nil?
24           write_attribute('created_on', t) if respond_to?(:created_on) && created_on.nil?
25
26           write_attribute('updated_at', t) if respond_to?(:updated_at)
27           write_attribute('updated_on', t) if respond_to?(:updated_on)
28         end
29         create_without_timestamps
30       end
31
32       def update_with_timestamps #:nodoc:
33         if record_timestamps
34           t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
35           write_attribute('updated_at', t) if respond_to?(:updated_at)
36           write_attribute('updated_on', t) if respond_to?(:updated_on)
37         end
38         update_without_timestamps
39       end
40   end
41 end
Note: See TracBrowser for help on using the browser.