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
cee809c9
Commit
cee809c9
authored
Oct 03, 2015
by
Ryan Bigg
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #267 from nerde/rails4
Using guard clauses.
parents
2b050e8d
6cf750a9
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
21 deletions
+11
-21
lib/paranoia.rb
+11
-21
No files found.
lib/paranoia.rb
View file @
cee809c9
...
@@ -22,16 +22,15 @@ module Paranoia
...
@@ -22,16 +22,15 @@ module Paranoia
def
with_deleted
def
with_deleted
if
ActiveRecord
::
VERSION
::
STRING
>=
"4.1"
if
ActiveRecord
::
VERSION
::
STRING
>=
"4.1"
unscope
where:
paranoia_column
return
unscope
where:
paranoia_column
else
all
.
tap
{
|
x
|
x
.
default_scoped
=
false
}
end
end
all
.
tap
{
|
x
|
x
.
default_scoped
=
false
}
end
end
def
only_deleted
def
only_deleted
if
paranoia_sentinel_value
.
nil?
if
paranoia_sentinel_value
.
nil?
with_deleted
.
where
.
not
(
paranoia_column
=>
paranoia_sentinel_value
)
return
with_deleted
.
where
.
not
(
paranoia_column
=>
paranoia_sentinel_value
)
e
lse
e
nd
# if paranoia_sentinel_value is not null, then it is possible that
# if paranoia_sentinel_value is not null, then it is possible that
# some deleted rows will hold a null value in the paranoia column
# some deleted rows will hold a null value in the paranoia column
# these will not match != sentinel value because "NULL != value" is
# these will not match != sentinel value because "NULL != value" is
...
@@ -39,7 +38,6 @@ module Paranoia
...
@@ -39,7 +38,6 @@ module Paranoia
quoted_paranoia_column
=
connection
.
quote_column_name
(
paranoia_column
)
quoted_paranoia_column
=
connection
.
quote_column_name
(
paranoia_column
)
with_deleted
.
where
(
"
#{
quoted_paranoia_column
}
IS NULL OR
#{
quoted_paranoia_column
}
!= ?"
,
paranoia_sentinel_value
)
with_deleted
.
where
(
"
#{
quoted_paranoia_column
}
IS NULL OR
#{
quoted_paranoia_column
}
!= ?"
,
paranoia_sentinel_value
)
end
end
end
alias
:deleted
:only_deleted
alias
:deleted
:only_deleted
def
restore
(
id_or_ids
,
opts
=
{})
def
restore
(
id_or_ids
,
opts
=
{})
...
@@ -78,16 +76,13 @@ module Paranoia
...
@@ -78,16 +76,13 @@ module Paranoia
transaction
do
transaction
do
run_callbacks
(
:destroy
)
do
run_callbacks
(
:destroy
)
do
result
=
delete
result
=
delete
if
result
&&
ActiveRecord
::
VERSION
::
STRING
>=
'4.2'
next
result
unless
result
&&
ActiveRecord
::
VERSION
::
STRING
>=
'4.2'
each_counter_cached_associations
do
|
association
|
each_counter_cached_associations
do
|
association
|
foreign_key
=
association
.
reflection
.
foreign_key
.
to_sym
foreign_key
=
association
.
reflection
.
foreign_key
.
to_sym
unless
destroyed_by_association
&&
destroyed_by_association
.
foreign_key
.
to_sym
==
foreign_key
next
if
destroyed_by_association
&&
destroyed_by_association
.
foreign_key
.
to_sym
==
foreign_key
if
send
(
association
.
reflection
.
name
)
next
unless
send
(
association
.
reflection
.
name
)
association
.
decrement_counters
association
.
decrement_counters
end
end
end
end
end
result
result
end
end
end
end
...
@@ -202,13 +197,11 @@ class ActiveRecord::Base
...
@@ -202,13 +197,11 @@ class ActiveRecord::Base
association_data
=
self
.
send
(
name
)
association_data
=
self
.
send
(
name
)
# has_one association can return nil
# has_one association can return nil
# .paranoid? will work for both instances and classes
# .paranoid? will work for both instances and classes
if
association_data
&&
association_data
.
paranoid?
next
unless
association_data
&&
association_data
.
paranoid?
if
reflection
.
collection?
if
reflection
.
collection?
association_data
.
with_deleted
.
each
(
&
:really_destroy!
)
next
association_data
.
with_deleted
.
each
(
&
:really_destroy!
)
else
association_data
.
really_destroy!
end
end
end
association_data
.
really_destroy!
end
end
end
end
write_attribute
(
paranoia_column
,
current_time_from_proper_timezone
)
write_attribute
(
paranoia_column
,
current_time_from_proper_timezone
)
...
@@ -268,11 +261,8 @@ module ActiveRecord
...
@@ -268,11 +261,8 @@ module ActiveRecord
protected
protected
def
build_relation_with_paranoia
(
klass
,
table
,
attribute
,
value
)
def
build_relation_with_paranoia
(
klass
,
table
,
attribute
,
value
)
relation
=
build_relation_without_paranoia
(
klass
,
table
,
attribute
,
value
)
relation
=
build_relation_without_paranoia
(
klass
,
table
,
attribute
,
value
)
if
klass
.
respond_to?
(
:paranoia_column
)
return
relation
unless
klass
.
respond_to?
(
:paranoia_column
)
relation
.
and
(
klass
.
arel_table
[
klass
.
paranoia_column
].
eq
(
klass
.
paranoia_sentinel_value
))
relation
.
and
(
klass
.
arel_table
[
klass
.
paranoia_column
].
eq
(
klass
.
paranoia_sentinel_value
))
else
relation
end
end
end
alias_method_chain
:build_relation
,
:paranoia
alias_method_chain
:build_relation
,
:paranoia
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