Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rase when calling lock! in a dirty record
  • Loading branch information
rafaelfranca committed Oct 23, 2017
1 parent e65aff7 commit 63cf158
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 4 additions & 0 deletions 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*
Expand Down
9 changes: 5 additions & 4 deletions activerecord/lib/active_record/locking/pessimistic.rb
Expand Up @@ -63,12 +63,13 @@ module Pessimistic
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
Expand Down
22 changes: 11 additions & 11 deletions activerecord/test/cases/locking_test.rb
Expand Up @@ -565,18 +565,18 @@ def test_eager_find_with_lock
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

Expand Down

0 comments on commit 63cf158

Please sign in to comment.