Skip to content
Closed
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
13 changes: 8 additions & 5 deletions lib/mongoid/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Paranoia

default_scope where(deleted_at: nil)
scope :deleted, ne(deleted_at: nil)
define_model_callbacks :restore
end

# Delete the paranoid +Document+ from the database completely. This will
Expand Down Expand Up @@ -96,11 +97,13 @@ def destroyed?
#
# @since 1.0.0
def restore
paranoid_collection.find(atomic_selector).
update({ "$unset" => { paranoid_field => true }})
attributes.delete("deleted_at")
@destroyed = false
true
run_callbacks(:restore) do
paranoid_collection.find(atomic_selector).
update({ "$unset" => { paranoid_field => true }})
attributes.delete("deleted_at")
@destroyed = false
true
end
end

# Returns a string representing the documents's key suitable for use in URLs.
Expand Down
13 changes: 12 additions & 1 deletion spec/app/models/paranoid_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class ParanoidPost

field :title, type: String

attr_accessor :after_destroy_called, :before_destroy_called
attr_accessor :after_destroy_called, :before_destroy_called,
:after_restore_called, :before_restore_called

belongs_to :person

Expand All @@ -16,6 +17,8 @@ class ParanoidPost

before_destroy :before_destroy_stub
after_destroy :after_destroy_stub
before_restore :before_restore_stub
after_restore :after_restore_stub

def before_destroy_stub
self.before_destroy_called = true
Expand All @@ -25,6 +28,14 @@ def after_destroy_stub
self.after_destroy_called = true
end

def before_restore_stub
self.before_restore_called = true
end

def after_restore_stub
self.after_restore_called = true
end

class << self
def old
where(created_at: { "$lt" => 30.days.ago })
Expand Down
2 changes: 1 addition & 1 deletion spec/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Person
field :owner_id, type: Integer
field :security_code
field :reading, type: Object
field :bson_id, type: Moped::BSON::ObjectId
field :bson_id, type: BSON::ObjectId
field :pattern, type: Regexp
field :override_me, type: Integer
field :t, as: :test, type: String
Expand Down
8 changes: 8 additions & 0 deletions spec/mongoid/paranoia_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,14 @@
it "marks document again as persisted" do
post.persisted?.should be_true
end

it "executes the before restore callbacks" do
post.before_restore_called.should be_true
end

it "executes the after restore callbacks" do
post.after_restore_called.should be_true
end
end

context "when the document is embedded" do
Expand Down