identity_cache 1.5.2 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25effde8c6f5f470a9e90f18a17b89ed2c12f4d3191e0c05110568acdcb2153c
4
- data.tar.gz: ff299911448670f58000ca5f047d4a9bbd04fe00452392eb698232a40d244e74
3
+ metadata.gz: 8a7b8ac895d1bdef7695e310042d3699d5d9b279c12d538eb3a577b51cc27acd
4
+ data.tar.gz: a423c92964c108f8038202ac4ef996697023b39c2db445635d4365a7d81e19fb
5
5
  SHA512:
6
- metadata.gz: 1b861f668ca23a245b16c6ac4d3a8a2b80d954386f67607bca8ce51507d7c3fcb8852d419e38e4c15f5999c8614fb7a5e2661d78687ea510b73f83d107a58c9e
7
- data.tar.gz: dd2a1b9fa974aa139eb99b6e2bc718ee7725b15f54998a125bea2b552b4dfe7de537d9c216516ffb0419daecb40c762debff1c17fcbf973eb42b0344af7bb2ad
6
+ metadata.gz: 65487956cfd97f17abcf90bf6055283fe0b3840a5f01a82d69e570638c4f8cd10a75cda05a9b80c1eb0f6013d8f41a3f5b94a451417cd16c57e16b7ef4fa6081
7
+ data.tar.gz: aa8ec598d2854332aa7054be7c4af26ec7c85271f15cd26b9967a74493e5ec7f91e636369ff0f144699f0ab9f1c719477713c5f069b7379f224c1e67d0c7c561
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.5.4
6
+
7
+ - Make `prefetch_associations` work as expected on associations that have been partially prefetched
8
+
9
+ ## 1.5.3
10
+
11
+ - No longer call `should_use_cache?` on embedded/prefetched associations when reading values
12
+
5
13
  ## 1.5.2
6
14
 
7
15
  - Add missing `should_use_cache?` to `fetch_multi` methods.
@@ -14,7 +14,7 @@ module IdentityCache
14
14
  ensure_base_model
15
15
 
16
16
  unless (reflection = reflect_on_association(association))
17
- raise AssociationError, "Association named '#{association}' was not found on #{self.class}"
17
+ raise AssociationError, "Association named '#{association}' was not found on #{self}"
18
18
  end
19
19
 
20
20
  if reflection.scope
@@ -9,7 +9,7 @@ module IdentityCache
9
9
  reflection.active_record.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
10
10
  def #{cached_accessor_name}
