diff options
author | Josh Kalderimis <josh.kalderimis@gmail.com> | 2011-02-09 15:24:32 +0100 |
---|---|---|
committer | Santiago Pastorino and José Ignacio Costa <santiago+jose@wyeworks.com> | 2011-02-09 18:28:15 -0200 |
commit | 9d8fdfec38a145e3f5074fd8dc0216630c268e32 (patch) | |
tree | 6b45692cd0947d6d8aced25ae055fe93ca51e9db | |
parent | 8bc464c8090f2929917ecd2d9476c914aa6da787 (diff) | |
download | rails-9d8fdfec38a145e3f5074fd8dc0216630c268e32.tar.gz rails-9d8fdfec38a145e3f5074fd8dc0216630c268e32.tar.bz2 rails-9d8fdfec38a145e3f5074fd8dc0216630c268e32.zip |
removed some duplication from LH issue 5505 regarding AR touch and optimistic locking [#5505 state:resolved]
Signed-off-by: Santiago Pastorino and José Ignacio Costa <santiago+jose@wyeworks.com>
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 14 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 6 |
2 files changed, 11 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index e5065de7fb..6b2b1ebafe 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -58,6 +58,12 @@ module ActiveRecord end private + def increment_lock + lock_col = self.class.locking_column + previous_lock_value = send(lock_col).to_i + send(lock_col + '=', previous_lock_value + 1) + end + def attributes_from_column_definition result = super @@ -78,8 +84,8 @@ module ActiveRecord return 0 if attribute_names.empty? lock_col = self.class.locking_column - previous_value = send(lock_col).to_i - send(lock_col + '=', previous_value + 1) + previous_lock_value = send(lock_col).to_i + increment_lock attribute_names += [lock_col] attribute_names.uniq! @@ -89,7 +95,7 @@ module ActiveRecord stmt = relation.where( relation.table[self.class.primary_key].eq(quoted_id).and( - relation.table[lock_col].eq(quote_value(previous_value)) + relation.table[lock_col].eq(quote_value(previous_lock_value)) ) ).arel.compile_update(arel_attributes_values(false, false, attribute_names)) @@ -103,7 +109,7 @@ module ActiveRecord # If something went wrong, revert the version. rescue Exception - send(lock_col + '=', previous_value) + send(lock_col + '=', previous_lock_value) raise end end diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 3fd67078b4..4ccb7461a1 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -233,11 +233,7 @@ module ActiveRecord changes[column.to_s] = write_attribute(column.to_s, current_time) end - if locking_enabled? - lock_col = self.class.locking_column.to_s - previous_value = send(lock_col).to_i - changes[lock_col] = write_attribute(lock_col, previous_value + 1) - end + changes[self.class.locking_column] = increment_lock if locking_enabled? @changed_attributes.except!(*changes.keys) primary_key = self.class.primary_key |