Skip to content

Commit 77ff9a0

Browse files
committed
Push current_time_from_proper_timezone and timestamp attributes methods up to class method
Actually these methods don't need instantiation.
1 parent b177427 commit 77ff9a0

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

activerecord/lib/active_record/timestamp.rb

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,41 @@ def initialize_dup(other) # :nodoc:
5252
clear_timestamp_attributes
5353
end
5454

55+
class_methods do
56+
private
57+
def timestamp_attributes_for_create_in_model
58+
timestamp_attributes_for_create.select { |c| column_names.include?(c) }
59+
end
60+
61+
def timestamp_attributes_for_update_in_model
62+
timestamp_attributes_for_update.select { |c| column_names.include?(c) }
63+
end
64+
65+
def all_timestamp_attributes_in_model
66+
timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
67+
end
68+
69+
def timestamp_attributes_for_create
70+
["created_at", "created_on"]
71+
end
72+
73+
def timestamp_attributes_for_update
74+
["updated_at", "updated_on"]
75+
end
76+
77+
def current_time_from_proper_timezone
78+
default_timezone == :utc ? Time.now.utc : Time.now
79+
end
80+
end
81+
5582
private
5683

5784
def _create_record
5885
if record_timestamps
5986
current_time = current_time_from_proper_timezone
6087

61-
all_timestamp_attributes.each do |column|
62-
if has_attribute?(column) && !attribute_present?(column)
88+
all_timestamp_attributes_in_model.each do |column|
89+
if !attribute_present?(column)
6390
write_attribute(column, current_time)
6491
end
6592
end
@@ -85,41 +112,29 @@ def should_record_timestamps?
85112
end
86113

87114
def timestamp_attributes_for_create_in_model
88-
timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c) }
115+
self.class.send(:timestamp_attributes_for_create_in_model)
89116
end
90117

91118
def timestamp_attributes_for_update_in_model
92-
timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c) }
119+
self.class.send(:timestamp_attributes_for_update_in_model)
93120
end
94121

95122
def all_timestamp_attributes_in_model
96-
timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
97-
end
98-
99-
def timestamp_attributes_for_update
100-
["updated_at", "updated_on"]
101-
end
102-
103-
def timestamp_attributes_for_create
104-
["created_at", "created_on"]
123+
self.class.send(:all_timestamp_attributes_in_model)
105124
end
106125

107-
def all_timestamp_attributes
108-
timestamp_attributes_for_create + timestamp_attributes_for_update
126+
def current_time_from_proper_timezone
127+
self.class.send(:current_time_from_proper_timezone)
109128
end
110129

111-
def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update)
130+
def max_updated_column_timestamp(timestamp_names = self.class.send(:timestamp_attributes_for_update))
112131
timestamp_names
113132
.map { |attr| self[attr] }
114133
.compact
115134
.map(&:to_time)
116135
.max
117136
end
118137

119-
def current_time_from_proper_timezone
120-
self.class.default_timezone == :utc ? Time.now.utc : Time.now
121-
end
122-
123138
# Clear attributes and changed_attributes
124139
def clear_timestamp_attributes
125140
all_timestamp_attributes_in_model.each do |attribute_name|

activerecord/test/cases/timestamp_test.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -430,21 +430,6 @@ def test_timestamp_column_values_are_present_in_the_callbacks
430430
assert_not_equal person.born_at, nil
431431
end
432432

433-
def test_timestamp_attributes_for_create
434-
toy = Toy.first
435-
assert_equal ["created_at", "created_on"], toy.send(:timestamp_attributes_for_create)
436-
end
437-
438-
def test_timestamp_attributes_for_update
439-
toy = Toy.first
440-
assert_equal ["updated_at", "updated_on"], toy.send(:timestamp_attributes_for_update)
441-
end
442-
443-
def test_all_timestamp_attributes
444-
toy = Toy.first
445-
assert_equal ["created_at", "created_on", "updated_at", "updated_on"], toy.send(:all_timestamp_attributes)
446-
end
447-
448433
def test_timestamp_attributes_for_create_in_model
449434
toy = Toy.first
450435
assert_equal ["created_at"], toy.send(:timestamp_attributes_for_create_in_model)

0 commit comments

Comments
 (0)