permanent_records 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/permanent_records.rb +20 -4
- data/spec/permanent_records_spec.rb +28 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0.
|
1
|
+
3.0.2
|
data/lib/permanent_records.rb
CHANGED
@@ -43,15 +43,27 @@ module PermanentRecords
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def destroy(force = nil)
|
46
|
-
if !is_permanent? || (
|
46
|
+
if !is_permanent? || PermanentRecords.should_force_destroy?(force)
|
47
47
|
return permanently_delete_records_after { super() }
|
48
48
|
end
|
49
49
|
destroy_with_permanent_records force
|
50
50
|
end
|
51
51
|
|
52
|
+
def self.should_force_destroy?(force)
|
53
|
+
if Hash === force
|
54
|
+
force[:force]
|
55
|
+
else
|
56
|
+
:force == force
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.should_ignore_validations?(force)
|
61
|
+
Hash === force && false == force[:validate]
|
62
|
+
end
|
63
|
+
|
52
64
|
protected
|
53
65
|
|
54
|
-
def set_deleted_at(value)
|
66
|
+
def set_deleted_at(value, force = nil)
|
55
67
|
return self unless is_permanent?
|
56
68
|
record = self.class.unscoped.find(id)
|
57
69
|
record.deleted_at = value
|
@@ -59,7 +71,11 @@ module PermanentRecords
|
|
59
71
|
# we call save! instead of update_attribute so an ActiveRecord::RecordInvalid
|
60
72
|
# error will be raised if the record isn't valid. (This prevents reviving records that
|
61
73
|
# disregard validation constraints,)
|
62
|
-
|
74
|
+
if PermanentRecords.should_ignore_validations?(force)
|
75
|
+
record.save(:validate => false)
|
76
|
+
else
|
77
|
+
record.save!
|
78
|
+
end
|
63
79
|
@attributes, @attributes_cache = record.attributes, record.attributes
|
64
80
|
rescue Exception => e
|
65
81
|
# trigger dependent record destruction (they were revived before this record,
|
@@ -71,7 +87,7 @@ module PermanentRecords
|
|
71
87
|
|
72
88
|
def destroy_with_permanent_records(force = nil)
|
73
89
|
_run_destroy_callbacks do
|
74
|
-
deleted? || new_record? ? save : set_deleted_at(Time.now)
|
90
|
+
deleted? || new_record? ? save : set_deleted_at(Time.now, force)
|
75
91
|
end
|
76
92
|
self
|
77
93
|
end
|
@@ -45,6 +45,34 @@ describe PermanentRecords do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
context 'with hash-style :force argument' do
|
49
|
+
let(:should_force) {{ force: true }}
|
50
|
+
|
51
|
+
it 'does really remove the record' do
|
52
|
+
expect { subject }.to change { record.class.count }.by(-1)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when validations fail' do
|
57
|
+
before {
|
58
|
+
Hole.any_instance.stub(:valid?).and_return(false)
|
59
|
+
}
|
60
|
+
it 'raises' do
|
61
|
+
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'with validation opt-out' do
|
65
|
+
let(:should_force) {{ validate: false }}
|
66
|
+
it 'doesnt raise' do
|
67
|
+
expect { subject }.to_not raise_error
|
68
|
+
end
|
69
|
+
it 'soft-deletes the invalid record' do
|
70
|
+
subject.should be_deleted
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
48
76
|
context 'when model has no deleted_at column' do
|
49
77
|
let(:record) { kitty }
|
50
78
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permanent_records
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
segments:
|
92
92
|
- 0
|
93
|
-
hash:
|
93
|
+
hash: 1685930428738744402
|
94
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|