lib/counter_culture/extensions.rb in counter_culture-2.1.2 vs lib/counter_culture/extensions.rb in counter_culture-2.1.3
- old
+ new
@@ -18,27 +18,19 @@
include AfterCommitAction unless include?(AfterCommitAction)
# initialize callbacks only once
after_create :_update_counts_after_create
- before_destroy :_update_counts_after_destroy, if: -> (model) do
- if model.respond_to?(:paranoia_destroyed?)
- !model.paranoia_destroyed?
- elsif defined?(Discard::Model) && model.class.include?(Discard::Model)
- !model.discarded?
- else
- true
- end
- end
+ before_destroy :_update_counts_after_destroy, unless: :destroyed_for_counter_culture?
if respond_to?(:before_real_destroy) &&
new.respond_to?(:paranoia_destroyed?)
before_real_destroy :_update_counts_after_destroy,
if: -> (model) { !model.paranoia_destroyed? }
end
- after_update :_update_counts_after_update
+ after_update :_update_counts_after_update, unless: :destroyed_for_counter_culture?
if respond_to?(:before_restore)
before_restore :_update_counts_after_create,
if: -> (model) { model.deleted? }
end
@@ -128,9 +120,20 @@
# increment the counter cache of the new value
counter.change_counter_cache(self, :increment => true, :counter_column => counter_cache_name)
# decrement the counter cache of the old value
counter.change_counter_cache(self, :increment => false, :was => true, :counter_column => counter_cache_name_was)
end
+ end
+ end
+
+ # check if record is soft-deleted
+ def destroyed_for_counter_culture?
+ if respond_to?(:paranoia_destroyed?)
+ paranoia_destroyed?
+ elsif defined?(Discard::Model) && self.class.include?(Discard::Model)
+ discarded?
+ else
+ false
end
end
end
end