predictive_load 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/predictive_load/active_record_collection_observation.rb +27 -0
- data/lib/predictive_load/loader.rb +2 -1
- data/lib/predictive_load.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c588660d2fac16be90c9c518cf38777a5a88908e
|
4
|
+
data.tar.gz: a5019c17bd022a04f8a805637ca2d3a9583dacd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 415799244ca6a53685d163174d6ee846a5749c68899d4b91571a36e878771882c05269581b1e55a631044916617d53e2222b261a99f1c25c7ec0b103b5833ef7
|
7
|
+
data.tar.gz: 41ba67603699ab634d0d788c1a90aad5df3d18f5a304a11f213b0a657d3c80d3e9ed9edd5ef260617c3ee36f82f0c8df45c65dc812dab4ab291fcb9272bb1297
|
data/README.md
CHANGED
@@ -34,10 +34,10 @@ Produces:
|
|
34
34
|
|
35
35
|
### Disabling preload
|
36
36
|
|
37
|
-
Some things cannot be preloaded, use `
|
37
|
+
Some things cannot be preloaded, use `predictive_load: false`
|
38
38
|
|
39
39
|
```
|
40
|
-
has_many :foos,
|
40
|
+
has_many :foos, predictive_load: false
|
41
41
|
```
|
42
42
|
|
43
43
|
### N+1 detection logging
|
@@ -78,3 +78,4 @@ would have prevented all 10 queries
|
|
78
78
|
|
79
79
|
* Calling association#size will trigger an N+1 on SELECT COUNT(*). Work around by calling #length, loading all records.
|
80
80
|
* Calling first / last will trigger an N+1.
|
81
|
+
* Rails 4: unscoped will disable eager loading to circument a rails bug ... hopefully fixed in rails 5 https://github.com/rails/rails/pull/16531
|
@@ -3,6 +3,7 @@ module PredictiveLoad::ActiveRecordCollectionObservation
|
|
3
3
|
def self.included(base)
|
4
4
|
ActiveRecord::Relation.send(:include, RelationObservation)
|
5
5
|
ActiveRecord::Base.send(:include, CollectionMember)
|
6
|
+
ActiveRecord::Base.send(:extend, UnscopedTracker)
|
6
7
|
ActiveRecord::Associations::Association.send(:include, AssociationNotification)
|
7
8
|
ActiveRecord::Associations::CollectionAssociation.send(:include, CollectionAssociationNotification)
|
8
9
|
end
|
@@ -32,6 +33,32 @@ module PredictiveLoad::ActiveRecordCollectionObservation
|
|
32
33
|
|
33
34
|
end
|
34
35
|
|
36
|
+
# disable eager loading since includes + unscoped is broken on rails 4
|
37
|
+
module UnscopedTracker
|
38
|
+
if ActiveRecord::VERSION::MAJOR == 4
|
39
|
+
def unscoped
|
40
|
+
if block_given?
|
41
|
+
begin
|
42
|
+
old, self.predictive_load_disabled = predictive_load_disabled, true
|
43
|
+
super
|
44
|
+
ensure
|
45
|
+
self.predictive_load_disabled = old
|
46
|
+
end
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def predictive_load_disabled=(value)
|
54
|
+
Thread.current[:predictive_load_disabled] = value
|
55
|
+
end
|
56
|
+
|
57
|
+
def predictive_load_disabled
|
58
|
+
Thread.current[:predictive_load_disabled]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
35
62
|
module AssociationNotification
|
36
63
|
|
37
64
|
def self.included(base)
|
@@ -40,7 +40,8 @@ module PredictiveLoad
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def supports_preload?(association)
|
43
|
-
return false if
|
43
|
+
return false if ActiveRecord::Base.predictive_load_disabled
|
44
|
+
return false if association.reflection.options[:predictive_load] == false
|
44
45
|
return false if association.reflection.options[:conditions].respond_to?(:to_proc) # rails 3 conditions proc (we do not know if it uses instance methods)
|
45
46
|
if ActiveRecord::VERSION::MAJOR > 3
|
46
47
|
return false if association.reflection.scope.try(:arity).to_i > 0 # rails 4+ conditions block, if it uses a passed in object, we assume it is not preloadable
|
data/lib/predictive_load.rb
CHANGED