mirror of https://github.com/rails/rails
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:
commit
0c78ab2cc7
|
@ -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`
|
* Specify callback in `has_secure_token`
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|
|
@ -661,7 +661,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns true if this object was just created -- that is, prior to the last
|
# 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.
|
# returned true.
|
||||||
def previously_new_record?
|
def previously_new_record?
|
||||||
@previously_new_record
|
@previously_new_record
|
||||||
|
@ -760,6 +760,7 @@ module ActiveRecord
|
||||||
def delete
|
def delete
|
||||||
_delete_row if persisted?
|
_delete_row if persisted?
|
||||||
@destroyed = true
|
@destroyed = true
|
||||||
|
@previously_new_record = false
|
||||||
freeze
|
freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -775,6 +776,7 @@ module ActiveRecord
|
||||||
destroy_associations
|
destroy_associations
|
||||||
@_trigger_destroy_callback ||= persisted? && destroy_row > 0
|
@_trigger_destroy_callback ||= persisted? && destroy_row > 0
|
||||||
@destroyed = true
|
@destroyed = true
|
||||||
|
@previously_new_record = false
|
||||||
freeze
|
freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -968,6 +968,14 @@ class BasicsTest < ActiveRecord::TestCase
|
||||||
assert_equal false, Topic.find(1).previously_new_record?
|
assert_equal false, Topic.find(1).previously_new_record?
|
||||||
end
|
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
|
def test_previously_persisted_returns_boolean
|
||||||
assert_equal false, Topic.new.previously_persisted?
|
assert_equal false, Topic.new.previously_persisted?
|
||||||
assert_equal false, Topic.new.destroy.previously_persisted?
|
assert_equal false, Topic.new.destroy.previously_persisted?
|
||||||
|
|
Loading…
Reference in New Issue