Sha256: b15ba012028515886a64edcbc3aa511157dbc33302a8eb8a4c335cfed07ea935

Contents?: true

Size: 1.32 KB

Versions: 37

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Koi
  module Model
    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)
      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)
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
katalyst-koi-4.8.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.8.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.7.3 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.6.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.9 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.8 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.7 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.6 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.5 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.4 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.3 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.2 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.0.beta.2 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.5.0.beta.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.4.1 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.4.0 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.3.5 app/models/concerns/koi/model/archivable.rb
katalyst-koi-4.3.4 app/models/concerns/koi/model/archivable.rb