1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Raise StaleObjectError if touched object is stale and locking is enabled

Fixes #19776

change test variable names and use more verbose on method

less verbose

use _read_attribute instead of send
This commit is contained in:
Mehmet Emin İNAÇ 2015-04-16 17:40:52 +03:00
parent c8bab30ce5
commit 5e8d96c523
2 changed files with 25 additions and 3 deletions

View file

@ -477,11 +477,23 @@ module ActiveRecord
changes[column] = write_attribute(column, time) changes[column] = write_attribute(column, time)
end end
changes[self.class.locking_column] = increment_lock if locking_enabled?
clear_attribute_changes(changes.keys) clear_attribute_changes(changes.keys)
primary_key = self.class.primary_key primary_key = self.class.primary_key
self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1 scope = self.class.unscoped.where(primary_key => id)
if locking_enabled?
locking_column = self.class.locking_column
scope = scope.where(locking_column => _read_attribute(locking_column))
changes[locking_column] = increment_lock
end
result = scope.update_all(changes) == 1
if !result && locking_enabled?
raise ActiveRecord::StaleObjectError.new(self, "touch")
end
result
else else
true true
end end

View file

@ -177,6 +177,16 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_equal 1, p1.lock_version assert_equal 1, p1.lock_version
end end
def test_touch_stale_object
person = Person.create!(first_name: 'Mehmet Emin')
stale_person = Person.find(person.id)
person.update_attribute(:gender, 'M')
assert_raises(ActiveRecord::StaleObjectError) do
stale_person.touch
end
end
def test_lock_column_name_existing def test_lock_column_name_existing
t1 = LegacyThing.find(1) t1 = LegacyThing.find(1)
t2 = LegacyThing.find(1) t2 = LegacyThing.find(1)