mirror of
https://github.com/discourse/discourse.git
synced 2024-12-13 20:03:51 +08:00
d25fd34b44
* DEV: Remove with_deleted workarounds for old Rails version These workarounds using private APIs are no longer required in the latest version of Rails. The referenced issue (https://github.com/rails/rails/issues/4306) was closed in 2013. The acts_as_paranoid workaround which this was based on was removed for rails > 5. Switching to using a scope also allows us to use it within a `belongs_to` relation (e.g. in the Poll model). This avoids issues which can be caused by unscoping all `where` clauses. Predicates are not necessarily strings, so calling `.join(" AND ")` can sometimes cause weird errors. If we use `WhereClause#ast`, and then `.to_sql` we achieve the same thing with fully public APIs, and it will work successfully for all predicates.
38 lines
899 B
Ruby
38 lines
899 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Trashable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
default_scope { where(deleted_at: nil) }
|
|
scope :with_deleted, -> { unscope(where: :deleted_at) }
|
|
|
|
belongs_to :deleted_by, class_name: 'User'
|
|
end
|
|
|
|
def trashed?
|
|
deleted_at.present?
|
|
end
|
|
|
|
def trash!(trashed_by = nil)
|
|
# 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!
|
|
#
|
|
trash_update(DateTime.now, trashed_by.try(:id))
|
|
end
|
|
|
|
def recover!
|
|
trash_update(nil, nil)
|
|
end
|
|
|
|
private
|
|
|
|
def trash_update(deleted_at, deleted_by_id)
|
|
self.update_columns(deleted_at: deleted_at, deleted_by_id: deleted_by_id)
|
|
end
|
|
|
|
end
|