goldiloader 5.1.0 → 5.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/goldiloader/active_record_patches.rb +14 -1
- data/lib/goldiloader/custom_preloads.rb +13 -2
- data/lib/goldiloader/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ef809faf0ae768be18614302b3095863c0dce6ad5bdead11429a8968f39fef8
|
4
|
+
data.tar.gz: 7f4343a5a6065412c374c86c101dae7ed794b48bdfa8b5ff1765c7f9e2445c5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4747e4da90b4bb33bf8533d6101f0906e069085cc6987d178204f69afe23a55d5f81c387ee32757e6ba4d12479744a2bd121b8e0306a1774ba92f6451535fc2c
|
7
|
+
data.tar.gz: beaffcdef50baad23534537413a6217ad815d4cec10eb196713a27a83d59b68ed0a2a09b65aa5452fc76018f0e19c5b9136c4e260597655f72e0bc917ee519a0
|
@@ -177,7 +177,20 @@ module Goldiloader
|
|
177
177
|
def auto_include?
|
178
178
|
# Only auto include through associations if the target association is auto-loadable
|
179
179
|
through_association = owner.association(through_reflection.name)
|
180
|
-
|
180
|
+
auto_include_self = super
|
181
|
+
|
182
|
+
# If the current association cannot be auto-included there is nothing we can do
|
183
|
+
return false unless auto_include_self
|
184
|
+
|
185
|
+
# If the through association can just be auto-included we're good
|
186
|
+
return true if through_association.auto_include?
|
187
|
+
|
188
|
+
# If the through association was already loaded and does not contain new, changed, or destroyed records
|
189
|
+
# we are also able to auto-include the association. It means it has only already been read or changes are
|
190
|
+
# already persisted.
|
191
|
+
through_association.loaded? && Array.wrap(through_association.target).none? do |record|
|
192
|
+
record.new_record? || record.changed? || record.destroyed?
|
193
|
+
end
|
181
194
|
end
|
182
195
|
end
|
183
196
|
::ActiveRecord::Associations::HasManyThroughAssociation.prepend(::Goldiloader::ThroughAssociationPatch)
|
@@ -10,7 +10,7 @@ module Goldiloader
|
|
10
10
|
def preloaded(model, cache_name:, key:, &block)
|
11
11
|
unless preloaded?(cache_name)
|
12
12
|
ids = models.map do |record|
|
13
|
-
record
|
13
|
+
key_from_record(record, key)
|
14
14
|
end
|
15
15
|
|
16
16
|
# We're using instance_exec instead of a simple yield to make sure that the
|
@@ -26,13 +26,24 @@ module Goldiloader
|
|
26
26
|
|
27
27
|
private
|
28
28
|
|
29
|
+
def key_from_record(record, key_or_key_list)
|
30
|
+
if key_or_key_list.is_a?(Array)
|
31
|
+
# allow passing an array of keys that will be collected from the record
|
32
|
+
key_or_key_list.map do |key|
|
33
|
+
record.public_send(key)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
record.public_send(key_or_key_list)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
29
40
|
def store_preloaded(cache_name, preloaded_hash)
|
30
41
|
@custom_preloads ||= {}
|
31
42
|
@custom_preloads[cache_name] = preloaded_hash
|
32
43
|
end
|
33
44
|
|
34
45
|
def fetch_preloaded(cache_name, instance, key:)
|
35
|
-
@custom_preloads&.dig(cache_name, instance
|
46
|
+
@custom_preloads&.dig(cache_name, key_from_record(instance, key))
|
36
47
|
end
|
37
48
|
|
38
49
|
def preloaded?(cache_name)
|
data/lib/goldiloader/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: goldiloader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.1
|
4
|
+
version: 5.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Turkel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|