Module: IdentityCache::ParentModelExpiration

Extended by:
ActiveSupport::Concern
Defined in:
lib/identity_cache/parent_model_expiration.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_parents_to_cache_expiry_set(parents_to_expire) ⇒ Object



18
19
20
21
22
# File 'lib/identity_cache/parent_model_expiration.rb', line 18

def add_parents_to_cache_expiry_set(parents_to_expire)
  self.class.parent_expiration_entries.each do |association_name, cached_associations|
    parents_to_expire_on_changes(parents_to_expire, association_name, cached_associations)
  end
end

#add_record_to_cache_expiry_set(parents_to_expire, record) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/identity_cache/parent_model_expiration.rb', line 24

def add_record_to_cache_expiry_set(parents_to_expire, record)
  key = record.primary_cache_index_key
  unless parents_to_expire[key]
    parents_to_expire[key] = record
    record.add_parents_to_cache_expiry_set(parents_to_expire) if record.respond_to?(:add_parents_to_cache_expiry_set, true)
  end
end

#expire_parent_cachesObject



10
11
12
13
14
15
16
# File 'lib/identity_cache/parent_model_expiration.rb', line 10

def expire_parent_caches
  parents_to_expire = {}
  add_parents_to_cache_expiry_set(parents_to_expire)
  parents_to_expire.each_value do |parent|
    parent.send(:expire_primary_index)
  end
end

#parents_to_expire_on_changes(parents_to_expire, association_name, cached_associations) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/identity_cache/parent_model_expiration.rb', line 32

def parents_to_expire_on_changes(parents_to_expire, association_name, cached_associations)
  parent_association = self.class.reflect_on_association(association_name)
  foreign_key = parent_association.association_foreign_key

  new_parent = send(association_name)

  old_parent = nil
  if transaction_changed_attributes[foreign_key].present?
    begin
      if parent_association.options[:polymorhpic]
        klass = transaction_changed_attributes[parent_association.association_foreign_key].try(:constantize)
        klass ||= new_parent.class
      else
        klass = parent_association.klass
      end
      old_parent = klass.find(transaction_changed_attributes[foreign_key])
    rescue ActiveRecord::RecordNotFound => e
      # suppress errors finding the old parent if its been destroyed since it will have expired itself in that case
    end
  end

  cached_associations.each do |parent_class, only_on_foreign_key_change|
    if new_parent && new_parent.is_a?(parent_class) && should_expire_identity_cache_parent?(foreign_key, only_on_foreign_key_change)
      add_record_to_cache_expiry_set(parents_to_expire, new_parent)
    end

    if old_parent && old_parent.is_a?(parent_class)
      add_record_to_cache_expiry_set(parents_to_expire, old_parent)
    end
  end
end

#should_expire_identity_cache_parent?(foreign_key, only_on_foreign_key_change) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/identity_cache/parent_model_expiration.rb', line 64

def should_expire_identity_cache_parent?(foreign_key, only_on_foreign_key_change)
  if only_on_foreign_key_change
    destroyed? || was_new_record? || transaction_changed_attributes[foreign_key].present?
  else
    true
  end
end