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 +4 -4
- data/lib/ar_lazy_preload/contexts/base_context.rb +23 -4
- data/lib/ar_lazy_preload/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: 780b04da1bdc2de0822bcf78d9ac51d7e24f5bf66aee70d42e81a0a0cb662a21
|
4
|
+
data.tar.gz: bceb49a3525fcfcac85d8662485992bc81e37015a9100a4332f3aaae1c0bd59e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
83
|
-
|
84
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2021-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|