Skip to content

Commit e2a0cda

Browse files
committed
Adds support for models using compound primary keys
1 parent 24a883f commit e2a0cda

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

lib/identity_cache/cached/attribute.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ def initialize(model, attribute_or_proc, alias_name, key_fields, unique)
1919
end
2020

2121
def attribute
22-
@attribute ||= @attribute_proc.call.to_sym
22+
@attribute ||= begin
23+
res = @attribute_proc.call
24+
if res.is_a?(Array)
25+
res.map(&:to_sym)
26+
else
27+
res.to_sym
28+
end
29+
end
2330
end
2431

2532
def fetch(db_key)
@@ -59,7 +66,11 @@ def cache_key(index_key)
5966
def load_one_from_db(key)
6067
query = model.reorder(nil).where(load_from_db_where_conditions(key))
6168
query = query.limit(1) if unique
62-
results = query.pluck(attribute)
69+
results = if attribute.is_a?(Array)
70+
query.pluck(*attribute)
71+
else
72+
query.pluck(attribute)
73+
end
6374
unique ? results.first : results
6475
end
6576

@@ -138,10 +149,15 @@ def field_types
138149

139150
def cache_key_prefix
140151
@cache_key_prefix ||= begin
152+
attribute_key_prefix = if attribute.is_a?(Array)
153+
attribute.join("/")
154+
else
155+
attribute.to_s
156+
end
141157
unique_indicator = unique ? "" : "s"
142158
"attr#{unique_indicator}" \
143159
":#{model.base_class.name}" \
144-
":#{attribute}" \
160+
":#{attribute_key_prefix}" \
145161
":#{key_fields.join("/")}:"
146162
end
147163
end

test/custom_primary_keys_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@ def setup
1010
@parent_record = CustomParentRecord.create!(parent_primary_key: 1)
1111
@child_record_1 = CustomChildRecord.create!(custom_parent_record: @parent_record, child_primary_key: 1)
1212
@child_record_2 = CustomChildRecord.create!(custom_parent_record: @parent_record, child_primary_key: 2)
13+
14+
CompositePrimaryKeyRecord.cache_has_many(:custom_child_records)
15+
CPKReference.cache_belongs_to(:composite_primary_key_record)
16+
@composite_record = CompositePrimaryKeyRecord.create!(key_part_one: 1, key_part_two: 2)
17+
@cpk_reference = CPKReference.create!(composite_primary_key_record: @composite_record)
1318
end
1419

1520
def test_fetch_parent
1621
assert_nothing_raised do
1722
CustomParentRecord.fetch(@parent_record.parent_primary_key)
1823
end
1924
end
25+
26+
def test_fetch_composite_primary_key_record
27+
assert_nothing_raised do
28+
cpk_record = CompositePrimaryKeyRecord.fetch([@composite_record.key_part_one, @composite_record.key_part_two])
29+
refute_nil cpk_record
30+
end
31+
end
2032
end

test/helpers/models.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,14 @@ class CustomChildRecord < ActiveRecord::Base
9696
belongs_to :custom_parent_record, foreign_key: :parent_id
9797
self.primary_key = "child_primary_key"
9898
end
99+
100+
class CompositePrimaryKeyRecord < ActiveRecord::Base
101+
include IdentityCache
102+
has_many :cpk_references, foreign_key: [:key_part_one, :key_part_two]
103+
self.primary_key = [:key_part_one, :key_part_two]
104+
end
105+
106+
class CPKReference < ActiveRecord::Base
107+
include IdentityCache
108+
belongs_to :composite_primary_key_record, foreign_key: [:key_part_one, :key_part_two]
109+
end

0 commit comments

Comments
 (0)