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
7c0d4ac5
Unverified
Commit
7c0d4ac5
authored
Nov 03, 2017
by
John Hawthorn
Committed by
GitHub
Nov 03, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #423 from jhawthorn/paranoia_aliases
Add paranoia_destroy and paranoia_delete aliases
parents
acd43c15
c63e0ab0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
27 deletions
+15
-27
lib/paranoia.rb
+15
-27
No files found.
lib/paranoia.rb
View file @
7c0d4ac5
...
...
@@ -14,7 +14,6 @@ module Paranoia
def
self
.
included
(
klazz
)
klazz
.
extend
Query
klazz
.
extend
Callbacks
end
module
Query
...
...
@@ -53,31 +52,11 @@ module Paranoia
end
end
module
Callbacks
def
self
.
extended
(
klazz
)
[
:restore
,
:real_destroy
].
each
do
|
callback_name
|
klazz
.
define_callbacks
callback_name
klazz
.
define_singleton_method
(
"before_
#{
callback_name
}
"
)
do
|*
args
,
&
block
|
set_callback
(
callback_name
,
:before
,
*
args
,
&
block
)
end
klazz
.
define_singleton_method
(
"around_
#{
callback_name
}
"
)
do
|*
args
,
&
block
|
set_callback
(
callback_name
,
:around
,
*
args
,
&
block
)
end
klazz
.
define_singleton_method
(
"after_
#{
callback_name
}
"
)
do
|*
args
,
&
block
|
set_callback
(
callback_name
,
:after
,
*
args
,
&
block
)
end
end
end
end
def
destroy
def
paranoia_destroy
transaction
do
run_callbacks
(
:destroy
)
do
@_disable_counter_cache
=
deleted?
result
=
delete
result
=
paranoia_
delete
next
result
unless
result
&&
ActiveRecord
::
VERSION
::
STRING
>=
'4.2'
each_counter_cached_associations
do
|
association
|
foreign_key
=
association
.
reflection
.
foreign_key
.
to_sym
...
...
@@ -90,8 +69,14 @@ module Paranoia
end
end
end
alias_method
:destroy
,
:paranoia_destroy
def
delete
def
paranoia_destroy!
paranoia_destroy
||
raise
(
ActiveRecord
::
RecordNotDestroyed
.
new
(
"Failed to destroy the record"
,
self
))
end
def
paranoia_delete
raise
ActiveRecord
::
ReadOnlyRecord
,
"
#{
self
.
class
}
is marked as readonly"
if
readonly?
if
persisted?
# if a transaction exists, add the record so that after_commit
...
...
@@ -103,6 +88,7 @@ module Paranoia
end
self
end
alias_method
:delete
,
:paranoia_delete
def
restore!
(
opts
=
{})
self
.
class
.
transaction
do
...
...
@@ -112,7 +98,7 @@ module Paranoia
# This only happened on Rails versions earlier than 4.1.
noop_if_frozen
=
ActiveRecord
.
version
<
Gem
::
Version
.
new
(
"4.1"
)
if
within_recovery_window?
(
recovery_window_range
)
&&
((
noop_if_frozen
&&
!
@attributes
.
frozen?
)
||
!
noop_if_frozen
)
@_disable_counter_cache
=
!
delet
ed?
@_disable_counter_cache
=
!
paranoia_destroy
ed?
write_attribute
paranoia_column
,
paranoia_sentinel_value
update_columns
(
paranoia_restore_attributes
)
each_counter_cached_associations
do
|
association
|
...
...
@@ -149,7 +135,7 @@ module Paranoia
def
really_destroy!
transaction
do
run_callbacks
(
:real_destroy
)
do
@_disable_counter_cache
=
delet
ed?
@_disable_counter_cache
=
paranoia_destroy
ed?
dependent_reflections
=
self
.
class
.
reflections
.
select
do
|
name
,
reflection
|
reflection
.
options
[
:dependent
]
==
:destroy
end
...
...
@@ -241,6 +227,8 @@ end
ActiveSupport
.
on_load
(
:active_record
)
do
class
ActiveRecord
::
Base
def
self
.
acts_as_paranoid
(
options
=
{})
define_model_callbacks
:restore
,
:real_destroy
alias_method
:really_destroyed?
,
:destroyed?
alias_method
:really_delete
,
:delete
alias_method
:destroy_without_paranoia
,
:destroy
...
...
@@ -317,7 +305,7 @@ module ActiveRecord
class
AssociationNotSoftDestroyedValidator
<
ActiveModel
::
EachValidator
def
validate_each
(
record
,
attribute
,
value
)
# if association is soft destroyed, add an error
if
value
.
present?
&&
value
.
delet
ed?
if
value
.
present?
&&
value
.
paranoia_destroy
ed?
record
.
errors
[
attribute
]
<<
'has been soft-deleted'
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