Skip to content

Commit

Permalink
use foreign_key to reference parent association
Browse files Browse the repository at this point in the history
  • Loading branch information
slucaskim committed Nov 29, 2023
1 parent d52de88 commit ecbeadf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/identity_cache/parent_model_expiration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def add_record_to_cache_expiry_set(parents_to_expire, record)

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
foreign_key = parent_association.foreign_key

new_parent = send(association_name)

Expand Down
2 changes: 1 addition & 1 deletion test/custom_primary_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class CustomPrimaryKeysTest < IdentityCache::TestCase
def setup
super
CustomParentRecord.cache_has_many(:custom_child_record)
CustomParentRecord.cache_has_many(:custom_child_records)
CustomChildRecord.cache_belongs_to(:custom_parent_record)
@parent_record = CustomParentRecord.create!(parent_primary_key: 1)
@child_record_1 = CustomChildRecord.create!(custom_parent_record: @parent_record, child_primary_key: 1)
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class StiRecordTypeA < StiRecord

class CustomParentRecord < ActiveRecord::Base
include IdentityCache
has_many :custom_child_record, foreign_key: :parent_id
has_many :custom_child_records, foreign_key: :parent_id
self.primary_key = "parent_primary_key"
end

Expand Down
29 changes: 29 additions & 0 deletions test/parent_model_expiration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,33 @@ def test_recursively_expire_parent_caches
fetched_name = Item.fetch(item.id).fetch_associated_records.first.fetch_deeply_associated_records.first.name
assert_equal("updated child", fetched_name)
end

def test_custom_parent_foreign_key_expiry
define_cache_indexes = lambda do
CustomParentRecord.cache_has_many(:custom_child_records, embed: true)
CustomChildRecord.cache_belongs_to(:custom_parent_record)
end
define_cache_indexes.call
old_parent = CustomParentRecord.new(parent_primary_key: 1)
old_parent.save!
child = CustomChildRecord.new(child_primary_key: 10, parent_id: old_parent.id)
child.save!

# Warm the custom_child_records embedded cache on the old parent record
assert_equal 10, CustomParentRecord.fetch(1).fetch_custom_child_records.first.child_primary_key

new_parent = CustomParentRecord.new(parent_primary_key: 2)
new_parent.save!
# Warm the custom_child_records embedded cache on the new parent record
assert_empty CustomParentRecord.fetch(2).fetch_custom_child_records

# Now invoke a db update, where the child switches parent
child.parent_id = new_parent.parent_primary_key
child.save!

# the old parent's custom_child_records embedded cache should be invalidated and empty
assert_empty CustomParentRecord.fetch(1).fetch_custom_child_records
# the new parent's custom_child_records embedded cache should be invalidated and filled with the new association
assert_equal 10, CustomParentRecord.fetch(2).fetch_custom_child_records.first.child_primary_key
end
end

0 comments on commit ecbeadf

Please sign in to comment.