Sha256: 82b4cd0c4d1a467517fc81537156665a51837dbd5e7ade000895443b2ee921f7

Contents?: true

Size: 1.86 KB

Versions: 18

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Koi
  module Model
    # Add support for archiving using an `:archived_at` column. Adds scopes for
    # including/excluding collection elements based on whether they have an
    # archived_at date set or not.
    #
    # Usage:
    # Include this module in your model and add the `archived_at` column via a
    # migration.
    #
    # Examples:
    #     Model.all # default scope, excludes archived
    #     Model.archived # only returns archived records
    #     Model.with_archived # returns all records
    module Archivable
      extend ActiveSupport::Concern

      included do
        scope :not_archived, -> { where(archived_at: nil) }
        scope :archived, -> { unscope(where: :archived_at).where.not(archived_at: nil) }
        scope :with_archived, -> { unscope(where: :archived_at) }

        default_scope { not_archived }

        alias_method :archived?, :archived
      end

      # Returns true iff the record has been archived.
      def archived
        archived_at.present?
      end

      # Update archived status based on given boolean value.
      def archived=(archived)
        if ActiveRecord::Type::Boolean.new.cast(archived)
          archive
        else
          restore
        end
      end

      # Mark a record as archived. It will no longer appear in default queries.
      def archive
        self.archived_at = Time.current
      end

      # Archive a record immediately, without validation.
      def archive!
        archive
        save!(validate: false) if persisted?
        self
      end

      # Mark a record as no longer archived. It will appear in default queries.
      def restore
        self.archived_at = nil
      end

      # Restore a record immediately, without validation.
      def restore!
        restore
        save!(validate: false) if persisted?
        self
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
katalyst-koi-4.15.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.15.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.14.3 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.14.2 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.14.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.14.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.13.2 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.13.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.6 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.5 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.4 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.3 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.2 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.12.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.11.2 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.11.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.11.0 app/models/concerns/koi/model/archivable.rb