predictive_load 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/predictive_load/active_record_collection_observation.rb +1 -1
- data/lib/predictive_load/loader.rb +9 -2
- data/lib/predictive_load/watcher.rb +3 -1
- data/predictive_load.gemspec +1 -1
- data/test/loader_test.rb +8 -0
- data/test/models.rb +11 -0
- data/test/watcher_test.rb +20 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9765b8681e6f08d2913adc2e00dd8543221e4104
|
4
|
+
data.tar.gz: 7c9efbb5370c71cf3209a534e8694ac698f6e282
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36c715e1fc8d8e241e766fdf1fb2df3ca78505b10ca717cdb87f6164020a2527e40ba422657fc53b4dd3322f870b8ffe45879d099c13a7cb6aa9aab36240e034
|
7
|
+
data.tar.gz: c48502690d43a1a2b93d555fbbbb95bceac4e54d4c2048a570faf98c7bead3b5fb62995fedf624e02af1f9e00c7b566b3e95277a2828ef63cf45753b80461103
|
@@ -48,7 +48,7 @@ module PredictiveLoad::ActiveRecordCollectionObservation
|
|
48
48
|
|
49
49
|
def notify_collection_observer
|
50
50
|
if @owner.collection_observer
|
51
|
-
@owner.collection_observer.loading_association(@owner,
|
51
|
+
@owner.collection_observer.loading_association(@owner, self)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -20,8 +20,10 @@ module PredictiveLoad
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def loading_association(record,
|
24
|
-
|
23
|
+
def loading_association(record, association)
|
24
|
+
association_name = association.reflection.name
|
25
|
+
|
26
|
+
if all_records_will_likely_load_association?(association_name) && supports_preload?(association)
|
25
27
|
preload(association_name)
|
26
28
|
end
|
27
29
|
end
|
@@ -34,6 +36,11 @@ module PredictiveLoad
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
39
|
+
def supports_preload?(association)
|
40
|
+
return false if association.reflection.options[:conditions].respond_to?(:to_proc)
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
37
44
|
protected
|
38
45
|
|
39
46
|
attr_reader :records
|
@@ -27,8 +27,10 @@ module PredictiveLoad
|
|
27
27
|
@loaded_associations = {}
|
28
28
|
end
|
29
29
|
|
30
|
-
def loading_association(record,
|
30
|
+
def loading_association(record, association)
|
31
|
+
association_name = association.reflection.name
|
31
32
|
return if !all_records_will_likely_load_association?(association_name)
|
33
|
+
return if !supports_preload?(association)
|
32
34
|
|
33
35
|
if loaded_associations.key?(association_name)
|
34
36
|
log_query_plan(association_name)
|
data/predictive_load.gemspec
CHANGED
data/test/loader_test.rb
CHANGED
@@ -44,6 +44,14 @@ describe PredictiveLoad::Loader do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
it "does not attempt to preload associations with proc conditions" do
|
48
|
+
comments = Comment.all
|
49
|
+
assert_equal 2, comments.size
|
50
|
+
assert_queries(2) do
|
51
|
+
comments.each { |comment| assert comment.user_by_proc.full_name }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
47
55
|
end
|
48
56
|
|
49
57
|
describe "has_one" do
|
data/test/models.rb
CHANGED
@@ -3,6 +3,11 @@ class User < ActiveRecord::Base
|
|
3
3
|
has_many :topics, :through => :comments
|
4
4
|
has_one :photo
|
5
5
|
has_and_belongs_to_many :emails
|
6
|
+
|
7
|
+
def full_name
|
8
|
+
name
|
9
|
+
end
|
10
|
+
|
6
11
|
end
|
7
12
|
|
8
13
|
class Email < ActiveRecord::Base
|
@@ -14,10 +19,16 @@ end
|
|
14
19
|
|
15
20
|
class Comment < ActiveRecord::Base
|
16
21
|
belongs_to :user
|
22
|
+
belongs_to :user_by_proc, :class_name => "User", :foreign_key => :user_id,
|
23
|
+
:conditions => proc { "1 = #{one}" }
|
17
24
|
belongs_to :topic
|
18
25
|
|
19
26
|
scope :by_topic, lambda { |topic| where(:topic_id => topic.id) }
|
20
27
|
scope :recent, order('updated_at desc')
|
28
|
+
|
29
|
+
def one
|
30
|
+
1
|
31
|
+
end
|
21
32
|
end
|
22
33
|
|
23
34
|
class Photo < ActiveRecord::Base
|
data/test/watcher_test.rb
CHANGED
@@ -57,14 +57,24 @@ predictive_load: would have prevented all 1 queries
|
|
57
57
|
|
58
58
|
end
|
59
59
|
|
60
|
+
it "does not blow up when preloading associations with proc conditions" do
|
61
|
+
log = StringIO.new
|
62
|
+
logger = build_logger(log)
|
63
|
+
ActiveRecord::Base.logger = logger
|
64
|
+
|
65
|
+
comments = Comment.all
|
66
|
+
assert_equal 2, comments.size
|
67
|
+
assert_queries(2) do
|
68
|
+
comments.each { |comment| assert comment.user_by_proc.full_name }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
60
72
|
end
|
61
73
|
|
62
74
|
def assert_log(message, gsub_pattern)
|
63
75
|
original_logger = ActiveRecord::Base.logger
|
64
76
|
log = StringIO.new
|
65
|
-
logger =
|
66
|
-
logger.level = Logger::Severity::INFO
|
67
|
-
logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
|
77
|
+
logger = build_logger(log)
|
68
78
|
ActiveSupport::LogSubscriber.colorize_logging = false
|
69
79
|
ActiveRecord::Base.logger = logger
|
70
80
|
|
@@ -76,4 +86,11 @@ predictive_load: would have prevented all 1 queries
|
|
76
86
|
ActiveRecord::Base.logger = original_logger
|
77
87
|
end
|
78
88
|
|
89
|
+
def build_logger(log)
|
90
|
+
Logger.new(log).tap do |logger|
|
91
|
+
logger.level = Logger::Severity::INFO
|
92
|
+
logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
79
96
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: predictive_load
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Chapweske
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Predictive loader
|
14
14
|
email:
|