Permalink
Browse files

removed some duplication from LH issue 5505 regarding AR touch and op…

…timistic locking [#5505 state:resolved]

Signed-off-by: Santiago Pastorino and José Ignacio Costa <santiago+jose@wyeworks.com>
  • Loading branch information...
1 parent 8bc464c commit 9d8fdfec38a145e3f5074fd8dc0216630c268e32 @joshk joshk committed with Santiago Pastorino and José Ignacio Costa Feb 9, 2011
@@ -58,6 +58,12 @@ def locking_enabled? #:nodoc:
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 @@ def update(attribute_names = @attributes.keys) #:nodoc:
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 @@ def update(attribute_names = @attributes.keys) #:nodoc:
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 @@ def update(attribute_names = @attributes.keys) #:nodoc:
# If something went wrong, revert the version.
rescue Exception
- send(lock_col + '=', previous_value)
+ send(lock_col + '=', previous_lock_value)
raise
end
end
@@ -233,11 +233,7 @@ def touch(name = nil)
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

0 comments on commit 9d8fdfe

Please sign in to comment.