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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb9fa507002032502359805bb5a3e97730abba37
4
- data.tar.gz: 40ad5bd0b8d8aa0b65bdd4d81bd20617a901744f
3
+ metadata.gz: c588660d2fac16be90c9c518cf38777a5a88908e
4
+ data.tar.gz: a5019c17bd022a04f8a805637ca2d3a9583dacd1
5
5
  SHA512:
6
- metadata.gz: 785eaf1a3a9a1ad2f7c79ad0603ae92e0366cde058ffe8941ab5e81093df731562f6c90b8724aa5f0613949058c7aa0bbf414992ea8b4d597524fee79b024d70
7
- data.tar.gz: 3ac672859b89465d53fe8086dad7d82fe3519869452537ac7ab551b7c7216111a1b336c4f746aebb2c9af6e720d01b3e40a680e3b1a575406cf313ca38412d44
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 `no_preload`
37
+ Some things cannot be preloaded, use `predictive_load: false`
38
38
 
39
39
  ```
40
- has_many :foos, no_preload: true
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 association.reflection.options[:no_preload]
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
@@ -1,4 +1,4 @@
1
1
  module PredictiveLoad
2
2
  end
3
3
 
4
- ActiveRecord::Associations::Builder::Association.valid_options << :no_preload
4
+ ActiveRecord::Associations::Builder::Association.valid_options << :predictive_load
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: predictive_load
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Chapweske