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
|
# File 'lib/active_tools/active_record/custom_counter_cache/instance_methods.rb', line 24
def custom_counter_cache_after_update(assoc_name, reflection, assoc_mapping)
record_changed =
if reflection.polymorphic?
send((Rails.version >= "5.1" ? :saved_change_to_attribute? : :attribute_changed?), reflection.foreign_type)||send((Rails.version >= "5.1" ? :saved_change_to_attribute? : :attribute_changed?), reflection.foreign_key)
else
send((Rails.version >= "5.1" ? :saved_change_to_attribute? : :attribute_changed?), reflection.foreign_key)
end
if (@_after_create_custom_counter_called ||= false)
@_after_create_custom_counter_called = false
elsif !new_record? && record_changed
model = reflection.options[:polymorphic] ? attribute(reflection.foreign_type).try(:constantize) : reflection.klass
model_was = reflection.options[:polymorphic] ? send((Rails.version >= "5.1" ? :attribute_before_last_save : :attribute_was), reflection.foreign_type).try(:constantize) : reflection.klass
foreign_key = attribute(reflection.foreign_key)
foreign_key_was = send((Rails.version >= "5.1" ? :attribute_before_last_save : :attribute_was), reflection.foreign_key)
if foreign_key && model.respond_to?(:increment_counter) && to_increment = model.find_by_id(foreign_key)
ActiveRecord::CustomCounterCache.digger(self, to_increment, assoc_mapping) do |parent, cache_column, value|
parent.class.update_counters(parent.id, cache_column => value)
end
end
if foreign_key_was && model_was.respond_to?(:decrement_counter) && to_decrement = model_was.find_by_id(foreign_key_was)
ActiveRecord::CustomCounterCache.digger(self, to_decrement, assoc_mapping) do |parent, cache_column, value|
parent.class.update_counters(parent.id, cache_column => -value)
end
end
end
end
|