6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/guard_against_physical_delete/support_counter_cache/associations/builder/belongs_to.rb', line 6
def self.included(obj)
class << obj
prepend MethodOverrides
private
def add_logical_delete_counter_cache_methods(mixin)
mixin.class_eval do
def belongs_to_counter_cache_after_logical_delete(reflection)
cache_column = reflection.counter_cache_column
delete_column = self.logical_delete_column
return unless saved_change_to_logical_delete_column?(delete_column)
return unless logical_delete_column_before_last_save(delete_column).nil?
record = send(reflection.name)
record.class.decrement_counter(cache_column, record.id) unless record.nil?
end
def belongs_to_counter_cache_after_revive(reflection)
cache_column = reflection.counter_cache_column
delete_column = self.logical_delete_column
return unless saved_change_to_logical_delete_column?(delete_column)
return if logical_delete_column_before_last_save(delete_column).nil?
return unless send(delete_column).nil?
record = send(reflection.name)
record.class.increment_counter(cache_column, record.id) unless record.nil?
end
if ActiveRecord.version < Gem::Version.new('5.1.0')
def saved_change_to_logical_delete_column?(delete_column)
__send__("#{delete_column}_changed?")
end
def logical_delete_column_before_last_save(delete_column)
__send__("#{delete_column}_was")
end
else
def saved_change_to_logical_delete_column?(delete_column)
__send__("saved_change_to_#{delete_column}?")
end
def logical_delete_column_before_last_save(delete_column)
__send__("#{delete_column}_before_last_save")
end
end
end
mixin.redefine_method("belongs_to_counter_cache_after_update_for_#{name}") do
end if mixin.method_defined?("belongs_to_counter_cache_after_update_for_#{name}")
end
end
end
|