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: e5acb78ebf278d88e47a50d7f3c6c49f55bee48704eb115082b45d1421f84313
4
- data.tar.gz: 4034b092560abca7b531f81485c850f366e79bb5f4996c0477dd1525921a2ef8
3
+ metadata.gz: 4b14210a0559e4703e779410d7381cd84f4e25d808107ea4558bff158320ad66
4
+ data.tar.gz: 74bd3fc10220916fe12e8394731f0e4846ae157bd24922ca434f83800ee56cfc
5
5
  SHA512:
6
- metadata.gz: 433c75215165ab250d932c5e3307ec78777f45cb9a6c691dcb33386566ce211e65771808e81c95ab0d46fd0a786e41899fd418858c466503fe723828ba56858d
7
- data.tar.gz: dc0947c73fd7c01697442dd717a0e43efe35fce6a87ab281d9b51f86ea6d1b192be53f6c25b975294d7896ac3d73f79701ea4ea1c8c0cfb1e01d39c31b6a8fd1
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
 
@@ -51,5 +51,7 @@ module Goldiloader
51
51
  end
52
52
 
53
53
  alias_method :register_model, :register_models
54
+
55
+ prepend Goldiloader::CustomPreloads
54
56
  end
55
57
  end
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Goldiloader
4
- VERSION = '5.0.0'
4
+ VERSION = '5.1.0'
5
5
  end
data/lib/goldiloader.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'active_support/all'
4
4
  require 'active_record'
5
5
  require 'goldiloader/compatibility'
6
+ require 'goldiloader/custom_preloads'
6
7
  require 'goldiloader/auto_include_context'
7
8
  require 'goldiloader/scope_info'
8
9
  require 'goldiloader/association_options'
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.0.0
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-09-18 00:00:00.000000000 Z
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