Created
December 5, 2018 13:04
-
-
Save ryenski/933745206091f325dc245e124e40dc30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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