DEV: Added .only_deleted scope in the Trashable module (#20196)
This commit is contained in:
parent
6e522e4aad
commit
5d32db76dd
|
@ -6,6 +6,7 @@ module Trashable
|
|||
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) }
|
||||
|
||||
belongs_to :deleted_by, class_name: "User"
|
||||
end
|
||||
|
|
|
@ -9,4 +9,20 @@ RSpec.describe Trashable do
|
|||
expect { p1.trash! }.to change { Post.count }.by(-1)
|
||||
expect(Post.with_deleted.count).to eq(Post.count + 1)
|
||||
end
|
||||
|
||||
it "can list only deleted items" do
|
||||
p1 = Fabricate(:post)
|
||||
p2 = Fabricate(:post)
|
||||
|
||||
p1.trash!
|
||||
expect(Post.only_deleted.count).to eq(1)
|
||||
expect(Post.only_deleted.first).to eq(p1)
|
||||
end
|
||||
|
||||
it "can recover" do
|
||||
p1 = Fabricate(:post)
|
||||
p1.trash!
|
||||
expect { p1.recover! }.to change { Post.count }.by(1)
|
||||
expect(Post.with_deleted.count).to eq(Post.count)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue