lib/kasket/write_mixin.rb in kasket-4.1.0 vs lib/kasket/write_mixin.rb in kasket-4.2.0
- old
+ new
@@ -38,12 +38,12 @@
end
def kasket_keys
attribute_sets = [attributes.symbolize_keys]
- if changed?
- old_attributes = Hash[*changes.map {|attribute, values| [attribute, values[0]]}.flatten].symbolize_keys
+ if previous_changes.present?
+ old_attributes = Hash[*previous_changes.map {|attribute, values| [attribute, values[0]]}.flatten].symbolize_keys
attribute_sets << old_attributes.reverse_merge(attribute_sets[0])
end
keys = []
self.class.kasket_indices.each do |index|
@@ -56,10 +56,23 @@
keys.flatten!
keys.uniq!
keys
end
+ def kasket_after_commit
+ keys = kasket_keys
+
+ if persisted? && Kasket::CONFIGURATION[:write_through]
+ key = store_in_kasket
+ keys.delete(key)
+ end
+
+ keys.each do |key|
+ Kasket.cache.delete(key)
+ end
+ end
+
def clear_kasket_indices
kasket_keys.each do |key|
Kasket.cache.delete(key)
end
end
@@ -82,12 +95,14 @@
unless model_class.method_defined?(:kasket_cacheable?)
model_class.send(:alias_method, :kasket_cacheable?, :default_kasket_cacheable?)
end
- model_class.after_save :clear_kasket_indices
- model_class.after_touch :clear_kasket_indices
- model_class.after_destroy :clear_kasket_indices
+ model_class.after_commit :kasket_after_commit
+
+ if ActiveRecord::VERSION::MAJOR == 3 || (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0)
+ model_class.after_touch :kasket_after_commit
+ end
class << model_class
alias_method_chain :transaction, :kasket_disabled
alias_method_chain :update_counters, :kasket_clearing
end