Module: CacheNinja

Extended by:
ActiveSupport::Concern
Defined in:
lib/cache_ninja.rb,
lib/cache_ninja/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Instance Method Details

#clear_cached_dataObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cache_ninja.rb', line 50

def clear_cached_data
  object_cache_key = "#{self.class.to_s.underscore}/#{id}"
  Rails.cache.delete(object_cache_key)

  self_n_parents = collect_parent_classes(self.class)
  self_n_parents.each do |self_n_parent|
    next unless self_n_parent.respond_to?(:cached_associations) && self_n_parent.cached_associations.present?

    self_n_parent.cached_associations.each do |association|
      Rails.cache.delete("#{self_n_parent.name.downcase}_#{id}_cached_#{association}")
      assoc_class_n_id = Array(send(association)).compact.map { |obj| [obj.class, obj.id] }

      assoc_class_n_id.each do |assoc_class, assoc_id|
        assoc_self_n_parents = collect_parent_classes(assoc_class)
        assoc_self_n_parents.each do |assoc_self_n_parent|
          Rails.cache.delete("#{assoc_self_n_parent.name.downcase}_#{assoc_id}_cached_#{self_n_parent.name.underscore.singularize}")
          Rails.cache.delete("#{assoc_self_n_parent.name.downcase}_#{assoc_id}_cached_#{self_n_parent.name.underscore.pluralize}")
        end
      end
    end
  end
end

#collect_parent_classes(klass) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/cache_ninja.rb', line 73

def collect_parent_classes(klass)
  parents = []
  while klass != ActiveRecord::Base
    parents << klass
    klass = klass.superclass
  end
  parents
end