goldiloader 5.0.0 → 5.1.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b14210a0559e4703e779410d7381cd84f4e25d808107ea4558bff158320ad66
|
4
|
+
data.tar.gz: 74bd3fc10220916fe12e8394731f0e4846ae157bd24922ca434f83800ee56cfc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8c6f697034dab771dec9391aba56c647631f96f942165adae988803f74c59a7dca7515360349d0193f0d66a60f8fc9d19da1288613562de8b13b6f23b329961
|
7
|
+
data.tar.gz: ed6feff242aedac8add87e36d0863f34e3d6dcc00405f0a98dc6cb803b3d75451f228839e2cff251b5d9a58d0fa1b019e55f73d5d2689f957e11a4d517843389
|
@@ -25,6 +25,11 @@ module Goldiloader
|
|
25
25
|
@auto_include_context = nil
|
26
26
|
super
|
27
27
|
end
|
28
|
+
|
29
|
+
def goldiload(cache_name = nil, key: self.class.primary_key, &block)
|
30
|
+
cache_name ||= block.source_location.join(':')
|
31
|
+
auto_include_context.preloaded(self, cache_name: cache_name, key: key, &block)
|
32
|
+
end
|
28
33
|
end
|
29
34
|
::ActiveRecord::Base.include(::Goldiloader::BasePatch)
|
30
35
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Goldiloader
|
4
|
+
module CustomPreloads
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
@custom_preloads = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def preloaded(model, cache_name:, key:, &block)
|
11
|
+
unless preloaded?(cache_name)
|
12
|
+
ids = models.map do |record|
|
13
|
+
record.public_send(key)
|
14
|
+
end
|
15
|
+
|
16
|
+
# We're using instance_exec instead of a simple yield to make sure that the
|
17
|
+
# given block does not have any references to the model instance as this might
|
18
|
+
# lead to unexpected results. The block will be executed in the context of the
|
19
|
+
# class of the model instead.
|
20
|
+
block_context = models.first.class
|
21
|
+
preloaded_hash = block_context.instance_exec(ids, &block)
|
22
|
+
store_preloaded(cache_name, preloaded_hash)
|
23
|
+
end
|
24
|
+
fetch_preloaded(cache_name, model, key: key)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def store_preloaded(cache_name, preloaded_hash)
|
30
|
+
@custom_preloads ||= {}
|
31
|
+
@custom_preloads[cache_name] = preloaded_hash
|
32
|
+
end
|
33
|
+
|
34
|
+
def fetch_preloaded(cache_name, instance, key:)
|
35
|
+
@custom_preloads&.dig(cache_name, instance.public_send(key))
|
36
|
+
end
|
37
|
+
|
38
|
+
def preloaded?(cache_name)
|
39
|
+
@custom_preloads&.key?(cache_name)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/goldiloader/version.rb
CHANGED
data/lib/goldiloader.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.
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Turkel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -238,6 +238,7 @@ files:
|
|
238
238
|
- lib/goldiloader/association_options.rb
|
239
239
|
- lib/goldiloader/auto_include_context.rb
|
240
240
|
- lib/goldiloader/compatibility.rb
|
241
|
+
- lib/goldiloader/custom_preloads.rb
|
241
242
|
- lib/goldiloader/scope_info.rb
|
242
243
|
- lib/goldiloader/version.rb
|
243
244
|
homepage: https://github.com/salsify/goldiloader
|