11
11
  association_klass = association(:#{name}).klass
12
- if association_klass.should_use_cache? && #{reflection.foreign_key}.present? && !association(:#{name}).loaded?
12
+ if (loaded_by_idc? || association_klass.should_use_cache?) && #{reflection.foreign_key}.present? && !association(:#{name}).loaded?
13
13
  if defined?(#{records_variable_name})
14
14
  #{records_variable_name}
15
15
  else
@@ -77,16 +77,20 @@ module IdentityCache
77
77
  end
78
78
  end
79
79
 
80
- load_strategy.load_multi(
81
- reflection.klass.cached_primary_index,
82
- ids_to_owner_record.keys
83
- ) do |associated_records_by_id|
84
- associated_records_by_id.each do |id, associated_record|
85
- owner_record = ids_to_owner_record.fetch(id)
86
- write(owner_record, associated_record)
87
- end
80
+ if ids_to_owner_record.any?
81
+ load_strategy.load_multi(
82
+ reflection.klass.cached_primary_index,
83
+ ids_to_owner_record.keys
84
+ ) do |associated_records_by_id|
85
+ associated_records_by_id.each do |id, associated_record|
86
+ owner_record = ids_to_owner_record.fetch(id)
87
+ write(owner_record, associated_record)
88
+ end
88
89
 
89
- yield associated_records_by_id.values.compact
90
+ yield associated_records_by_id.values.compact
91
+ end
92
+ else
93
+ yield records.filter_map { |record| record.instance_variable_get(records_variable_name) }
90
94
  end
91
95
  end
92
96
  end
@@ -54,7 +54,10 @@ module IdentityCache
54
54
 
55
55
  def load_one_from_db(id)
56
56
  record = build_query(id).take
57
- model.send(:setup_embedded_associations_on_miss, [record]) if record
57
+ if record
58
+ model.send(:setup_embedded_associations_on_miss, [record])
59
+ record.send(:mark_as_loaded_by_idc)
60
+ end
58
61
  record
59
62
  end
60
63
 
@@ -63,6 +66,7 @@ module IdentityCache
63
66
 
64
67
  records = build_query(ids).to_a
65
68
  model.send(:setup_embedded_associations_on_miss, records)
69
+ records.each { |record| record.send(:mark_as_loaded_by_idc) }
66
70
  records.index_by(&:id)
67
71
  end
68
72
 
@@ -25,7 +25,7 @@ module IdentityCache
25
25
  def read(record)
26
26
  assoc = record.association(name)
27
27
 
28
- if assoc.klass.should_use_cache? && !assoc.loaded? && assoc.target.blank?
28
+ if (record.send(:loaded_by_idc?) || assoc.klass.should_use_cache?) && !assoc.loaded? && assoc.target.blank?
29
29
  if record.instance_variable_defined?(records_variable_name)
30
30
  record.instance_variable_get(records_variable_name)
31
31
  elsif record.instance_variable_defined?(dehydrated_variable_name)
@@ -22,7 +22,7 @@ module IdentityCache
22
22
 
23
23
  def #{cached_accessor_name}
24
24
  assoc = association(:#{name})
25
- if assoc.klass.should_use_cache? && !assoc.loaded? && assoc.target.blank?
25
+ if (loaded_by_idc? || assoc.klass.should_use_cache?) && !assoc.loaded? && assoc.target.blank?
26
26
  #{records_variable_name} ||= #{reflection.class_name}.fetch_multi(#{cached_ids_name})
27
27
  else
28
28
  #{name}.to_a
@@ -143,7 +143,7 @@ module IdentityCache
143
143
 
144
144
  def check_association_for_caching(association)
145
145
  unless (association_reflection = reflect_on_association(association))
146
- raise AssociationError, "Association named '#{association}' was not found on #{self.class}"
146
+ raise AssociationError, "Association named '#{association}' was not found on #{self}"
147
147
  end
148
148
  if association_reflection.options[:through]
149
149
  raise UnsupportedAssociationError, "caching through associations isn't supported"
@@ -69,6 +69,7 @@ module IdentityCache
69
69
 
70
70
  def record_from_coder(coder, klass) # :nodoc:
71
71
  record = klass.instantiate(coder[:attributes].dup)
72
+ record.send(:mark_as_loaded_by_idc)
72
73
 
73
74
  if coder.key?(:associations)
74
75
  coder[:associations].each do |name, value|
@@ -9,5 +9,15 @@ module IdentityCache
9
9
  IdentityCache.should_use_cache?
10
10
  end
11
11
  end
12
+
13
+ private
14
+
15
+ def mark_as_loaded_by_idc
16
+ @loaded_by_idc = true
17
+ end
18
+
19
+ def loaded_by_idc?
20
+ defined?(@loaded_by_idc)
21
+ end
12
22
  end
13
23
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IdentityCache
4
- VERSION = "1.5.2"
4
+ VERSION = "1.5.4"
5
5
  CACHE_VERSION = 8
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identity_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camilo Lopez
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2023-12-15 00:00:00.000000000 Z
17
+ date: 2024-02-20 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activerecord
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
190
  - !ruby/object:Gem::Version
191
191
  version: '0'
192
192
  requirements: []
193
- rubygems_version: 3.4.22
193
+ rubygems_version: 3.5.6
194
194
  signing_key:
195
195
  specification_version: 4
196
196
  summary: IdentityCache lets you specify how you want to cache your model objects,