Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dirty tracking of paranoia attributes #375

Open
wants to merge 2 commits into
base: core
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def paranoia_delete
# if a transaction exists, add the record so that after_commit
# callbacks can be run
add_to_transaction
update_columns(paranoia_destroy_attributes)
paranoia_update_columns(paranoia_destroy_attributes)
elsif !frozen?
assign_attributes(paranoia_destroy_attributes)
end
Expand All @@ -112,7 +112,8 @@ def restore!(opts = {})
if within_recovery_window?(recovery_window_range) && ((noop_if_frozen && [email protected]?) || !noop_if_frozen)
@_disable_counter_cache = !paranoia_destroyed?
write_attribute paranoia_column, paranoia_sentinel_value
update_columns(paranoia_restore_attributes)
paranoia_update_columns(paranoia_restore_attributes)
touch
each_counter_cached_associations do |association|
if send(association.reflection.name)
association.increment_counters
Expand Down Expand Up @@ -144,6 +145,14 @@ def paranoia_destroyed?
end
alias :deleted? :paranoia_destroyed?

def paranoia_update_columns(attributes)
attributes.keys.each do |key|
send("#{key}_will_change!")
end
update_columns(attributes)
changes_applied
end

def really_destroy!(update_destroy_attributes: true)
with_transaction_returning_status do
run_callbacks(:real_destroy) do
Expand Down
31 changes: 31 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ def test_update_columns_on_paranoia_destroyed
assert record.update_columns deleted_at: Time.now
end

def test_dirty_tracking_on_paranoia_destroyed
record = ParentModel.create
record.destroy

assert_equal record.previous_changes.keys, ["deleted_at"]
end

def test_scoping_behavior_for_paranoid_models
parent1 = ParentModel.create
parent2 = ParentModel.create
Expand Down Expand Up @@ -330,6 +337,13 @@ def test_destroy_behavior_for_custom_column_models
assert_equal 1, model.class.deleted.count
end

def test_dirty_tracking_for_custom_column_models
record = CustomColumnModel.create
record.destroy

assert_equal record.previous_changes.keys, ["destroyed_at"]
end

def test_destroy_behavior_for_custom_column_models_with_recovery_options
model = CustomColumnModel.new
model.save!
Expand Down Expand Up @@ -377,6 +391,12 @@ def test_active_column_model
assert_equal 1, model.class.deleted.count
end

def test_dirty_tracking_for_active_column_model
record = ActiveColumnModel.create
record.destroy
assert_equal record.previous_changes.keys, ["deleted_at", "active"]
end

def test_active_column_model_with_uniqueness_validation_only_checks_non_deleted_records
a = ActiveColumnModelWithUniquenessValidation.create!(name: "A")
a.destroy
Expand Down Expand Up @@ -571,6 +591,17 @@ def test_restore
assert_equal false, model.paranoia_destroyed?
end

def test_dirty_tracking_on_restore
model = ParanoidModel.new
model.save
id = model.id
model.destroy

model = ParanoidModel.only_deleted.find(id)
model.restore!
assert_equal model.previous_changes.keys, ['deleted_at']
end

def test_restore_on_object_return_self
model = ParanoidModel.create
model.destroy
Expand Down
Loading