DEV: Added .only_deleted scope in the Trashable module (#20196)

This commit is contained in:
Sérgio Saquetim 2023-02-07 15:28:59 -03:00 committed by GitHub
parent 6e522e4aad
commit 5d32db76dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -6,6 +6,7 @@ module Trashable
included do included do
default_scope { where(deleted_at: nil) } default_scope { where(deleted_at: nil) }
scope :with_deleted, -> { unscope(where: :deleted_at) } scope :with_deleted, -> { unscope(where: :deleted_at) }
scope :only_deleted, -> { with_deleted.where.not(deleted_at: nil) }
belongs_to :deleted_by, class_name: "User" belongs_to :deleted_by, class_name: "User"
end end

View File

@ -9,4 +9,20 @@ RSpec.describe Trashable do
expect { p1.trash! }.to change { Post.count }.by(-1) expect { p1.trash! }.to change { Post.count }.by(-1)
expect(Post.with_deleted.count).to eq(Post.count + 1) expect(Post.with_deleted.count).to eq(Post.count + 1)
end 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 end