aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/timestamp.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-01-03 04:34:43 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-01-03 04:34:43 +0900
commit77ff9a0adbd1318b45203a76c64561b115245603 (patch)
tree85ba92d44decf75d669b68f937e19f58bdc8517a /activerecord/lib/active_record/timestamp.rb
parentb177427d977b21cb18d2eca688539565e8970324 (diff)
downloadrails-77ff9a0adbd1318b45203a76c64561b115245603.tar.gz
rails-77ff9a0adbd1318b45203a76c64561b115245603.tar.bz2
rails-77ff9a0adbd1318b45203a76c64561b115245603.zip
Push `current_time_from_proper_timezone` and timestamp attributes methods up to class method
Actually these methods don't need instantiation.
Diffstat (limited to 'activerecord/lib/active_record/timestamp.rb')
-rw-r--r--activerecord/lib/active_record/timestamp.rb55
1 files changed, 35 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index 030fc35e2f..09d8d1cdd4 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -52,14 +52,41 @@ module ActiveRecord
clear_timestamp_attributes
end
+ class_methods do
+ private
+ def timestamp_attributes_for_create_in_model
+ timestamp_attributes_for_create.select { |c| column_names.include?(c) }
+ end
+
+ def timestamp_attributes_for_update_in_model
+ timestamp_attributes_for_update.select { |c| column_names.include?(c) }
+ end
+
+ def all_timestamp_attributes_in_model
+ timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
+ end
+
+ def timestamp_attributes_for_create
+ ["created_at", "created_on"]
+ end
+
+ def timestamp_attributes_for_update
+ ["updated_at", "updated_on"]
+ end
+
+ def current_time_from_proper_timezone
+ default_timezone == :utc ? Time.now.utc : Time.now
+ end
+ end
+
private
def _create_record
if record_timestamps
current_time = current_time_from_proper_timezone
- all_timestamp_attributes.each do |column|
- if has_attribute?(column) && !attribute_present?(column)
+ all_timestamp_attributes_in_model.each do |column|
+ if !attribute_present?(column)
write_attribute(column, current_time)
end
end
@@ -85,30 +112,22 @@ module ActiveRecord
end
def timestamp_attributes_for_create_in_model
- timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c) }
+ self.class.send(:timestamp_attributes_for_create_in_model)
end
def timestamp_attributes_for_update_in_model
- timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c) }
+ self.class.send(:timestamp_attributes_for_update_in_model)
end
def all_timestamp_attributes_in_model
- timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
- end
-
- def timestamp_attributes_for_update
- ["updated_at", "updated_on"]
- end
-
- def timestamp_attributes_for_create
- ["created_at", "created_on"]
+ self.class.send(:all_timestamp_attributes_in_model)
end
- def all_timestamp_attributes
- timestamp_attributes_for_create + timestamp_attributes_for_update
+ def current_time_from_proper_timezone
+ self.class.send(:current_time_from_proper_timezone)
end
- def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update)
+ def max_updated_column_timestamp(timestamp_names = self.class.send(:timestamp_attributes_for_update))
timestamp_names
.map { |attr| self[attr] }
.compact
@@ -116,10 +135,6 @@ module ActiveRecord
.max
end
- def current_time_from_proper_timezone
- self.class.default_timezone == :utc ? Time.now.utc : Time.now
- end
-
# Clear attributes and changed_attributes
def clear_timestamp_attributes
all_timestamp_attributes_in_model.each do |attribute_name|