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
5f158301
Commit
5f158301
authored
Aug 03, 2015
by
Amit Aharoni
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed issue #258: uniqueness validator now works with custom active column
parent
bb98963b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
2 deletions
+35
-2
lib/paranoia.rb
+1
-1
test/paranoia_test.rb
+34
-1
No files found.
lib/paranoia.rb
View file @
5f158301
...
...
@@ -269,7 +269,7 @@ module ActiveRecord
def
build_relation_with_paranoia
(
klass
,
table
,
attribute
,
value
)
relation
=
build_relation_without_paranoia
(
klass
,
table
,
attribute
,
value
)
if
klass
.
respond_to?
(
:paranoia_column
)
relation
.
and
(
klass
.
arel_table
[
klass
.
paranoia_column
].
eq
(
nil
))
relation
.
and
(
klass
.
arel_table
[
klass
.
paranoia_column
].
eq
(
klass
.
paranoia_sentinel_value
))
else
relation
end
...
...
test/paranoia_test.rb
View file @
5f158301
...
...
@@ -38,7 +38,8 @@ def setup!
'namespaced_paranoid_has_ones'
=>
'deleted_at DATETIME, paranoid_belongs_tos_id INTEGER'
,
'namespaced_paranoid_belongs_tos'
=>
'deleted_at DATETIME, paranoid_has_one_id INTEGER'
,
'unparanoid_unique_models'
=>
'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER'
,
'active_column_models'
=>
'deleted_at DATETIME, active BOOLEAN'
'active_column_models'
=>
'deleted_at DATETIME, active BOOLEAN'
,
'active_column_model_with_uniqueness_validations'
=>
'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN'
}.
each
do
|
table_name
,
columns_as_sql_string
|
ActiveRecord
::
Base
.
connection
.
execute
"CREATE TABLE
#{
table_name
}
(id INTEGER NOT NULL PRIMARY KEY,
#{
columns_as_sql_string
}
)"
end
...
...
@@ -221,6 +222,19 @@ class ParanoiaTest < test_framework
assert_equal
1
,
model
.
class
.
deleted
.
count
end
def
test_active_column_model_with_uniqueness_validation_only_checks_non_deleted_records
a
=
ActiveColumnModelWithUniquenessValidation
.
create!
(
name:
"A"
)
a
.
destroy
b
=
ActiveColumnModelWithUniquenessValidation
.
new
(
name:
"A"
)
assert
b
.
valid?
end
def
test_active_column_model_with_uniqueness_validation_still_works_on_non_deleted_records
a
=
ActiveColumnModelWithUniquenessValidation
.
create!
(
name:
"A"
)
b
=
ActiveColumnModelWithUniquenessValidation
.
new
(
name:
"A"
)
refute
b
.
valid?
end
def
test_sentinel_value_for_custom_sentinel_models
model
=
CustomSentinelModel
.
new
assert_equal
0
,
model
.
class
.
count
...
...
@@ -1032,6 +1046,25 @@ class ActiveColumnModel < ActiveRecord::Base
end
end
class
ActiveColumnModelWithUniquenessValidation
<
ActiveRecord
::
Base
validates
:name
,
:uniqueness
=>
true
acts_as_paranoid
column: :active
,
sentinel_value:
true
def
paranoia_restore_attributes
{
deleted_at:
nil
,
active:
true
}
end
def
paranoia_destroy_attributes
{
deleted_at:
current_time_from_proper_timezone
,
active:
nil
}
end
end
class
NonParanoidModel
<
ActiveRecord
::
Base
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