Module: IdentityCache::ParentModelExpiration

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

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#expire_parent_cache_on_changes(parent_name, foreign_key, parent_class, only_on_foreign_key_change) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/identity_cache/parent_model_expiration.rb', line 24

def expire_parent_cache_on_changes(parent_name, foreign_key, parent_class, only_on_foreign_key_change)
  new_parent = send(parent_name)

  if new_parent && new_parent.respond_to?(:expire_primary_index, true)
    if should_expire_identity_cache_parent?(foreign_key, only_on_foreign_key_change)
      new_parent.send(:expire_primary_index)
      new_parent.send(:expire_parent_caches) if new_parent.respond_to?(:expire_parent_caches, true)
    end
  end

  if transaction_changed_attributes[foreign_key].present?
    begin
      old_parent = parent_class.find(transaction_changed_attributes[foreign_key])
      old_parent.send(:expire_primary_index) if old_parent.respond_to?(:expire_primary_index, true)
      old_parent.send(:expire_parent_caches)  if old_parent.respond_to?(:expire_parent_caches, true)
    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

  true
end

#expire_parent_cachesObject



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

def expire_parent_caches
  self.class.parent_expiration_entries.each do |parent_expiration_entry|
    send(parent_expiration_entry)
  end
end

#should_expire_identity_cache_parent?(foreign_key, only_on_foreign_key_change) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/identity_cache/parent_model_expiration.rb', line 47

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