From 5d32db76dd4a81b690e982dffd91777e91038046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Tue, 7 Feb 2023 15:28:59 -0300 Subject: [PATCH] DEV: Added .only_deleted scope in the Trashable module (#20196) --- app/models/concerns/trashable.rb | 1 + spec/lib/trashable_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/models/concerns/trashable.rb b/app/models/concerns/trashable.rb index cdd52c08425..bc75463de7c 100644 --- a/app/models/concerns/trashable.rb +++ b/app/models/concerns/trashable.rb @@ -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 diff --git a/spec/lib/trashable_spec.rb b/spec/lib/trashable_spec.rb index ca5d4e8ac5d..d0657ad15a3 100644 --- a/spec/lib/trashable_spec.rb +++ b/spec/lib/trashable_spec.rb @@ -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