guard_against_physical_delete 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +4 -4
- data/README.rdoc +1 -1
- data/guard_against_physical_delete.gemspec +7 -6
- data/lib/guard_against_physical_delete.rb +2 -2
- data/lib/guard_against_physical_delete/base.rb +12 -1
- data/lib/guard_against_physical_delete/relation.rb +9 -8
- data/lib/guard_against_physical_delete/support_counter_cache.rb +1 -12
- data/lib/guard_against_physical_delete/support_counter_cache/associations/builder/belongs_to.rb +42 -18
- data/lib/guard_against_physical_delete/version.rb +3 -0
- metadata +20 -36
- data/lib/guard_against_physical_delete/support_counter_cache/3.0.10/associations.rb +0 -43
- data/lib/guard_against_physical_delete/support_counter_cache/3.2_and_4.0/associations/builder/belongs_to.rb +0 -50
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 45d992be15cebf1a69af7a5309431c76d75419b685270406b603358fd927768b
|
4
|
+
data.tar.gz: dcf4e518665fd15fdc6bcc69de2a8221359c2ffa87505e2ad3655f8c2c93de47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fbec342b295649e9b26c8ec65d0a03816c4486a79ab47a2416df21b81ea25579fb3fe28a2836f845b38f535806d56fe00c242ef68927404aebbc0a653a94e1e
|
7
|
+
data.tar.gz: 24a22455eecaf42ac697f4e6e53b7d5398153b8327855439c26a9b7f079c086224af9a766e756a246f9ff45fbe711a38091e722d59fd708f727d77954f086a31
|
data/.travis.yml
CHANGED
data/README.rdoc
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
|
3
|
+
require_relative "lib/guard_against_physical_delete/version"
|
4
|
+
|
3
5
|
Gem::Specification.new do |s|
|
4
6
|
s.name = "guard_against_physical_delete"
|
5
|
-
s.version =
|
7
|
+
s.version = GuardAgainstPhysicalDelete::VERSION
|
6
8
|
|
7
9
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
10
|
s.authors = ["MORITA shingo"]
|
@@ -18,11 +20,10 @@ Gem::Specification.new do |s|
|
|
18
20
|
s.rubygems_version = "1.8.15"
|
19
21
|
s.summary = "Guard against physical delete"
|
20
22
|
|
21
|
-
s.add_runtime_dependency "activerecord", "
|
22
|
-
s.add_development_dependency "rake", "
|
23
|
-
s.add_development_dependency "rspec", "
|
24
|
-
s.add_development_dependency "sqlite3"
|
25
|
-
s.add_development_dependency "rdoc", "~> 3.12"
|
23
|
+
s.add_runtime_dependency "activerecord", "~> 5"
|
24
|
+
s.add_development_dependency "rake", "~> 13"
|
25
|
+
s.add_development_dependency "rspec", "~> 3"
|
26
|
+
s.add_development_dependency "sqlite3", "~> 1.4"
|
26
27
|
s.add_development_dependency "bundler"
|
27
28
|
s.add_development_dependency "database_cleaner"
|
28
29
|
s.add_development_dependency "simplecov"
|
@@ -2,10 +2,10 @@ require 'active_record'
|
|
2
2
|
require 'guard_against_physical_delete/physical_delete_error'
|
3
3
|
require 'guard_against_physical_delete/relation'
|
4
4
|
require 'guard_against_physical_delete/base'
|
5
|
+
require 'guard_against_physical_delete/support_counter_cache'
|
5
6
|
|
6
7
|
ActiveSupport.on_load :active_record do
|
7
8
|
ActiveRecord::Base.send(:include, GuardAgainstPhysicalDelete::Base)
|
8
9
|
ActiveRecord::Relation.send(:include, GuardAgainstPhysicalDelete::Relation)
|
9
|
-
|
10
|
-
require 'guard_against_physical_delete/support_counter_cache'
|
10
|
+
ActiveRecord::Associations::Builder::BelongsTo.send(:include, GuardAgainstPhysicalDelete::SupportCounterCache::Associations::Builder::BelongsTo)
|
11
11
|
end
|
@@ -12,6 +12,7 @@ module GuardAgainstPhysicalDelete
|
|
12
12
|
set_logical_delete_column :deleted_at
|
13
13
|
end
|
14
14
|
obj.send(:include, InstanceMethods)
|
15
|
+
obj.prepend MethodOverrides if ActiveRecord.version >= Gem::Version.new('5.2.0') # #delete no longer calls Relation#delete_all
|
15
16
|
end
|
16
17
|
|
17
18
|
module ClassMethods
|
@@ -36,7 +37,7 @@ module GuardAgainstPhysicalDelete
|
|
36
37
|
private
|
37
38
|
|
38
39
|
THREAD_LOCAL_KEY = :__GuardAgainstPhysicalDelete__thread_local_permission__
|
39
|
-
private_constant :THREAD_LOCAL_KEY
|
40
|
+
private_constant :THREAD_LOCAL_KEY
|
40
41
|
|
41
42
|
def physical_delete_permission
|
42
43
|
Thread.current[THREAD_LOCAL_KEY] ||= Hash.new { |h,k| h[k] = 0 }
|
@@ -53,5 +54,15 @@ module GuardAgainstPhysicalDelete
|
|
53
54
|
self.save!
|
54
55
|
end
|
55
56
|
end
|
57
|
+
|
58
|
+
module MethodOverrides
|
59
|
+
def delete
|
60
|
+
unless self.class.delete_permitted?
|
61
|
+
raise GuardAgainstPhysicalDelete::PhysicalDeleteError, self.class.name
|
62
|
+
end
|
63
|
+
|
64
|
+
super
|
65
|
+
end
|
66
|
+
end
|
56
67
|
end
|
57
68
|
end
|
@@ -1,15 +1,16 @@
|
|
1
1
|
module GuardAgainstPhysicalDelete
|
2
2
|
module Relation
|
3
|
-
def self.included(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
def self.included(base)
|
4
|
+
base.prepend MethodOverrides
|
5
|
+
end
|
6
|
+
|
7
|
+
module MethodOverrides
|
8
|
+
def delete_all
|
9
|
+
unless klass.delete_permitted?
|
10
|
+
raise GuardAgainstPhysicalDelete::PhysicalDeleteError, klass.name
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
+
super
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -1,12 +1 @@
|
|
1
|
-
|
2
|
-
require 'guard_against_physical_delete/support_counter_cache/associations/builder/belongs_to'
|
3
|
-
ActiveRecord::Associations::Builder::BelongsTo.send(:include, GuardAgainstPhysicalDelete::SupportCounterCache::Associations::Builder::BelongsTo)
|
4
|
-
elsif ActiveRecord::VERSION::STRING >= "3.2"
|
5
|
-
require 'guard_against_physical_delete/support_counter_cache/3.2_and_4.0/associations/builder/belongs_to'
|
6
|
-
ActiveRecord::Associations::Builder::BelongsTo.send(:include, GuardAgainstPhysicalDelete::SupportCounterCache::Associations::Builder::BelongsTo)
|
7
|
-
elsif ActiveRecord::VERSION::STRING == "3.0.10"
|
8
|
-
require 'guard_against_physical_delete/support_counter_cache/3.0.10/associations'
|
9
|
-
ActiveRecord::Base.send(:include, GuardAgainstPhysicalDelete::SupportCounterCache::Associations)
|
10
|
-
else
|
11
|
-
raise "guard_against_physical_delete doesn't support ActiveRecord version #{ActiveRecord::VERSION::STRING}"
|
12
|
-
end
|
1
|
+
require 'guard_against_physical_delete/support_counter_cache/associations/builder/belongs_to'
|
data/lib/guard_against_physical_delete/support_counter_cache/associations/builder/belongs_to.rb
CHANGED
@@ -5,20 +5,17 @@ module GuardAgainstPhysicalDelete
|
|
5
5
|
module BelongsTo
|
6
6
|
def self.included(obj)
|
7
7
|
class << obj
|
8
|
+
prepend MethodOverrides
|
9
|
+
|
8
10
|
private
|
9
|
-
def add_counter_cache_methods_with_logical_delete(mixin)
|
10
|
-
add_counter_cache_methods_without_logical_delete mixin
|
11
|
-
add_logical_delete_counter_cache_methods(mixin)
|
12
|
-
end
|
13
|
-
alias_method_chain :add_counter_cache_methods, :logical_delete
|
14
11
|
|
15
12
|
def add_logical_delete_counter_cache_methods(mixin)
|
16
13
|
mixin.class_eval do
|
17
14
|
def belongs_to_counter_cache_after_logical_delete(reflection)
|
18
15
|
cache_column = reflection.counter_cache_column
|
19
16
|
delete_column = self.logical_delete_column
|
20
|
-
return unless
|
21
|
-
return unless
|
17
|
+
return unless saved_change_to_logical_delete_column?(delete_column)
|
18
|
+
return unless logical_delete_column_before_last_save(delete_column).nil?
|
22
19
|
|
23
20
|
record = send(reflection.name)
|
24
21
|
record.class.decrement_counter(cache_column, record.id) unless record.nil?
|
@@ -27,29 +24,56 @@ module GuardAgainstPhysicalDelete
|
|
27
24
|
def belongs_to_counter_cache_after_revive(reflection)
|
28
25
|
cache_column = reflection.counter_cache_column
|
29
26
|
delete_column = self.logical_delete_column
|
30
|
-
return unless
|
31
|
-
return if
|
27
|
+
return unless saved_change_to_logical_delete_column?(delete_column)
|
28
|
+
return if logical_delete_column_before_last_save(delete_column).nil?
|
32
29
|
return unless send(delete_column).nil?
|
33
30
|
record = send(reflection.name)
|
34
31
|
record.class.increment_counter(cache_column, record.id) unless record.nil?
|
35
32
|
end
|
33
|
+
|
34
|
+
if ActiveRecord.version < Gem::Version.new('5.1.0')
|
35
|
+
def saved_change_to_logical_delete_column?(delete_column)
|
36
|
+
__send__("#{delete_column}_changed?")
|
37
|
+
end
|
38
|
+
|
39
|
+
def logical_delete_column_before_last_save(delete_column)
|
40
|
+
__send__("#{delete_column}_was")
|
41
|
+
end
|
42
|
+
else
|
43
|
+
def saved_change_to_logical_delete_column?(delete_column)
|
44
|
+
__send__("saved_change_to_#{delete_column}?")
|
45
|
+
end
|
46
|
+
|
47
|
+
def logical_delete_column_before_last_save(delete_column)
|
48
|
+
__send__("#{delete_column}_before_last_save")
|
49
|
+
end
|
50
|
+
end
|
36
51
|
end
|
52
|
+
|
37
53
|
mixin.redefine_method("belongs_to_counter_cache_after_update_for_#{name}") do
|
38
54
|
# do nothing
|
39
55
|
end if mixin.method_defined?("belongs_to_counter_cache_after_update_for_#{name}")
|
40
56
|
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
41
60
|
|
42
|
-
|
43
|
-
|
61
|
+
module MethodOverrides
|
62
|
+
private
|
44
63
|
|
45
|
-
|
64
|
+
def add_counter_cache_methods(mixin)
|
65
|
+
super(mixin)
|
66
|
+
add_logical_delete_counter_cache_methods(mixin)
|
67
|
+
end
|
46
68
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
69
|
+
def add_counter_cache_callbacks(model, reflection)
|
70
|
+
super(model, reflection)
|
71
|
+
|
72
|
+
return unless model.logical_delete?
|
73
|
+
|
74
|
+
model.after_update -> record do
|
75
|
+
record.belongs_to_counter_cache_after_logical_delete(reflection)
|
76
|
+
record.belongs_to_counter_cache_after_revive(reflection)
|
53
77
|
end
|
54
78
|
end
|
55
79
|
end
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: guard_against_physical_delete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MORITA shingo
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2012-03-06 00:00:00.000000000 Z
|
@@ -14,72 +14,58 @@ dependencies:
|
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '5'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '3'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sqlite3
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rdoc
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
59
|
- - "~>"
|
74
60
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
61
|
+
version: '1.4'
|
76
62
|
type: :development
|
77
63
|
prerelease: false
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
79
65
|
requirements:
|
80
66
|
- - "~>"
|
81
67
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
68
|
+
version: '1.4'
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: bundler
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -155,13 +141,12 @@ files:
|
|
155
141
|
- lib/guard_against_physical_delete/physical_delete_error.rb
|
156
142
|
- lib/guard_against_physical_delete/relation.rb
|
157
143
|
- lib/guard_against_physical_delete/support_counter_cache.rb
|
158
|
-
- lib/guard_against_physical_delete/support_counter_cache/3.0.10/associations.rb
|
159
|
-
- lib/guard_against_physical_delete/support_counter_cache/3.2_and_4.0/associations/builder/belongs_to.rb
|
160
144
|
- lib/guard_against_physical_delete/support_counter_cache/associations/builder/belongs_to.rb
|
145
|
+
- lib/guard_against_physical_delete/version.rb
|
161
146
|
homepage: http://github.com/cookpad/guard_against_physical_delete
|
162
147
|
licenses: []
|
163
148
|
metadata: {}
|
164
|
-
post_install_message:
|
149
|
+
post_install_message:
|
165
150
|
rdoc_options: []
|
166
151
|
require_paths:
|
167
152
|
- lib
|
@@ -176,9 +161,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
161
|
- !ruby/object:Gem::Version
|
177
162
|
version: '0'
|
178
163
|
requirements: []
|
179
|
-
|
180
|
-
|
181
|
-
signing_key:
|
164
|
+
rubygems_version: 3.1.4
|
165
|
+
signing_key:
|
182
166
|
specification_version: 4
|
183
167
|
summary: Guard against physical delete
|
184
168
|
test_files: []
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module GuardAgainstPhysicalDelete
|
2
|
-
module SupportCounterCache
|
3
|
-
module Associations
|
4
|
-
def self.included(obj)
|
5
|
-
class << obj
|
6
|
-
def add_counter_cache_callbacks_with_logical_delete(reflection)
|
7
|
-
add_counter_cache_callbacks_without_logical_delete(reflection)
|
8
|
-
return unless logical_delete?
|
9
|
-
add_logical_delete_counter_cache_callback(reflection)
|
10
|
-
end
|
11
|
-
alias_method_chain :add_counter_cache_callbacks, :logical_delete
|
12
|
-
|
13
|
-
def add_logical_delete_counter_cache_callback(reflection)
|
14
|
-
cache_column = reflection.counter_cache_column
|
15
|
-
|
16
|
-
logical_delete_method_name = "belongs_to_counter_cache_before_logical_delete_for_#{reflection.name}".to_sym
|
17
|
-
define_method(logical_delete_method_name) do
|
18
|
-
delete_column = self.logical_delete_column
|
19
|
-
next unless send("#{delete_column}_changed?")
|
20
|
-
next unless send("#{delete_column}_was").nil?
|
21
|
-
association = send(reflection.name)
|
22
|
-
association.class.decrement_counter(cache_column, association.id) unless association.nil?
|
23
|
-
end
|
24
|
-
|
25
|
-
revive_method_name = "belongs_to_counter_cache_before_revive_for_#{reflection.name}".to_sym
|
26
|
-
define_method(revive_method_name) do
|
27
|
-
delete_column = self.logical_delete_column
|
28
|
-
next unless send("#{delete_column}_changed?")
|
29
|
-
next if send("#{delete_column}_was").nil?
|
30
|
-
next unless send(delete_column).nil?
|
31
|
-
|
32
|
-
clear_association_cache
|
33
|
-
association = send(reflection.name)
|
34
|
-
association.class.increment_counter(cache_column, association.id) unless association.nil?
|
35
|
-
end
|
36
|
-
|
37
|
-
before_update(logical_delete_method_name, revive_method_name)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module GuardAgainstPhysicalDelete
|
2
|
-
module SupportCounterCache
|
3
|
-
module Associations
|
4
|
-
module Builder
|
5
|
-
module BelongsTo
|
6
|
-
def self.included(obj)
|
7
|
-
obj.class_eval do
|
8
|
-
def add_counter_cache_callbacks_with_logical_delete(reflection)
|
9
|
-
add_counter_cache_callbacks_without_logical_delete(reflection)
|
10
|
-
return unless model.logical_delete?
|
11
|
-
add_logical_delete_counter_cache_callback(reflection)
|
12
|
-
end
|
13
|
-
alias_method_chain :add_counter_cache_callbacks, :logical_delete
|
14
|
-
|
15
|
-
def add_logical_delete_counter_cache_callback(reflection)
|
16
|
-
cache_column = reflection.counter_cache_column
|
17
|
-
name = self.name
|
18
|
-
|
19
|
-
logical_delete_method_name = "belongs_to_counter_cache_after_logical_delete_for_#{name}".to_sym
|
20
|
-
mixin.redefine_method(logical_delete_method_name) do
|
21
|
-
delete_column = self.logical_delete_column
|
22
|
-
next unless send("#{delete_column}_changed?")
|
23
|
-
next unless send("#{delete_column}_was").nil?
|
24
|
-
record = send(name)
|
25
|
-
record.class.decrement_counter(cache_column, record.id) unless record.nil?
|
26
|
-
end
|
27
|
-
|
28
|
-
mixin.redefine_method("belongs_to_counter_cache_after_update_for_#{name}") do
|
29
|
-
# do nothing
|
30
|
-
end if mixin.method_defined?("belongs_to_counter_cache_after_update_for_#{name}")
|
31
|
-
|
32
|
-
revive_method_name = "belongs_to_counter_cache_after_revive_for_#{name}".to_sym
|
33
|
-
|
34
|
-
mixin.redefine_method(revive_method_name) do
|
35
|
-
delete_column = self.logical_delete_column
|
36
|
-
next unless send("#{delete_column}_changed?")
|
37
|
-
next if send("#{delete_column}_was").nil?
|
38
|
-
next unless send(delete_column).nil?
|
39
|
-
record = send(name)
|
40
|
-
record.class.increment_counter(cache_column, record.id) unless record.nil?
|
41
|
-
end
|
42
|
-
model.after_update(logical_delete_method_name, revive_method_name)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|