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
14ddc9fd
Commit
14ddc9fd
authored
Oct 22, 2012
by
Josh Goebel
Committed by
Ryan Bigg
Nov 15, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
paranoia should work well with associations, unscoped is evil
Fixes #32
parent
ef8b1f4b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
8 deletions
+30
-8
README.md
+3
-3
lib/paranoia.rb
+4
-3
lib/paranoia/version.rb
+1
-1
test/paranoia_test.rb
+22
-1
No files found.
README.md
View file @
14ddc9fd
...
@@ -91,9 +91,9 @@ end
...
@@ -91,9 +91,9 @@ end
You can replace the older acts_as_paranoid methods as follows:
You can replace the older acts_as_paranoid methods as follows:
```
ruby
```
ruby
find_with_deleted
(
:all
)
# =>
unscop
ed
find_with_deleted
(
:all
)
# =>
with_delet
ed
find_with_deleted
(
:first
)
# =>
unscop
ed.first
find_with_deleted
(
:first
)
# =>
with_delet
ed.first
find_with_deleted
(
id
)
# =>
unscop
ed.find(id)
find_with_deleted
(
id
)
# =>
with_delet
ed.find(id)
find_only_deleted
(
:all
)
# => only_deleted
find_only_deleted
(
:all
)
# => only_deleted
find_only_deleted
(
:first
)
# => only_deleted.first
find_only_deleted
(
:first
)
# => only_deleted.first
...
...
lib/paranoia.rb
View file @
14ddc9fd
...
@@ -7,9 +7,10 @@ module Paranoia
...
@@ -7,9 +7,10 @@ module Paranoia
def
paranoid?
;
true
;
end
def
paranoid?
;
true
;
end
def
only_deleted
def
only_deleted
unscoped
{
scoped
.
tap
{
|
x
|
x
.
default_scoped
=
false
}.
where
(
"deleted_at is not null"
)
where
(
"deleted_at is not null"
)
end
}
def
with_deleted
scoped
.
tap
{
|
x
|
x
.
default_scoped
=
false
}
end
end
end
end
...
...
lib/paranoia/version.rb
View file @
14ddc9fd
module
Paranoia
module
Paranoia
VERSION
=
"1.
1
.0"
VERSION
=
"1.
2
.0"
end
end
test/paranoia_test.rb
View file @
14ddc9fd
...
@@ -8,7 +8,8 @@ FileUtils.mkdir_p File.dirname(DB_FILE)
...
@@ -8,7 +8,8 @@ FileUtils.mkdir_p File.dirname(DB_FILE)
FileUtils
.
rm_f
DB_FILE
FileUtils
.
rm_f
DB_FILE
ActiveRecord
::
Base
.
establish_connection
:adapter
=>
'sqlite3'
,
:database
=>
DB_FILE
ActiveRecord
::
Base
.
establish_connection
:adapter
=>
'sqlite3'
,
:database
=>
DB_FILE
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, 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 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 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)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
...
@@ -70,6 +71,21 @@ class ParanoiaTest < Test::Unit::TestCase
...
@@ -70,6 +71,21 @@ class ParanoiaTest < Test::Unit::TestCase
end
end
def
test_scoping_behavior_for_paranoid_models
ParanoidModel
.
unscoped
.
delete_all
parent1
=
ParentModel
.
create
parent2
=
ParentModel
.
create
p1
=
ParanoidModel
.
create
(
:parent_model
=>
parent1
)
p2
=
ParanoidModel
.
create
(
:parent_model
=>
parent2
)
p1
.
destroy
p2
.
destroy
assert_equal
0
,
parent1
.
paranoid_models
.
count
assert_equal
1
,
parent1
.
paranoid_models
.
only_deleted
.
count
p3
=
ParanoidModel
.
create
(
:parent_model
=>
parent1
)
assert_equal
2
,
parent1
.
paranoid_models
.
with_deleted
.
count
assert_equal
[
p1
,
p3
],
parent1
.
paranoid_models
.
with_deleted
end
def
test_destroy_behavior_for_featureful_paranoid_models
def
test_destroy_behavior_for_featureful_paranoid_models
model
=
get_featureful_model
model
=
get_featureful_model
assert_equal
0
,
model
.
class
.
count
assert_equal
0
,
model
.
class
.
count
...
@@ -146,7 +162,12 @@ end
...
@@ -146,7 +162,12 @@ end
# Helper classes
# Helper classes
class
ParentModel
<
ActiveRecord
::
Base
has_many
:paranoid_models
end
class
ParanoidModel
<
ActiveRecord
::
Base
class
ParanoidModel
<
ActiveRecord
::
Base
belongs_to
:parent_model
acts_as_paranoid
acts_as_paranoid
end
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