Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activerecord / lib / active_record / timestamp.rb
blobee3e426512057682a61606eae70fb756bfd4c10a
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
15       base.cattr_accessor :record_timestamps, :instance_writer => false
16       base.record_timestamps = true
17     end
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?
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
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