forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrashable.rb
More file actions
46 lines (38 loc) · 1.17 KB
/
Copy pathtrashable.rb
File metadata and controls
46 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Trashable
extend ActiveSupport::Concern
included do
default_scope where(with_deleted_scope_sql)
# scope unscoped does not work
end
module ClassMethods
def with_deleted
# lifted from acts_as_paranoid, works around https://github.com/rails/rails/issues/4306
#
# with this in place Post.limit(10).with_deleted, will work as expected
#
scope = self.scoped.with_default_scope
scope.where_values.delete(with_deleted_scope_sql)
scope
end
def with_deleted_scope_sql
scoped.table[:deleted_at].eq(nil).to_sql
end
end
def trash!
# 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)
end
def recover!
# see: https://github.com/rails/rails/issues/8436
#
# Fixed in Rails 4
#
self.class.unscoped.update_all({deleted_at: nil}, id: self.id)
raw_write_attribute :deleted_at, nil
end
end