Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/mongoid/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def configure

default_scope -> { where(deleted_at: nil) }
scope :deleted, -> { ne(deleted_at: nil) }
scope :with_deleted, lambda {
msg = 'This scope requires Mongoid >= 9 and allow_scopes_to_unset_default_scope to be set to true'
raise msg unless Mongoid.try(:allow_scopes_to_unset_default_scope)

criteria.remove_scoping(unscoped.where(deleted_at: nil))
}
define_model_callbacks :restore
define_model_callbacks :remove
end
Expand Down Expand Up @@ -77,7 +83,7 @@ def persisted?
# @since 1.0.0
alias orig_delete delete

def remove(_ = {})
def remove(_ = {}) # rubocop:disable Naming/PredicateMethod
time = self.deleted_at = Time.now
_paranoia_update('$set' => { paranoid_field => time })
@destroyed = true
Expand Down
3 changes: 3 additions & 0 deletions perf/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class Model
include Mongoid::Document

field :text, type: String

index({ text: 'text' })
Expand All @@ -19,13 +20,15 @@ class Model
class ParanoidModel
include Mongoid::Document
include Mongoid::Paranoia

field :text, type: String

index({ text: 'text' })
end

class MetaParanoidModel
include Mongoid::Document

field :text, type: String
field :deleted_at, type: Time
default_scope -> { where(deleted_at: nil) }
Expand Down
30 changes: 30 additions & 0 deletions spec/mongoid/paranoia_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ class ParanoidConfiguredReset
end
end

describe ".with_deleted" do

let(:posts) do
2.times.map { |i| ParanoidPost.create(title: "testing #{i}") }
end

around do |example|
skip unless Mongoid.respond_to?(:allow_scopes_to_unset_default_scope=)

original_value = Mongoid.allow_scopes_to_unset_default_scope
Mongoid.configure { |config| config.allow_scopes_to_unset_default_scope = true }

example.run

Mongoid.configure { |config| config.allow_scopes_to_unset_default_scope = original_value }
end

before do
posts.first.destroy
end

let(:with_deleted) do
ParanoidPost.with_deleted
end

it "returns the deleted documents" do
expect(with_deleted).to eq(posts)
end
end

describe "#destroy!" do

context "when the document is a root" do
Expand Down
26 changes: 26 additions & 0 deletions spec/mongoid/scoping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,31 @@
expect(criteria.selector).to eq({ "fresh" => true })
end
end

context "when chaining with_deleted" do

let(:criteria) do
Fish.fresh.with_deleted
end

if Mongoid.respond_to?(:allow_scopes_to_unset_default_scope=)
around do |example|
original_value = Mongoid.allow_scopes_to_unset_default_scope
Mongoid.configure { |config| config.allow_scopes_to_unset_default_scope = true }

example.run

Mongoid.configure { |config| config.allow_scopes_to_unset_default_scope = original_value }
end

it "removes only deleted_at from the selector" do
expect(criteria.selector).to eq({ "fresh" => true })
end
else
it "raises an error if the feature is not supported" do
expect { criteria.selector }.to raise_error(/requires Mongoid >= 9/)
end
end
end
end
end
Loading