Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paranoia
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paranoia
Commits
ebec99ba
Commit
ebec99ba
authored
Oct 17, 2013
by
Michael Dilley
Committed by
Ryan Bigg
Oct 21, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add callbacks for 'new' AR lifecycle :restore
Fixes #72
parent
dd8b8e12
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
1 deletions
+36
-1
lib/paranoia.rb
+20
-1
test/paranoia_test.rb
+16
-0
No files found.
lib/paranoia.rb
View file @
ebec99ba
module
Paranoia
module
Paranoia
def
self
.
included
(
klazz
)
def
self
.
included
(
klazz
)
klazz
.
extend
Query
klazz
.
extend
Query
klazz
.
extend
Callbacks
end
end
module
Query
module
Query
...
@@ -23,6 +24,24 @@ module Paranoia
...
@@ -23,6 +24,24 @@ module Paranoia
end
end
end
end
module
Callbacks
def
self
.
extended
(
klazz
)
klazz
.
define_callbacks
:restore
klazz
.
define_singleton_method
(
"before_restore"
)
do
|*
args
,
&
block
|
set_callback
(
:restore
,
:before
,
*
args
,
&
block
)
end
klazz
.
define_singleton_method
(
"around_restore"
)
do
|*
args
,
&
block
|
set_callback
(
:restore
,
:around
,
*
args
,
&
block
)
end
klazz
.
define_singleton_method
(
"after_restore"
)
do
|*
args
,
&
block
|
set_callback
(
:restore
,
:after
,
*
args
,
&
block
)
end
end
end
def
destroy
def
destroy
run_callbacks
(
:destroy
)
{
delete
}
run_callbacks
(
:destroy
)
{
delete
}
end
end
...
@@ -33,7 +52,7 @@ module Paranoia
...
@@ -33,7 +52,7 @@ module Paranoia
end
end
def
restore!
def
restore!
update_column
:deleted_at
,
nil
run_callbacks
(
:restore
)
{
update_column
:deleted_at
,
nil
}
end
end
def
destroyed?
def
destroyed?
...
...
test/paranoia_test.rb
View file @
ebec99ba
...
@@ -197,6 +197,21 @@ class ParanoiaTest < Test::Unit::TestCase
...
@@ -197,6 +197,21 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal
0
,
ParanoidModel
.
unscoped
.
where
(
id:
model
.
id
).
count
assert_equal
0
,
ParanoidModel
.
unscoped
.
where
(
id:
model
.
id
).
count
end
end
def
test_restore_behavior_for_callbacks
model
=
CallbackModel
.
new
model
.
save
id
=
model
.
id
model
.
destroy
assert
model
.
destroyed?
model
=
CallbackModel
.
only_deleted
.
find
(
id
)
model
.
restore!
model
.
reload
assert
model
.
instance_variable_get
(
:@restore_callback_called
)
end
def
test_real_destroy
def
test_real_destroy
model
=
ParanoidModel
.
new
model
=
ParanoidModel
.
new
model
.
save
model
.
save
...
@@ -268,6 +283,7 @@ end
...
@@ -268,6 +283,7 @@ end
class
CallbackModel
<
ActiveRecord
::
Base
class
CallbackModel
<
ActiveRecord
::
Base
acts_as_paranoid
acts_as_paranoid
before_destroy
{
|
model
|
model
.
instance_variable_set
:@callback_called
,
true
}
before_destroy
{
|
model
|
model
.
instance_variable_set
:@callback_called
,
true
}
before_restore
{
|
model
|
model
.
instance_variable_set
:@restore_callback_called
,
true
}
end
end
class
ParentModel
<
ActiveRecord
::
Base
class
ParentModel
<
ActiveRecord
::
Base
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment