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
10194e05
Commit
10194e05
authored
Sep 16, 2014
by
Ryan Bigg
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #123 from JeskTop/setting_restore
hasOne association could not restore
parents
ceaaecb1
c98d4083
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
0 deletions
+87
-0
lib/paranoia.rb
+6
-0
test/paranoia_test.rb
+81
-0
No files found.
lib/paranoia.rb
View file @
10194e05
...
@@ -140,6 +140,12 @@ module Paranoia
...
@@ -140,6 +140,12 @@ module Paranoia
end
end
end
end
end
end
if
association_data
.
nil?
&&
association
.
macro
.
to_s
==
"has_one"
association_class_name
=
association
.
options
[
:class_name
].
present?
?
association
.
options
[:
class_name
]
:
association
.
name
.
to_s
.
camelize
association_foreign_key
=
association
.
options
[
:foreign_key
].
present?
?
association
.
options
[:
foreign_key
]
:
"
#{
self
.
class
.
name
.
to_s
.
underscore
}
_id"
Object
.
const_get
(
association_class_name
).
only_deleted
.
where
(
association_foreign_key
,
self
.
id
).
first
.
try
(
:restore
,
recursive:
true
)
end
end
end
clear_association_cache
if
destroyed_associations
.
present?
clear_association_cache
if
destroyed_associations
.
present?
...
...
test/paranoia_test.rb
View file @
10194e05
...
@@ -14,6 +14,8 @@ def connect!
...
@@ -14,6 +14,8 @@ def connect!
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE parent_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 paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_model_with_anthor_class_name_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord
::
Base
.
connection
.
execute
'CREATE TABLE paranoid_model_with_foreign_key_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER)'
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)'
...
@@ -446,7 +448,11 @@ class ParanoiaTest < test_framework
...
@@ -446,7 +448,11 @@ class ParanoiaTest < test_framework
# setup and destroy test objects
# setup and destroy test objects
hasOne
=
ParanoidModelWithHasOne
.
create
hasOne
=
ParanoidModelWithHasOne
.
create
belongsTo
=
ParanoidModelWithBelong
.
create
belongsTo
=
ParanoidModelWithBelong
.
create
anthorClassName
=
ParanoidModelWithAnthorClassNameBelong
.
create
foreignKey
=
ParanoidModelWithForeignKeyBelong
.
create
hasOne
.
paranoid_model_with_belong
=
belongsTo
hasOne
.
paranoid_model_with_belong
=
belongsTo
hasOne
.
class_name_belong
=
anthorClassName
hasOne
.
paranoid_model_with_foreign_key_belong
=
foreignKey
hasOne
.
save!
hasOne
.
save!
hasOne
.
destroy
hasOne
.
destroy
...
@@ -460,6 +466,61 @@ class ParanoiaTest < test_framework
...
@@ -460,6 +466,61 @@ class ParanoiaTest < test_framework
assert_equal
true
,
hasOne
.
reload
.
deleted_at
.
nil?
assert_equal
true
,
hasOne
.
reload
.
deleted_at
.
nil?
assert_equal
true
,
belongsTo
.
reload
.
deleted_at
.
nil?
,
"
#{
belongsTo
.
deleted_at
}
"
assert_equal
true
,
belongsTo
.
reload
.
deleted_at
.
nil?
,
"
#{
belongsTo
.
deleted_at
}
"
assert
ParanoidModelWithBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a record"
assert
ParanoidModelWithBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a record"
assert
ParanoidModelWithAnthorClassNameBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be an other record"
assert
ParanoidModelWithForeignKeyBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a foreign_key record"
end
def
test_new_restore_with_has_one_association
# setup and destroy test objects
hasOne
=
ParanoidModelWithHasOne
.
create
belongsTo
=
ParanoidModelWithBelong
.
create
anthorClassName
=
ParanoidModelWithAnthorClassNameBelong
.
create
foreignKey
=
ParanoidModelWithForeignKeyBelong
.
create
hasOne
.
paranoid_model_with_belong
=
belongsTo
hasOne
.
class_name_belong
=
anthorClassName
hasOne
.
paranoid_model_with_foreign_key_belong
=
foreignKey
hasOne
.
save!
hasOne
.
destroy
assert_equal
false
,
hasOne
.
deleted_at
.
nil?
assert_equal
false
,
belongsTo
.
deleted_at
.
nil?
# Does it restore has_one associations?
newHasOne
=
ParanoidModelWithHasOne
.
with_deleted
.
find
(
hasOne
.
id
)
newHasOne
.
restore
(
:recursive
=>
true
)
newHasOne
.
save!
assert_equal
true
,
hasOne
.
reload
.
deleted_at
.
nil?
assert_equal
true
,
belongsTo
.
reload
.
deleted_at
.
nil?
,
"
#{
belongsTo
.
deleted_at
}
"
assert
ParanoidModelWithBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a record"
assert
ParanoidModelWithAnthorClassNameBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be an other record"
assert
ParanoidModelWithForeignKeyBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a foreign_key record"
end
def
test_model_restore_with_has_one_association
# setup and destroy test objects
hasOne
=
ParanoidModelWithHasOne
.
create
belongsTo
=
ParanoidModelWithBelong
.
create
anthorClassName
=
ParanoidModelWithAnthorClassNameBelong
.
create
foreignKey
=
ParanoidModelWithForeignKeyBelong
.
create
hasOne
.
paranoid_model_with_belong
=
belongsTo
hasOne
.
class_name_belong
=
anthorClassName
hasOne
.
paranoid_model_with_foreign_key_belong
=
foreignKey
hasOne
.
save!
hasOne
.
destroy
assert_equal
false
,
hasOne
.
deleted_at
.
nil?
assert_equal
false
,
belongsTo
.
deleted_at
.
nil?
# Does it restore has_one associations?
ParanoidModelWithHasOne
.
restore
(
hasOne
.
id
,
:recursive
=>
true
)
hasOne
.
save!
assert_equal
true
,
hasOne
.
reload
.
deleted_at
.
nil?
assert_equal
true
,
belongsTo
.
reload
.
deleted_at
.
nil?
,
"
#{
belongsTo
.
deleted_at
}
"
assert
ParanoidModelWithBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a record"
assert
ParanoidModelWithAnthorClassNameBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be an other record"
assert
ParanoidModelWithForeignKeyBelong
.
with_deleted
.
reload
.
count
!=
0
,
"There should be a foreign_key record"
end
end
def
test_restore_with_nil_has_one_association
def
test_restore_with_nil_has_one_association
...
@@ -664,6 +725,8 @@ end
...
@@ -664,6 +725,8 @@ end
# refer back to regression test for #118
# refer back to regression test for #118
class
ParanoidModelWithHasOne
<
ParanoidModel
class
ParanoidModelWithHasOne
<
ParanoidModel
has_one
:paranoid_model_with_belong
,
:dependent
=>
:destroy
has_one
:paranoid_model_with_belong
,
:dependent
=>
:destroy
has_one
:class_name_belong
,
:dependent
=>
:destroy
,
:class_name
=>
"ParanoidModelWithAnthorClassNameBelong"
has_one
:paranoid_model_with_foreign_key_belong
,
:dependent
=>
:destroy
,
:foreign_key
=>
"has_one_foreign_key_id"
end
end
class
ParanoidModelWithBelong
<
ActiveRecord
::
Base
class
ParanoidModelWithBelong
<
ActiveRecord
::
Base
...
@@ -671,6 +734,24 @@ class ParanoidModelWithBelong < ActiveRecord::Base
...
@@ -671,6 +734,24 @@ class ParanoidModelWithBelong < ActiveRecord::Base
belongs_to
:paranoid_model_with_has_one
belongs_to
:paranoid_model_with_has_one
end
end
class
ParanoidModelWithAnthorClassNameBelong
<
ActiveRecord
::
Base
acts_as_paranoid
belongs_to
:paranoid_model_with_has_one
end
class
ParanoidModelWithForeignKeyBelong
<
ActiveRecord
::
Base
acts_as_paranoid
belongs_to
:paranoid_model_with_has_one
end
class
FlaggedModel
<
PlainModel
acts_as_paranoid
:flag_column
=>
:is_deleted
end
class
FlaggedModelWithCustomIndex
<
PlainModel
acts_as_paranoid
:flag_column
=>
:is_deleted
,
:indexed_column
=>
:is_deleted
end
class
AsplodeModel
<
ActiveRecord
::
Base
class
AsplodeModel
<
ActiveRecord
::
Base
acts_as_paranoid
acts_as_paranoid
before_destroy
do
|
r
|
before_destroy
do
|
r
|
...
...
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