Skip to content

Instantly share code, notes, and snippets.

@ryenski
Created December 5, 2018 13:04
Show Gist options
  • Save ryenski/933745206091f325dc245e124e40dc30 to your computer and use it in GitHub Desktop.
Save ryenski/933745206091f325dc245e124e40dc30 to your computer and use it in GitHub Desktop.
module Archivable
extend ActiveSupport::Concern
included do
scope :archived, ->{ unscope(where: :archived_at).where.not(archived_at: nil) }
scope :without_archived, ->{ where(archived_at: nil) }
scope :with_archived, ->{ unscope(where: :archived_at) }
default_scope { without_archived }
end
def archive!
update_column(:archived_at, Time.zone.now)
broadcast(:after_archive, self)
broadcast("archive_#{broadcast_model_name_key}_successful", self)
end
def restore!
update_column(:archived_at, nil)
broadcast(:after_restore, self)
broadcast("restore_#{broadcast_model_name_key}_successful", self)
end
def archived?
archived_at?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment