aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-07-13 08:05:09 +0200
committerJosé Valim <jose.valim@gmail.com>2010-07-13 08:05:09 +0200
commit34013115626b2632ffe8ec357e2c5f47cfa059b2 (patch)
tree56c1c61fa0eebc8ffffaa004a204c10be6f26501 /activerecord/lib/active_record
parent1d45ea081493cd4eca95cd75cce7be7b8d9cb07c (diff)
downloadrails-34013115626b2632ffe8ec357e2c5f47cfa059b2.tar.gz
rails-34013115626b2632ffe8ec357e2c5f47cfa059b2.tar.bz2
rails-34013115626b2632ffe8ec357e2c5f47cfa059b2.zip
Tidying up a bit, so update_attribute is not called twice on touch.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/persistence.rb18
-rw-r--r--activerecord/lib/active_record/timestamp.rb24
2 files changed, 17 insertions, 25 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 44264bec7f..e53cc5ee8c 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -105,19 +105,17 @@ module ActiveRecord
# Updates a single attribute and saves the record without going through the normal validation procedure
# or callbacks. This is especially useful for boolean flags on existing records.
def update_attribute(name, value)
- send("#{name}=", value)
- hash = { name => read_attribute(name) }
-
- if record_update_timestamps
- timestamp_attributes_for_update_in_model.each do |column|
- hash[column] = read_attribute(column)
- @changed_attributes.delete(column.to_s)
- end
+ changes = record_update_timestamps || {}
+
+ if name
+ name = name.to_s
+ send("#{name}=", value)
+ changes[name] = read_attribute(name)
end
- @changed_attributes.delete(name.to_s)
+ @changed_attributes.except!(*changes.keys)
primary_key = self.class.primary_key
- self.class.update_all(hash, { primary_key => self[primary_key] }) == 1
+ self.class.update_all(changes, { primary_key => self[primary_key] }) == 1
end
# Updates all the attributes from the passed-in Hash and saves the record.
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index 5cddd07e82..341cc87be5 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -30,16 +30,11 @@ module ActiveRecord
# product.touch # updates updated_at/on
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
def touch(attribute = nil)
- current_time = current_time_from_proper_timezone
-
- if attribute
- self.update_attribute(attribute, current_time)
- else
- timestamp_attributes_for_update_in_model.each { |column| self.update_attribute(column, current_time) }
- end
+ update_attribute(attribute, current_time_from_proper_timezone)
end
private
+
def create #:nodoc:
if record_timestamps
current_time = current_time_from_proper_timezone
@@ -56,17 +51,16 @@ module ActiveRecord
end
def update(*args) #:nodoc:
- record_update_timestamps
+ record_update_timestamps if !partial_updates? || changed?
super
end
- def record_update_timestamps
- if record_timestamps && (!partial_updates? || changed?)
- current_time = current_time_from_proper_timezone
- timestamp_attributes_for_update_in_model.each { |column| write_attribute(column.to_s, current_time) }
- true
- else
- false
+ def record_update_timestamps #:nodoc:
+ return unless record_timestamps
+ current_time = current_time_from_proper_timezone
+ timestamp_attributes_for_update_in_model.inject({}) do |hash, column|
+ hash[column.to_s] = write_attribute(column.to_s, current_time)
+ hash
end
end