aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2017-08-23 16:08:34 -0400
committerRafael Mendonça França <rafaelmfranca@gmail.com>2017-10-23 12:50:46 -0400
commit63cf15877bae859ff7b4ebaf05186f3ca79c1863 (patch)
treea44d48a738159d2db928fc5aa68bd24b85fc8679
parente65aff70696be52b46ebe57207ebd8bb2cfcdbb6 (diff)
downloadrails-63cf15877bae859ff7b4ebaf05186f3ca79c1863.tar.gz
rails-63cf15877bae859ff7b4ebaf05186f3ca79c1863.tar.bz2
rails-63cf15877bae859ff7b4ebaf05186f3ca79c1863.zip
Rase when calling `lock!` in a dirty record
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/locking/pessimistic.rb9
-rw-r--r--activerecord/test/cases/locking_test.rb22
3 files changed, 20 insertions, 15 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 7d0b75ddbf..9a58ebd100 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Raises when calling `lock!` in a dirty record.
+
+ *Rafael Mendonça França*
+
* Remove deprecated support to passing a class to `:class_name` on associations.
*Rafael Mendonça França*
diff --git a/activerecord/lib/active_record/locking/pessimistic.rb b/activerecord/lib/active_record/locking/pessimistic.rb
index 72bccd4906..bb85c47e06 100644
--- a/activerecord/lib/active_record/locking/pessimistic.rb
+++ b/activerecord/lib/active_record/locking/pessimistic.rb
@@ -63,12 +63,13 @@ module ActiveRecord
def lock!(lock = true)
if persisted?
if changed?
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
- Locking a record with unpersisted changes is deprecated and will raise an
- exception in Rails 5.2. Use `save` to persist the changes, or `reload` to
- discard them explicitly.
+ raise(<<-MSG.squish)
+ Locking a record with unpersisted changes is not supported. Use
+ `save` to persist the changes, or `reload` to discard them
+ explicitly.
MSG
end
+
reload(lock: lock)
end
self
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb
index 743680ba92..6791d50940 100644
--- a/activerecord/test/cases/locking_test.rb
+++ b/activerecord/test/cases/locking_test.rb
@@ -565,18 +565,18 @@ unless in_memory_db?
end
end
- # Locking a record reloads it.
- def test_sane_lock_method
+ def test_lock_does_not_raise_when_the_object_is_not_dirty
+ person = Person.find 1
assert_nothing_raised do
- Person.transaction do
- person = Person.find 1
- old, person.first_name = person.first_name, "fooman"
- # Locking a dirty record is deprecated
- assert_deprecated do
- person.lock!
- end
- assert_equal old, person.first_name
- end
+ person.lock!
+ end
+ end
+
+ def test_lock_raises_when_the_record_is_dirty
+ person = Person.find 1
+ person.first_name = "fooman"
+ assert_raises(RuntimeError) do
+ person.lock!
end
end