Module: IdentityCache::ClassMethods::ParentModelExpiration

Defined in:
lib/identity_cache.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#expire_parent_cache_on_changes(parent_name, foreign_key, parent_class, options = {}) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/identity_cache.rb', line 467

def expire_parent_cache_on_changes(parent_name, foreign_key, parent_class, options = {})
  new_parent = send(parent_name)

  if new_parent && new_parent.respond_to?(:expire_primary_index, true)
    if should_expire_identity_cache_parent?(foreign_key, options[:only_on_foreign_key_change])
      new_parent.expire_primary_index
      new_parent.expire_parent_cache if new_parent.respond_to?(:expire_parent_cache)
    end
  end

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

#should_expire_identity_cache_parent?(foreign_key, only_on_foreign_key_change) ⇒ Boolean

Returns:

  • (Boolean)


490
491
492
493
494
495
496
# File 'lib/identity_cache.rb', line 490

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