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