ar_lazy_preload 0.6.2 → 0.7.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: d3b1c23e853fd6612e6d7f4f063e4cac5577c591a9a3b7be3b04c4a6801d1291
4
- data.tar.gz: 66a30a3b81d996ebbb9324dfedf4f874498da1b202f66dcab17385fc8596bde3
3
+ metadata.gz: 780b04da1bdc2de0822bcf78d9ac51d7e24f5bf66aee70d42e81a0a0cb662a21
4
+ data.tar.gz: bceb49a3525fcfcac85d8662485992bc81e37015a9100a4332f3aaae1c0bd59e
5
5
  SHA512:
6
- metadata.gz: 7fae1e071f6f199d8a881a2bf0a60a4b5d53e1467428d07e39d67d98539a20a9b8b319ac4aac7ba73675367c648db441a7eb24bcf1f252c0becddd7905150d9c
7
- data.tar.gz: 7c5107b91f3671cc4fa16ae38af7a8b54832c31afc10e337c76eb3b4f24440eff87d5202577bbbcef38628f3266af057966a4120097c8cf887524a8c8ce03ba3
6
+ metadata.gz: d32ee8fe923b29451f032ebb050168d6eaec8d5aec0b3136917a8196284198323ed804061bf64c7b36e104b95a43ced12372baa39a79976a1f69ae22a44c190c
7
+ data.tar.gz: d104a956772f064cb255db1f6743edd312da87da0526d2eff669976ce6f8537fb78a49cb302fa818942a4bb2bf1f0f0ae4226053c696e4575981aef774c356a9
@@ -45,7 +45,7 @@ module ArLazyPreload
45
45
 
46
46
  def perform_preloading(association_name)
47
47
  filtered_records = records.select do |record|
48
- reflection_names_cache[record.class].include?(association_name)
48
+ preloadable_record?(association_name, record)
49
49
  end
50
50
 
51
51
  preload_records(association_name, filtered_records)
@@ -79,11 +79,30 @@ module ArLazyPreload
79
79
  @preloader ||= ActiveRecord::Associations::Preloader.new
80
80
  end
81
81
 
82
- def reflection_names_cache
83
- @reflection_names_cache ||= Hash.new do |hash, klass|
84
- hash[klass] = klass.reflect_on_all_associations.map(&:name)
82
+ def preloadable_record?(association_name, record)
83
+ preloadable_reflections_cache.dig(record.class, association_name)
84
+ end
85
+
86
+ def preloadable_reflections_cache
87
+ @preloadable_reflections_cache ||= Hash.new do |hash, klass|
88
+ associations = klass.reflect_on_all_associations
89
+
90
+ hash[klass] = associations.each_with_object({}) do |reflection, cache|
91
+ cache[reflection.name] = preloadable_reflection?(klass, reflection)
92
+ end
85
93
  end
86
94
  end
95
+
96
+ def preloadable_reflection?(klass, reflection)
97
+ scope = reflection.scope
98
+ preloadable_scope = scope&.arity&.zero?
99
+ through_reflection =
100
+ reflection.options[:through] && klass.reflect_on_association(reflection.options[:through])
101
+ preloadable_through_reflection =
102
+ through_reflection && preloadable_reflection?(klass, through_reflection)
103
+
104
+ (!scope || preloadable_scope) && (!through_reflection || preloadable_through_reflection)
105
+ end
87
106
  end
88
107
  end
89
108
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArLazyPreload
4
- VERSION = "0.6.2"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_lazy_preload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-18 00:00:00.000000000 Z
11
+ date: 2021-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails