Merge pull request #48796 from adrianna-chang-shopify/ac-fix-previously-new-record

Fix `#previously_new_record?` on destroyed records
This commit is contained in:
Eileen M. Uchitelle 2023-07-25 16:55:34 -04:00 committed by GitHub
commit 0c78ab2cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -1,3 +1,11 @@
* Fix `#previously_new_record?` to return true for destroyed records.
Before, if a record was created and then destroyed, `#previously_new_record?` would return true.
Now, any UPDATE or DELETE to a record is considered a change, and will result in `#previously_new_record?`
returning false.
*Adrianna Chang*
* Specify callback in `has_secure_token`
```ruby

View File

@ -661,7 +661,7 @@ module ActiveRecord
end
# Returns true if this object was just created -- that is, prior to the last
# save, the object didn't exist in the database and new_record? would have
# update or delete, the object didn't exist in the database and new_record? would have
# returned true.
def previously_new_record?
@previously_new_record
@ -760,6 +760,7 @@ module ActiveRecord
def delete
_delete_row if persisted?
@destroyed = true
@previously_new_record = false
freeze
end
@ -775,6 +776,7 @@ module ActiveRecord
destroy_associations
@_trigger_destroy_callback ||= persisted? && destroy_row > 0
@destroyed = true
@previously_new_record = false
freeze
end

View File

@ -968,6 +968,14 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal false, Topic.find(1).previously_new_record?
end
def test_previously_new_record_on_destroyed_record
topic = Topic.create
assert_predicate topic, :previously_new_record?
topic.destroy
assert_not_predicate topic, :previously_new_record?
end
def test_previously_persisted_returns_boolean
assert_equal false, Topic.new.previously_persisted?
assert_equal false, Topic.new.destroy.previously_persisted?