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
e04bba48
Commit
e04bba48
authored
Nov 06, 2013
by
alfa-jpn
Committed by
Ryan Bigg
Nov 13, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for plain models callback
Fixes #81 Fixes #84 Conflicts: lib/paranoia.rb
parent
8572265e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
2 deletions
+52
-2
lib/paranoia.rb
+20
-2
test/paranoia_test.rb
+32
-0
No files found.
lib/paranoia.rb
View file @
e04bba48
...
@@ -44,12 +44,12 @@ module Paranoia
...
@@ -44,12 +44,12 @@ module Paranoia
end
end
def
destroy
def
destroy
run_callbacks
(
:destroy
)
{
delete
}
run_callbacks
(
:destroy
)
{
delete
_or_soft_delete
(
true
)
}
end
end
def
delete
def
delete
return
if
new_record?
return
if
new_record?
de
stroyed?
?
destroy!
:
update_column
(
paranoia_column
,
Time
.
now
)
de
lete_or_soft_delete
end
end
def
restore!
def
restore!
...
@@ -60,6 +60,24 @@ module Paranoia
...
@@ -60,6 +60,24 @@ module Paranoia
!!
send
(
paranoia_column
)
!!
send
(
paranoia_column
)
end
end
alias
:deleted?
:destroyed?
alias
:deleted?
:destroyed?
private
# select and exec delete or soft-delete.
# @param with_transaction [Boolean] exec with ActiveRecord Transactions, when soft-delete.
def
delete_or_soft_delete
(
with_transaction
=
false
)
destroyed?
?
destroy!
:
touch_paranoia_column
(
with_transaction
)
end
# touch paranoia column.
# insert time to paranoia column.
# @param with_transaction [Boolean] exec with ActiveRecord Transactions.
def
touch_paranoia_column
(
with_transaction
=
false
)
if
with_transaction
with_transaction_returning_status
{
touch
(
paranoia_column
)
}
else
touch
(
paranoia_column
)
end
end
end
end
class
ActiveRecord
::
Base
class
ActiveRecord
::
Base
...
...
test/paranoia_test.rb
View file @
e04bba48
...
@@ -63,9 +63,32 @@ class ParanoiaTest < Test::Unit::TestCase
...
@@ -63,9 +63,32 @@ class ParanoiaTest < Test::Unit::TestCase
# Anti-regression test for #81, which would've introduced a bug to break this test.
# Anti-regression test for #81, which would've introduced a bug to break this test.
def
test_destroy_behavior_for_plain_models_callbacks
def
test_destroy_behavior_for_plain_models_callbacks
model
=
CallbackModel
.
new
model
=
CallbackModel
.
new
model
.
save
model
.
remove_called_variables
# clear called callback flags
model
.
destroy
model
.
destroy
assert_equal
nil
,
model
.
instance_variable_get
(
:@update_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@save_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@validate_called
)
assert
model
.
instance_variable_get
(
:@destroy_callback_called
)
assert
model
.
instance_variable_get
(
:@after_destroy_callback_called
)
assert
model
.
instance_variable_get
(
:@after_commit_callback_called
)
end
def
test_delete_behavior_for_plain_models_callbacks
model
=
CallbackModel
.
new
model
.
save
model
.
remove_called_variables
# clear called callback flags
model
.
delete
assert_equal
nil
,
model
.
instance_variable_get
(
:@update_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@update_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@save_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@save_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@validate_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@destroy_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@after_destroy_callback_called
)
assert_equal
nil
,
model
.
instance_variable_get
(
:@after_commit_callback_called
)
end
end
def
test_destroy_behavior_for_paranoid_models
def
test_destroy_behavior_for_paranoid_models
...
@@ -313,6 +336,15 @@ class CallbackModel < ActiveRecord::Base
...
@@ -313,6 +336,15 @@ class CallbackModel < ActiveRecord::Base
before_restore
{
|
model
|
model
.
instance_variable_set
:@restore_callback_called
,
true
}
before_restore
{
|
model
|
model
.
instance_variable_set
:@restore_callback_called
,
true
}
before_update
{
|
model
|
model
.
instance_variable_set
:@update_callback_called
,
true
}
before_update
{
|
model
|
model
.
instance_variable_set
:@update_callback_called
,
true
}
before_save
{
|
model
|
model
.
instance_variable_set
:@save_callback_called
,
true
}
before_save
{
|
model
|
model
.
instance_variable_set
:@save_callback_called
,
true
}
after_destroy
{
|
model
|
model
.
instance_variable_set
:@after_destroy_callback_called
,
true
}
after_commit
{
|
model
|
model
.
instance_variable_set
:@after_commit_callback_called
,
true
}
validate
{
|
model
|
model
.
instance_variable_set
:@validate_called
,
true
}
def
remove_called_variables
instance_variables
.
each
{
|
name
|
(
name
.
to_s
.
end_with?
(
'_called'
))
?
remove_instance_variable
(
name
)
:
nil
}
end
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