From 09e3a9fb3b3b47c976388c103df05438267dacc6 Mon Sep 17 00:00:00 2001 From: Bogdanov Anton Date: Wed, 16 Nov 2022 03:11:40 +0400 Subject: [PATCH] skip updating paranoia_destroy_attributes for records while really_destroy! (#535) --- CHANGELOG.md | 5 +++++ README.md | 10 +++++++++- lib/paranoia.rb | 10 ++++++---- lib/paranoia/version.rb | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cec5dd1..79424e3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # paranoia Changelog +## 2.6.1 + +* [#535](https://github.com/rubysherpas/paranoia/pull/535) Allow to skip updating paranoia_destroy_attributes for records while really_destroy! + [Anton Bogdanov](https://github.com/kortirso) + ## 2.6.0 * [#512](https://github.com/rubysherpas/paranoia/pull/512) Quote table names; Mysql 8 has keywords that might match table names which cause an exception. diff --git a/README.md b/README.md index cc6746e8..cf898b8f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ [![Gem Version](https://badge.fury.io/rb/paranoia.svg)](https://badge.fury.io/rb/paranoia) [![build](https://github.com/rubysherpas/paranoia/actions/workflows/build.yml/badge.svg)](https://github.com/rubysherpas/paranoia/actions/workflows/build.yml) -**Notice:** +**Notice:** `paranoia` has some surprising behaviour (like overriding ActiveRecord's `delete` and `destroy`) and is not recommended for new projects. See [`discard`'s README](https://github.com/jhawthorn/discard#why-not-paranoia-or-acts_as_paranoid) for more details. @@ -103,6 +103,14 @@ If you really want it gone *gone*, call `really_destroy!`: # => client ``` +If you need skip updating timestamps for deleting records, call `really_destroy!(update_destroy_attributes: false)`. +When we call `really_destroy!(update_destroy_attributes: false)` on the parent `client`, then each child `email` will also have `really_destroy!(update_destroy_attributes: false)` called. + +``` ruby +>> client.really_destroy!(update_destroy_attributes: false) +# => client +``` + If you want to use a column other than `deleted_at`, you can pass it as an option: ``` ruby diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 57bdf16e..a7e87c77 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -144,7 +144,7 @@ def paranoia_destroyed? end alias :deleted? :paranoia_destroyed? - def really_destroy! + def really_destroy!(update_destroy_attributes: true) with_transaction_returning_status do run_callbacks(:real_destroy) do @_disable_counter_cache = paranoia_destroyed? @@ -158,12 +158,14 @@ def really_destroy! # .paranoid? will work for both instances and classes next unless association_data && association_data.paranoid? if reflection.collection? - next association_data.with_deleted.each(&:really_destroy!) + next association_data.with_deleted.find_each { |record| + record.really_destroy!(update_destroy_attributes: update_destroy_attributes) + } end - association_data.really_destroy! + association_data.really_destroy!(update_destroy_attributes: update_destroy_attributes) end end - update_columns(paranoia_destroy_attributes) + update_columns(paranoia_destroy_attributes) if update_destroy_attributes destroy_without_paranoia end end diff --git a/lib/paranoia/version.rb b/lib/paranoia/version.rb index 9b8b6061..e9227a35 100644 --- a/lib/paranoia/version.rb +++ b/lib/paranoia/version.rb @@ -1,3 +1,3 @@ module Paranoia - VERSION = '2.6.0'.freeze + VERSION = '2.6.1'.freeze end