### Run bundle install Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Install the gem dependencies after adding the gem to the Gemfile. ```shell bundle install ``` -------------------------------- ### Install gem in Gemfile Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Add the acts_as_paranoid gem to the Gemfile. Targets Rails 6.1+ and Ruby 3.1+. ```ruby gem "acts_as_paranoid", "~> 0.11.0" ``` -------------------------------- ### Associations with `with_deleted` option Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Shows how to use `with_deleted: true` on a `belongs_to` association to access soft-deleted parents. The example creates a parent and child, destroys the parent, and demonstrates that the default association returns nil while the `with_deleted` association works. ```ruby class Parent < ActiveRecord::Base has_many :children, class_name: "ParanoiacChild" end class ParanoiacChild < ActiveRecord::Base acts_as_paranoid belongs_to :parent # You may need to provide a foreign_key like this belongs_to :parent_including_deleted, class_name: "Parent", foreign_key: 'parent_id', with_deleted: true end parent = Parent.first child = parent.children.create parent.destroy child.parent #=> nil child.parent_including_deleted #=> Parent (it works!) ``` -------------------------------- ### Recover with `recover!` raising error Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Raises an error if recovery fails, typically an ActiveRecord error. The example shows a validation failure. ```ruby Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover! # => ActiveRecord::RecordInvalid: Validation failed: Name already exists ``` -------------------------------- ### Validate uniqueness without deleted records Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Uses `validates_as_paranoid` and `validates_uniqueness_of_without_deleted` to ignore deleted records in uniqueness checks. The example shows that a deleted record prevents recovery due to validation failure. ```ruby class Paranoiac < ActiveRecord::Base acts_as_paranoid validates_as_paranoid validates_uniqueness_of_without_deleted :name end p1 = Paranoiac.create(name: 'foo') p1.destroy p2 = Paranoiac.new(name: 'foo') p2.valid? #=> true p2.save p1.recover #=> fails validation! ``` -------------------------------- ### Generate migration for deleted_at column Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Create a migration to add a datetime column named deleted_at with an index to the paranoiac table. ```shell bin/rails generate migration AddDeletedAtToParanoiac deleted_at:datetime:index ``` -------------------------------- ### Check fully destroyed status with `destroyed_fully?` and `deleted_fully?` Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Demonstrates the difference between soft-deleted and fully destroyed objects using `deleted?`, `destroyed_fully?`, and `deleted_fully?`. ```ruby Paranoiac.create(name: 'foo').destroy Paranoiac.with_deleted.first.deleted? #=> true Paranoiac.with_deleted.first.destroyed_fully? #=> false p1 = Paranoiac.with_deleted.first p1.destroy # this fully destroys the object p1.destroyed_fully? #=> true p1.deleted_fully? #=> true ``` -------------------------------- ### Work with scopes and `only_deleted` Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Demonstrates chaining scopes with `only_deleted` and shows the effect of `destroy` on counts. ```ruby class Paranoiac < ActiveRecord::Base acts_as_paranoid scope :pretty, where(pretty: true) end Paranoiac.create(pretty: true) Paranoiac.pretty.count #=> 1 Paranoiac.only_deleted.count #=> 0 Paranoiac.pretty.only_deleted.count #=> 0 Paranoiac.first.destroy Paranoiac.pretty.count #=> 0 Paranoiac.only_deleted.count #=> 1 Paranoiac.pretty.only_deleted.count #=> 1 ``` -------------------------------- ### Check deleted status with `deleted?` Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Shows how to check if an object is soft-deleted using `deleted?` after calling `destroy`. ```ruby Paranoiac.create(name: 'foo').destroy Paranoiac.with_deleted.first.deleted? #=> true ``` -------------------------------- ### Enable acts_as_paranoid in model Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Add acts_as_paranoid to the model to enable soft delete functionality. Assumes a datetime column named deleted_at by default. ```ruby class Paranoiac < ActiveRecord::Base acts_as_paranoid end ``` -------------------------------- ### Recover with `recovery_window` option Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Overrides the recovery window for a single recover call using the `recovery_window` option. ```ruby Paranoiac.only_deleted.where("name = ?", "not dead yet").first .recover(recovery_window: 30.seconds) ``` -------------------------------- ### Recover with `raise_error: true` option Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Behaves like `recover!` by raising an error on failure when `raise_error: true` is passed to `recover`. ```ruby Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(raise_error: true) ``` -------------------------------- ### Chain scopes with `with_deleted` Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Shows that `with_deleted` can be chained with other scopes, and that order does not matter. ```ruby Paranoiac.pretty.with_deleted ``` ```ruby Paranoiac.with_deleted.pretty ``` -------------------------------- ### Permanently delete a record Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Use destroy_fully! or delete_all! to permanently delete a record, bypassing soft delete. destroy! is equivalent to destroy and only hides the record. ```ruby paranoiac.destroy_fully! Paranoiac.delete_all!(conditions) ``` -------------------------------- ### Recover a deleted record with `recover` Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Invokes `recover` on a soft-deleted record to restore it. All associations marked as `dependent: :destroy` are recursively recovered by default. ```ruby Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover ``` -------------------------------- ### Filter deleted records with scopes Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Use only_deleted to retrieve only deleted records and with_deleted to retrieve all records including deleted ones. ```ruby Paranoiac.only_deleted # retrieves only the deleted records Paranoiac.with_deleted # retrieves all records, deleted or not ``` -------------------------------- ### Use time-based scopes for deleted records Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md When using the default column_type of 'time', these scopes filter deleted records by time. deleted_inside_time_window takes a time and a duration. ```ruby time = Time.now Paranoiac.deleted_after_time(time) Paranoiac.deleted_before_time(time) # Or roll it all up and get a nice window: Paranoiac.deleted_inside_time_window(time, 2.minutes) ``` -------------------------------- ### Define recovery callbacks in a paranoid model Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Use before_recover and after_recover callbacks in a model that calls acts_as_paranoid. These fire before and after an object is recovered. Default ActiveRecord callbacks such as before_destroy and after_destroy are also triggered around .destroy! and .destroy_fully!. ```ruby class Paranoiac < ActiveRecord::Base acts_as_paranoid before_recover :set_counts after_recover :update_logs end ``` -------------------------------- ### Set `dependent_recovery_window` on model Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Configures the recovery window for dependent records. Dependent records are recovered if deleted within the specified time of the parent's deletion. ```ruby class Paranoiac < ActiveRecord::Base acts_as_paranoid has_many :paranoids, dependent: :destroy end class Paranoid < ActiveRecord::Base belongs_to :paranoic # Paranoid objects will be recovered alongside Paranoic objects # if they were deleted within 10 minutes of the Paranoic object acts_as_paranoid dependent_recovery_window: 10.minutes end ``` -------------------------------- ### Delete a hidden record permanently Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Destroy a record twice to permanently delete it. The first destroy hides it, the second (on only_deleted) removes it from the database. ```ruby p = Paranoiac.first # does NOT delete the first record, just hides it p.destroy # deletes the first record from the database Paranoiac.only_deleted.where(id: p.id).first.destroy ``` -------------------------------- ### Recover with `recursive: false` option Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Disables recursive recovery of dependent associations by passing `recursive: false` to `recover`. ```ruby Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(recursive: false) ``` -------------------------------- ### Set `recover_dependent_associations` default on model Source: https://github.com/actsasparanoid/acts_as_paranoid/blob/master/README.md Changes the default behavior for a model so dependent associations are not recovered unless explicitly requested. ```ruby class Paranoiac < ActiveRecord::Base acts_as_paranoid recover_dependent_associations: false end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.