lib/counter_culture/extensions.rb in counter_culture-1.3.1 vs lib/counter_culture/extensions.rb in counter_culture-1.4.0
- old
+ new
@@ -1,9 +1,9 @@
module CounterCulture
module Extensions
extend ActiveSupport::Concern
-
+
module ClassMethods
# this holds all configuration data
def after_commit_counter_cache
config = @after_commit_counter_cache || []
if superclass.respond_to?(:after_commit_counter_cache) && superclass.after_commit_counter_cache
@@ -25,11 +25,11 @@
# we keep a list of all counter caches we must maintain
@after_commit_counter_cache = []
end
if options[:column_names] && !options[:column_names].is_a?(Hash)
- raise ":column_names must be a Hash of conditions and column names"
+ raise ":column_names must be a Hash of conditions and column names"
end
# add the counter to our collection
@after_commit_counter_cache << Counter.new(self, relation, options)
end
@@ -109,13 +109,18 @@
# figure out whether the applicable counter cache changed (this can happen
# with dynamic column names)
counter_cache_name_was = counter.counter_cache_name_for(counter.previous_model(self))
counter_cache_name = counter.counter_cache_name_for(self)
- if send("#{counter.first_level_relation_foreign_key}_changed?") ||
- (counter.delta_column && send("#{counter.delta_column}_changed?")) ||
- counter_cache_name != counter_cache_name_was
+ if Rails.version >= "5.1.0"
+ foreign_key_changed = saved_changes[counter.first_level_relation_foreign_key].present?
+ delta_column_changed = (counter.delta_column && saved_changes[counter.delta_column].present?)
+ else
+ foreign_key_changed = attribute_changed?(counter.first_level_relation_foreign_key)
+ delta_column_changed = (counter.delta_column && attribute_changed?(counter.delta_column))
+ end
+ if foreign_key_changed || delta_column_changed || counter_cache_name != counter_cache_name_was
# 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)
@@ -123,6 +128,6 @@
end
end
end
end
-end
\ No newline at end of file
+end