You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
docuseal/app/models/concerns/soft_deletable.rb

43 lines
1.1 KiB

# frozen_string_literal: true
# SoftDeletable Concern
# Purpose: Provides soft delete functionality for models
# Usage: include SoftDeletable in any model with a deleted_at column
module SoftDeletable
extend ActiveSupport::Concern
included do
# Default scope to exclude soft-deleted records
default_scope { where(deleted_at: nil) }
# Scopes for querying soft-deleted records
scope :active, -> { where(deleted_at: nil) }
scope :archived, -> { unscope(where: :deleted_at).where.not(deleted_at: nil) }
scope :with_archived, -> { unscope(where: :deleted_at) }
end
# Soft delete the record by setting deleted_at timestamp
# @return [Boolean] true if successful
def soft_delete
update(deleted_at: Time.current)
end
# Restore a soft-deleted record
# @return [Boolean] true if successful
def restore
update(deleted_at: nil)
end
# Check if record is soft-deleted
# @return [Boolean] true if deleted_at is present
def deleted?
deleted_at.present?
end
# Check if record is active (not soft-deleted)
# @return [Boolean] true if deleted_at is nil
def active?
deleted_at.nil?
end
end