discourse/app/models/concerns/trashable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

38 lines
970 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module Trashable
extend ActiveSupport::Concern
included do
default_scope { where(deleted_at: nil) }
scope :with_deleted, -> { unscope(where: :deleted_at) }
scope :only_deleted, -> { with_deleted.where.not(deleted_at: nil) }
2013-05-07 00:46:46 -04:00
2013-07-09 15:20:18 -04:00
belongs_to :deleted_by, class_name: "User"
end
def trashed?
deleted_at.present?
end
2013-07-09 15:20:18 -04:00
def trash!(trashed_by = nil)
2013-05-07 00:50:02 -04:00
# note, an argument could be made that the column should probably called trashed_at
# however, deleted_at is the terminology used in the UI
#
# we could hijack use a delete! and delete - redirecting the originals elsewhere, but that is
# confusing as well. So for now, we go with trash!
#
2013-07-09 15:20:18 -04:00
trash_update(DateTime.now, trashed_by.try(:id))
end
def recover!
2013-07-09 15:20:18 -04:00
trash_update(nil, nil)
end
2013-07-09 15:20:18 -04:00
private
def trash_update(deleted_at, deleted_by_id)
self.update_columns(deleted_at: deleted_at, deleted_by_id: deleted_by_id)
2013-07-09 15:20:18 -04:00
end
end