Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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