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
e76e94e4
Commit
e76e94e4
authored
Jun 13, 2011
by
Eli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add proper delete and restore
parent
75181e0e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
12 deletions
+56
-12
Rakefile
+2
-1
lib/paranoia.rb
+9
-3
test/paranoia_test.rb
+45
-8
No files found.
Rakefile
View file @
e76e94e4
...
...
@@ -3,6 +3,6 @@ Bundler::GemHelper.install_tasks
task
:test
do
Dir
[
'test/*_test.rb'
].
each
do
|
testfile
|
load
testfile
load
testfile
end
end
\ No newline at end of file
lib/paranoia.rb
View file @
e76e94e4
...
...
@@ -14,14 +14,20 @@ module Paranoia
end
def
destroy
_run_destroy_callbacks
_run_destroy_callbacks
{
delete
}
end
def
delete
self
.
update_attribute
(
:deleted_at
,
Time
.
now
)
if
!
deleted?
&&
persisted?
freeze
end
alias
:delete
:destroy
def
restore!
update_attribute
:deleted_at
,
nil
end
def
destroyed?
!
self
[
:deleted_at
]
.
nil?
!
self
.
deleted_at
.
nil?
end
alias
:deleted?
:destroyed?
end
...
...
test/paranoia_test.rb
View file @
e76e94e4
...
...
@@ -11,6 +11,7 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_F
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
class
ParanoiaTest
<
Test
::
Unit
::
TestCase
def
test_plain_model_class_is_not_paranoid
...
...
@@ -29,12 +30,12 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal
true
,
ParanoidModel
.
new
.
paranoid?
end
def
test_de
lete
_behavior_for_plain_models
def
test_de
stroy
_behavior_for_plain_models
model
=
PlainModel
.
new
assert_equal
0
,
model
.
class
.
count
model
.
save!
assert_equal
1
,
model
.
class
.
count
model
.
de
lete
model
.
de
stroy
assert_equal
true
,
model
.
deleted_at
.
nil?
assert
model
.
frozen?
...
...
@@ -43,12 +44,12 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal
0
,
model
.
class
.
unscoped
.
count
end
def
test_de
lete
_behavior_for_paranoid_models
def
test_de
stroy
_behavior_for_paranoid_models
model
=
ParanoidModel
.
new
assert_equal
0
,
model
.
class
.
count
model
.
save!
assert_equal
1
,
model
.
class
.
count
model
.
de
lete
model
.
de
stroy
assert_equal
false
,
model
.
deleted_at
.
nil?
assert
model
.
frozen?
...
...
@@ -58,12 +59,12 @@ class ParanoiaTest < Test::Unit::TestCase
end
def
test_de
lete
_behavior_for_featureful_paranoid_models
def
test_de
stroy
_behavior_for_featureful_paranoid_models
model
=
get_featureful_model
assert_equal
0
,
model
.
class
.
count
model
.
save!
assert_equal
1
,
model
.
class
.
count
model
.
de
lete
model
.
de
stroy
assert_equal
false
,
model
.
deleted_at
.
nil?
...
...
@@ -71,16 +72,47 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal
1
,
model
.
class
.
unscoped
.
count
end
def
test_only_de
let
ed_scope_for_paranoid_models
def
test_only_de
stroy
ed_scope_for_paranoid_models
model
=
ParanoidModel
.
new
model
.
save
model
.
de
lete
model
.
de
stroy
model2
=
ParanoidModel
.
new
model2
.
save
assert_equal
model
,
ParanoidModel
.
only_deleted
.
last
assert_equal
false
,
ParanoidModel
.
only_deleted
.
include?
(
model2
)
end
def
test_delete_behavior_for_callbacks
model
=
CallbackModel
.
new
model
.
save
model
.
delete
assert_equal
nil
,
model
.
instance_variable_get
(
:@callback_called
)
end
def
test_destroy_behavior_for_callbacks
model
=
CallbackModel
.
new
model
.
save
model
.
destroy
assert
model
.
instance_variable_get
(
:@callback_called
)
end
def
test_restore
model
=
ParanoidModel
.
new
model
.
save
id
=
model
.
id
model
.
destroy
puts
model
.
inspect
puts
ParanoidModel
.
all
.
inspect
assert
model
.
destroyed?
model
=
ParanoidModel
.
only_deleted
.
find
(
id
)
model
.
restore!
assert_equal
false
,
model
.
destroyed?
end
private
def
get_featureful_model
...
...
@@ -101,3 +133,8 @@ end
class
PlainModel
<
ActiveRecord
::
Base
end
class
CallbackModel
<
ActiveRecord
::
Base
acts_as_paranoid
before_destroy
{
|
model
|
model
.
instance_variable_set
:@callback_called
,
true
}
end
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