mirror of
https://github.com/discourse/discourse.git
synced 2026-08-26 21:27:16 -05:00
Add deleted_by to Trashable tables
This commit is contained in:
+18
-8
@@ -5,6 +5,8 @@ module Trashable
|
||||
default_scope where(with_deleted_scope_sql)
|
||||
|
||||
# scope unscoped does not work
|
||||
|
||||
belongs_to :deleted_by, class_name: 'User'
|
||||
end
|
||||
|
||||
|
||||
@@ -24,23 +26,31 @@ module Trashable
|
||||
end
|
||||
end
|
||||
|
||||
def trash!
|
||||
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!
|
||||
#
|
||||
update_column(:deleted_at, DateTime.now)
|
||||
trash_update(DateTime.now, trashed_by.try(:id))
|
||||
end
|
||||
|
||||
def recover!
|
||||
# see: https://github.com/rails/rails/issues/8436
|
||||
#
|
||||
# Fixed in Rails 4
|
||||
#
|
||||
self.class.unscoped.where(id: self.id).update_all({deleted_at: nil})
|
||||
raw_write_attribute :deleted_at, nil
|
||||
trash_update(nil, nil)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def trash_update(deleted_at, deleted_by_id)
|
||||
# see: https://github.com/rails/rails/issues/8436
|
||||
#
|
||||
# Fixed in Rails 4
|
||||
#
|
||||
self.class.unscoped.where(id: self.id).update_all(deleted_at: deleted_at, deleted_by_id: deleted_by_id)
|
||||
raw_write_attribute :deleted_at, deleted_at
|
||||
raw_write_attribute :deleted_by_id, deleted_by_id
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user