Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (100 sloc) 3.542 kb
d7db6a8 @joshk class inheritable attributes is used no more! all internal use of class ...
joshk authored
1
d8a58ee @dhh git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@177 5ecf4fe2-1...
dhh authored
2 module ActiveRecord
4e3eec3 @rizwanreza Adds title and proper formatting to docs.
rizwanreza authored
3 # = Active Record Timestamp
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
4 #
4e3eec3 @rizwanreza Adds title and proper formatting to docs.
rizwanreza authored
5 # Active Record automatically timestamps create and update operations if the
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
6 # table has fields named <tt>created_at/created_on</tt> or
4e3eec3 @rizwanreza Adds title and proper formatting to docs.
rizwanreza authored
7 # <tt>updated_at/updated_on</tt>.
8 #
9 # Timestamping can be turned off by setting:
f389a8f @dhh Updated and added more documentation
dhh authored
10 #
c56e314 Updates to ActiveRecord::Timestamp documentation.
Brian Morearty authored
11 # config.active_record.record_timestamps = false
5fe64dd @jeremy r4724@asus: jeremy | 2006-06-28 19:22:46 -0700
jeremy authored
12 #
4e3eec3 @rizwanreza Adds title and proper formatting to docs.
rizwanreza authored
13 # Timestamps are in the local timezone by default but you can use UTC by setting:
14 #
c56e314 Updates to ActiveRecord::Timestamp documentation.
Brian Morearty authored
15 # config.active_record.default_timezone = :utc
e429453 @neerajdotname moving time zone related documentation to timestamp.rb from base.rb
neerajdotname authored
16 #
17 # == Time Zone aware attributes
18 #
19 # By default, ActiveRecord::Base keeps all the datetime columns time zone aware by executing following code.
20 #
c56e314 Updates to ActiveRecord::Timestamp documentation.
Brian Morearty authored
21 # config.active_record.time_zone_aware_attributes = true
e429453 @neerajdotname moving time zone related documentation to timestamp.rb from base.rb
neerajdotname authored
22 #
23 # This feature can easily be turned off by assigning value <tt>false</tt> .
24 #
c56e314 Updates to ActiveRecord::Timestamp documentation.
Brian Morearty authored
25 # If your attributes are time zone aware and you desire to skip time zone conversion to the current Time.zone
26 # when reading certain attributes then you can do following:
e429453 @neerajdotname moving time zone related documentation to timestamp.rb from base.rb
neerajdotname authored
27 #
c56e314 Updates to ActiveRecord::Timestamp documentation.
Brian Morearty authored
28 # class Topic < ActiveRecord::Base
29 # self.skip_time_zone_conversion_for_attributes = [:written_on]
30 # end
3a38c82 @jeremy Closes #3784.
jeremy authored
31 module Timestamp
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" DSL...
josh authored
32 extend ActiveSupport::Concern
5fe64dd @jeremy r4724@asus: jeremy | 2006-06-28 19:22:46 -0700
jeremy authored
33
a2875be @brynary Use DependencyModule for included hooks in ActiveRecord
brynary authored
34 included do
9e4c41c @jonleighton Remove ActiveRecord::Model
jonleighton authored
35 class_attribute :record_timestamps
36 self.record_timestamps = true
3a38c82 @jeremy Closes #3784.
jeremy authored
37 end
fdb61f0 @dhh Added ActiveRecord::Base#touch to update the updated_at/on attributes wi...
dhh authored
38
efef82f @frodsan fix :nodoc:s in AR::Timestamp [ci skip]
frodsan authored
39 def initialize_dup(other) # :nodoc:
c59c9bb @jonleighton Move clear_timestamp_attributes into Timestamp module
jonleighton authored
40 clear_timestamp_attributes
fb66521 @csmuc Call super to nullify the reference to the original errors object in the...
csmuc authored
41 super
c59c9bb @jonleighton Move clear_timestamp_attributes into Timestamp module
jonleighton authored
42 end
43
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
44 private
3401311 @josevalim Tidying up a bit, so update_attribute is not called twice on touch.
josevalim authored
45
1f3a1fe Rename update_attributes method to update, keep update_attributes as an ...
Amparo Luna + Guillermo Iguaran authored
46 def create_record
d7db6a8 @joshk class inheritable attributes is used no more! all internal use of class ...
joshk authored
47 if self.record_timestamps
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
48 current_time = current_time_from_proper_timezone
3a38c82 @jeremy Closes #3784.
jeremy authored
49
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
50 all_timestamp_attributes.each do |column|
50d395f @jonleighton Raise error when using write_attribute with a non-existent attribute.
jonleighton authored
51 if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
52 write_attribute(column.to_s, current_time)
53 end
8bb3b63 @neerajdotname Timestamp columns of HABTM join table should record timestamps
neerajdotname authored
54 end
c8b4cf6 @dhh Speed up timestamping a tad #1227 [skaen]
dhh authored
55 end
d8a58ee @dhh git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@177 5ecf4fe2-1...
dhh authored
56
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
57 super
58 end
fdb61f0 @dhh Added ActiveRecord::Base#touch to update the updated_at/on attributes wi...
dhh authored
59
1f3a1fe Rename update_attributes method to update, keep update_attributes as an ...
Amparo Luna + Guillermo Iguaran authored
60 def update_record(*args)
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
61 if should_record_timestamps?
62 current_time = current_time_from_proper_timezone
63
64 timestamp_attributes_for_update_in_model.each do |column|
65 column = column.to_s
66 next if attribute_changed?(column)
67 write_attribute(column, current_time)
68 end
69 end
01629d1 @neerajdotname This patch changes update_attribute implementatino so:
neerajdotname authored
70 super
71 end
72
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
73 def should_record_timestamps?
7efb1fe @jonleighton Rename the partial_updates config to partial_writes
jonleighton authored
74 self.record_timestamps && (!partial_writes? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?)
01629d1 @neerajdotname This patch changes update_attribute implementatino so:
neerajdotname authored
75 end
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
76
5178e64 @franckverrot Added timestamp_attributes_for_create_in_model
franckverrot authored
77 def timestamp_attributes_for_create_in_model
78 timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c.to_s) }
79 end
80
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
81 def timestamp_attributes_for_update_in_model
573fd39 @lifo Make sure Model#touch doesn't try to update non existing columns
lifo authored
82 timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c.to_s) }
8bb3b63 @neerajdotname Timestamp columns of HABTM join table should record timestamps
neerajdotname authored
83 end
84
a5b03e9 @franckverrot Implement and test private method all_timestamp_attributes_in_model
franckverrot authored
85 def all_timestamp_attributes_in_model
86 timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
87 end
88
efef82f @frodsan fix :nodoc:s in AR::Timestamp [ci skip]
frodsan authored
89 def timestamp_attributes_for_update
8bb3b63 @neerajdotname Timestamp columns of HABTM join table should record timestamps
neerajdotname authored
90 [:updated_at, :updated_on]
91 end
92
efef82f @frodsan fix :nodoc:s in AR::Timestamp [ci skip]
frodsan authored
93 def timestamp_attributes_for_create
8bb3b63 @neerajdotname Timestamp columns of HABTM join table should record timestamps
neerajdotname authored
94 [:created_at, :created_on]
95 end
96
efef82f @frodsan fix :nodoc:s in AR::Timestamp [ci skip]
frodsan authored
97 def all_timestamp_attributes
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
98 timestamp_attributes_for_create + timestamp_attributes_for_update
690352d @neerajdotname consolidating updated_at and updated_on
neerajdotname authored
99 end
e4943e9 @josevalim Make update_attribute behave as in Rails 2.3 and document the behavior i...
josevalim authored
100
1dc98c1 @bemurphy cache_key consults updated_on timestamp if present
bemurphy authored
101 def max_updated_column_timestamp
102 if (timestamps = timestamp_attributes_for_update.map { |attr| self[attr] }.compact).present?
103 timestamps.map { |ts| ts.to_time }.max
104 end
105 end
106
efef82f @frodsan fix :nodoc:s in AR::Timestamp [ci skip]
frodsan authored
107 def current_time_from_proper_timezone
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
108 self.class.default_timezone == :utc ? Time.now.utc : Time.now
109 end
c59c9bb @jonleighton Move clear_timestamp_attributes into Timestamp module
jonleighton authored
110
111 # Clear attributes and changed_attributes
112 def clear_timestamp_attributes
113 all_timestamp_attributes_in_model.each do |attribute_name|
114 self[attribute_name] = nil
115 changed_attributes.delete(attribute_name)
116 end
117 end
3a38c82 @jeremy Closes #3784.
jeremy authored
118 end
690352d @neerajdotname consolidating updated_at and updated_on
neerajdotname authored
119 end
Something went wrong with that request. Please try again.