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
d99f42d3
Commit
d99f42d3
authored
Jan 21, 2015
by
John Hawthorn
Committed by
John Hawthorn
Jan 22, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix problem when destroying non-paranoid has_one association
Thanks @kidlab
parent
9b8158e2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
10 deletions
+18
-10
lib/paranoia.rb
+9
-8
test/paranoia_test.rb
+9
-2
No files found.
lib/paranoia.rb
View file @
d99f42d3
...
@@ -170,15 +170,16 @@ class ActiveRecord::Base
...
@@ -170,15 +170,16 @@ class ActiveRecord::Base
reflection
.
options
[
:dependent
]
==
:destroy
reflection
.
options
[
:dependent
]
==
:destroy
end
end
if
dependent_reflections
.
any?
if
dependent_reflections
.
any?
dependent_reflections
.
each
do
|
name
,
_
|
dependent_reflections
.
each
do
|
name
,
reflection
|
associat
ed_records
=
self
.
send
(
name
)
associat
ion_data
=
self
.
send
(
name
)
# has_one association can return nil
# has_one association can return nil
if
associated_records
&&
associated_records
.
respond_to?
(
:with_deleted
)
# .paranoid? will work for both instances and classes
# Paranoid models will have this method, non-paranoid models will not
if
association_data
&&
association_data
.
paranoid?
associated_records
.
with_deleted
.
each
(
&
:really_destroy!
)
if
reflection
.
collection?
self
.
send
(
name
).
reload
association_data
.
with_deleted
.
each
(
&
:really_destroy!
)
elsif
associated_records
&&
!
associated_records
.
respond_to?
(
:each
)
# single record
else
associated_records
.
really_destroy!
association_data
.
really_destroy!
end
end
end
end
end
end
end
...
...
test/paranoia_test.rb
View file @
d99f42d3
...
@@ -361,9 +361,15 @@ class ParanoiaTest < test_framework
...
@@ -361,9 +361,15 @@ class ParanoiaTest < test_framework
def
test_real_destroy_dependent_destroy
def
test_real_destroy_dependent_destroy
parent
=
ParentModel
.
create
parent
=
ParentModel
.
create
child
=
parent
.
very_related_models
.
create
child1
=
parent
.
very_related_models
.
create
child2
=
parent
.
non_paranoid_models
.
create
child3
=
parent
.
create_non_paranoid_model
parent
.
really_destroy!
parent
.
really_destroy!
refute
RelatedModel
.
unscoped
.
exists?
(
child
.
id
)
refute
RelatedModel
.
unscoped
.
exists?
(
child1
.
id
)
refute
NonParanoidModel
.
unscoped
.
exists?
(
child2
.
id
)
refute
NonParanoidModel
.
unscoped
.
exists?
(
child3
.
id
)
end
end
def
test_real_destroy_dependent_destroy_after_normal_destroy
def
test_real_destroy_dependent_destroy_after_normal_destroy
...
@@ -757,6 +763,7 @@ class ParentModel < ActiveRecord::Base
...
@@ -757,6 +763,7 @@ class ParentModel < ActiveRecord::Base
has_many
:related_models
has_many
:related_models
has_many
:very_related_models
,
:class_name
=>
'RelatedModel'
,
dependent: :destroy
has_many
:very_related_models
,
:class_name
=>
'RelatedModel'
,
dependent: :destroy
has_many
:non_paranoid_models
,
dependent: :destroy
has_many
:non_paranoid_models
,
dependent: :destroy
has_one
:non_paranoid_model
,
dependent: :destroy
has_many
:asplode_models
,
dependent: :destroy
has_many
:asplode_models
,
dependent: :destroy
has_one
:polymorphic_model
,
as: :parent
,
dependent: :destroy
has_one
:polymorphic_model
,
as: :parent
,
dependent: :destroy
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