bullet 4.7.1 → 4.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +17 -17
- data/CHANGELOG.md +15 -0
- data/Gemfile +9 -2
- data/Gemfile.mongoid-2.4 +22 -0
- data/Gemfile.mongoid-2.5 +22 -0
- data/Gemfile.mongoid-2.6 +22 -0
- data/Gemfile.mongoid-2.7 +22 -0
- data/Gemfile.mongoid-2.8 +22 -0
- data/Gemfile.mongoid-3.0 +22 -0
- data/Gemfile.mongoid-3.1 +22 -0
- data/Gemfile.rails-3.0 +21 -0
- data/Gemfile.rails-3.1 +21 -0
- data/Gemfile.rails-3.2 +21 -0
- data/Gemfile.rails-4.0 +21 -0
- data/{Gemfile.rails-3.0.20 → Gemfile.rails-4.1} +1 -1
- data/README.md +24 -23
- data/Rakefile +13 -6
- data/lib/bullet.rb +1 -1
- data/lib/bullet/active_record41.rb +97 -0
- data/lib/bullet/dependency.rb +11 -1
- data/lib/bullet/mongoid4x.rb +1 -1
- data/lib/bullet/registry/base.rb +1 -1
- data/lib/bullet/version.rb +1 -1
- data/spec/bullet/detector/association_spec.rb +8 -8
- data/spec/bullet/detector/base_spec.rb +1 -1
- data/spec/bullet/detector/counter_cache_spec.rb +12 -12
- data/spec/bullet/detector/n_plus_one_query_spec.rb +32 -32
- data/spec/bullet/detector/unused_eager_loading_spec.rb +18 -18
- data/spec/bullet/ext/object_spec.rb +2 -2
- data/spec/bullet/ext/string_spec.rb +2 -2
- data/spec/bullet/notification/base_spec.rb +19 -13
- data/spec/bullet/notification/counter_cache_spec.rb +2 -2
- data/spec/bullet/notification/n_plus_one_query_spec.rb +3 -3
- data/spec/bullet/notification/unused_eager_loading_spec.rb +2 -2
- data/spec/bullet/notification_collector_spec.rb +4 -4
- data/spec/bullet/rack_spec.rb +21 -21
- data/spec/bullet/registry/association_spec.rb +3 -3
- data/spec/bullet/registry/base_spec.rb +6 -6
- data/spec/bullet/registry/object_spec.rb +2 -2
- data/spec/integration/active_record3/association_spec.rb +107 -107
- data/spec/integration/active_record4/association_spec.rb +105 -105
- data/spec/integration/counter_cache_spec.rb +4 -4
- data/spec/integration/mongoid/association_spec.rb +52 -52
- data/spec/spec_helper.rb +0 -1
- data/test.sh +12 -11
- metadata +25 -38
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/Gemfile.lock +0 -160
- data/Gemfile.mongoid-2.4.12 +0 -15
- data/Gemfile.mongoid-2.4.12.lock +0 -163
- data/Gemfile.mongoid-2.5.2 +0 -15
- data/Gemfile.mongoid-2.5.2.lock +0 -163
- data/Gemfile.mongoid-2.6.0 +0 -15
- data/Gemfile.mongoid-2.6.0.lock +0 -163
- data/Gemfile.mongoid-2.7.1 +0 -15
- data/Gemfile.mongoid-2.7.1.lock +0 -163
- data/Gemfile.mongoid-2.8.1 +0 -15
- data/Gemfile.mongoid-2.8.1.lock +0 -166
- data/Gemfile.mongoid-3.0.23 +0 -15
- data/Gemfile.mongoid-3.0.23.lock +0 -163
- data/Gemfile.mongoid-3.1.5 +0 -15
- data/Gemfile.mongoid-3.1.5.lock +0 -163
- data/Gemfile.mongoid.lock +0 -167
- data/Gemfile.rails-3.0.20.lock +0 -147
- data/Gemfile.rails-3.1.12 +0 -14
- data/Gemfile.rails-3.1.12.lock +0 -157
- data/Gemfile.rails-3.2.15 +0 -14
- data/Gemfile.rails-3.2.15.lock +0 -155
- data/Gemfile.rails-4.0.1 +0 -14
- data/Gemfile.rails-4.0.1.lock +0 -150
data/Rakefile
CHANGED
@@ -3,7 +3,6 @@ require "bundler"
|
|
3
3
|
Bundler.setup
|
4
4
|
|
5
5
|
require "rake"
|
6
|
-
require "rdoc/task"
|
7
6
|
require "rspec"
|
8
7
|
require "rspec/core/rake_task"
|
9
8
|
|
@@ -35,11 +34,19 @@ RSpec::Core::RakeTask.new('spec:progress') do |spec|
|
|
35
34
|
spec.pattern = "spec/**/*_spec.rb"
|
36
35
|
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
rdoc
|
41
|
-
|
42
|
-
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'rdoc/task'
|
40
|
+
|
41
|
+
desc "Generate documentation for the plugin."
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
rdoc.rdoc_dir = "rdoc"
|
44
|
+
rdoc.title = "bullet #{Bullet::VERSION}"
|
45
|
+
rdoc.rdoc_files.include("README*")
|
46
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
47
|
+
end
|
48
|
+
rescue LoadError
|
49
|
+
puts 'RDocTask is not supported for this platform'
|
43
50
|
end
|
44
51
|
|
45
52
|
task :default => :spec
|
data/lib/bullet.rb
CHANGED
@@ -66,7 +66,7 @@ module Bullet
|
|
66
66
|
|
67
67
|
def add_whitelist(options)
|
68
68
|
@whitelist[options[:type]][options[:class_name].classify] ||= []
|
69
|
-
@whitelist[options[:type]][options[:class_name].classify] << options[:association]
|
69
|
+
@whitelist[options[:type]][options[:class_name].classify] << options[:association]
|
70
70
|
end
|
71
71
|
|
72
72
|
def get_whitelist_associations(type, class_name)
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Bullet
|
2
|
+
module ActiveRecord
|
3
|
+
def self.enable
|
4
|
+
require 'active_record'
|
5
|
+
::ActiveRecord::Relation.class_eval do
|
6
|
+
alias_method :origin_to_a, :to_a
|
7
|
+
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
8
|
+
# if select only one object, then the only one object has impossible to cause N+1 query.
|
9
|
+
def to_a
|
10
|
+
records = origin_to_a
|
11
|
+
if records.first.class.name !~ /^HABTM_/
|
12
|
+
if records.size > 1
|
13
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
14
|
+
Bullet::Detector::CounterCache.add_possible_objects(records)
|
15
|
+
elsif records.size == 1
|
16
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
17
|
+
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
records
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
::ActiveRecord::Associations::Preloader.class_eval do
|
25
|
+
alias_method :origin_preloaders_on, :preloaders_on
|
26
|
+
|
27
|
+
def preloaders_on(association, records, scope)
|
28
|
+
if records.first.class.name !~ /^HABTM_/
|
29
|
+
records.each do |record|
|
30
|
+
Bullet::Detector::Association.add_object_associations(record, association)
|
31
|
+
end
|
32
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
|
33
|
+
end
|
34
|
+
origin_preloaders_on(association, records, scope)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
::ActiveRecord::FinderMethods.class_eval do
|
39
|
+
# add includes in scope
|
40
|
+
alias_method :origin_find_with_associations, :find_with_associations
|
41
|
+
def find_with_associations
|
42
|
+
records = origin_find_with_associations
|
43
|
+
associations = (eager_load_values + includes_values).uniq
|
44
|
+
records.each do |record|
|
45
|
+
Bullet::Detector::Association.add_object_associations(record, associations)
|
46
|
+
Bullet::Detector::NPlusOneQuery.call_association(record, associations)
|
47
|
+
end
|
48
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
|
49
|
+
records
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
::ActiveRecord::Associations::JoinDependency.class_eval do
|
54
|
+
alias_method :origin_construct_model, :construct_model
|
55
|
+
# call join associations
|
56
|
+
def construct_model(record, node, row, model_cache, id, aliases)
|
57
|
+
associations = node.reflection.name
|
58
|
+
Bullet::Detector::Association.add_object_associations(record, associations)
|
59
|
+
Bullet::Detector::NPlusOneQuery.call_association(record, associations)
|
60
|
+
origin_construct_model(record, node, row, model_cache, id, aliases)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
::ActiveRecord::Associations::CollectionAssociation.class_eval do
|
65
|
+
# call one to many associations
|
66
|
+
alias_method :origin_load_target, :load_target
|
67
|
+
def load_target
|
68
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
|
69
|
+
origin_load_target
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
::ActiveRecord::Associations::SingularAssociation.class_eval do
|
74
|
+
# call has_one and belongs_to associations
|
75
|
+
alias_method :origin_reader, :reader
|
76
|
+
def reader(force_reload = false)
|
77
|
+
result = origin_reader(force_reload)
|
78
|
+
if @owner.class.name !~ /^HABTM_/
|
79
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
|
80
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
81
|
+
end
|
82
|
+
result
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
::ActiveRecord::Associations::HasManyAssociation.class_eval do
|
87
|
+
alias_method :origin_has_cached_counter?, :has_cached_counter?
|
88
|
+
|
89
|
+
def has_cached_counter?(reflection = reflection)
|
90
|
+
result = origin_has_cached_counter?(reflection)
|
91
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name) unless result
|
92
|
+
result
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/lib/bullet/dependency.rb
CHANGED
@@ -18,8 +18,10 @@ module Bullet
|
|
18
18
|
'active_record3'
|
19
19
|
elsif active_record31? || active_record32?
|
20
20
|
'active_record3x'
|
21
|
-
elsif
|
21
|
+
elsif active_record40?
|
22
22
|
'active_record4'
|
23
|
+
elsif active_record41?
|
24
|
+
'active_record41'
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -56,6 +58,14 @@ module Bullet
|
|
56
58
|
active_record3? && ::ActiveRecord::VERSION::MINOR == 2
|
57
59
|
end
|
58
60
|
|
61
|
+
def active_record40?
|
62
|
+
active_record4? && ::ActiveRecord::VERSION::MINOR == 0
|
63
|
+
end
|
64
|
+
|
65
|
+
def active_record41?
|
66
|
+
active_record4? && ::ActiveRecord::VERSION::MINOR == 1
|
67
|
+
end
|
68
|
+
|
59
69
|
def mongoid2x?
|
60
70
|
mongoid? && ::Mongoid::VERSION =~ /\A2\.[4-8]/
|
61
71
|
end
|
data/lib/bullet/mongoid4x.rb
CHANGED
@@ -44,7 +44,7 @@ module Bullet
|
|
44
44
|
alias_method :origin_set_relation, :set_relation
|
45
45
|
|
46
46
|
def set_relation(name, relation)
|
47
|
-
if relation && relation.
|
47
|
+
if relation && relation.relation_metadata.macro !~ /embed/
|
48
48
|
Bullet::Detector::NPlusOneQuery.call_association(self, name)
|
49
49
|
end
|
50
50
|
origin_set_relation(name, relation)
|
data/lib/bullet/registry/base.rb
CHANGED
data/lib/bullet/version.rb
CHANGED
@@ -12,32 +12,32 @@ module Bullet
|
|
12
12
|
context ".start_request" do
|
13
13
|
it "should set @@checked to false" do
|
14
14
|
Association.start_request
|
15
|
-
Association.class_variable_get(:@@checked).
|
15
|
+
expect(Association.class_variable_get(:@@checked)).to eq false
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
context ".clear" do
|
20
20
|
it "should clear all class variables" do
|
21
21
|
Association.clear
|
22
|
-
Association.class_variable_get(:@@object_associations).
|
23
|
-
Association.class_variable_get(:@@possible_objects).
|
24
|
-
Association.class_variable_get(:@@impossible_objects).
|
25
|
-
Association.class_variable_get(:@@call_object_associations).
|
26
|
-
Association.class_variable_get(:@@eager_loadings).
|
22
|
+
expect(Association.class_variable_get(:@@object_associations)).to be_nil
|
23
|
+
expect(Association.class_variable_get(:@@possible_objects)).to be_nil
|
24
|
+
expect(Association.class_variable_get(:@@impossible_objects)).to be_nil
|
25
|
+
expect(Association.class_variable_get(:@@call_object_associations)).to be_nil
|
26
|
+
expect(Association.class_variable_get(:@@eager_loadings)).to be_nil
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
context ".add_object_association" do
|
31
31
|
it "should add object, associations pair" do
|
32
32
|
Association.add_object_associations(@post1, :associations)
|
33
|
-
Association.send(:object_associations).
|
33
|
+
expect(Association.send(:object_associations)).to be_include(@post1.bullet_ar_key, :associations)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
context ".add_call_object_associations" do
|
38
38
|
it "should add call object, associations pair" do
|
39
39
|
Association.add_call_object_associations(@post1, :associations)
|
40
|
-
Association.send(:call_object_associations).
|
40
|
+
expect(Association.send(:call_object_associations)).to be_include(@post1.bullet_ar_key, :associations)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -11,21 +11,21 @@ module Bullet
|
|
11
11
|
|
12
12
|
context ".clear" do
|
13
13
|
it "should clear all class variables" do
|
14
|
-
CounterCache.class_variable_get(:@@possible_objects).
|
15
|
-
CounterCache.class_variable_get(:@@impossible_objects).
|
14
|
+
expect(CounterCache.class_variable_get(:@@possible_objects)).to be_nil
|
15
|
+
expect(CounterCache.class_variable_get(:@@impossible_objects)).to be_nil
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
context ".add_counter_cache" do
|
20
20
|
it "should create notification if conditions met" do
|
21
|
-
CounterCache.
|
22
|
-
CounterCache.
|
21
|
+
expect(CounterCache).to receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(true)
|
22
|
+
expect(CounterCache).to receive(:create_notification).with("Post", [:comments])
|
23
23
|
CounterCache.add_counter_cache(@post1, [:comments])
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should not create notification if conditions not met" do
|
27
|
-
CounterCache.
|
28
|
-
CounterCache.
|
27
|
+
expect(CounterCache).to receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(false)
|
28
|
+
expect(CounterCache).to receive(:create_notification).never
|
29
29
|
CounterCache.add_counter_cache(@post1, [:comments])
|
30
30
|
end
|
31
31
|
end
|
@@ -33,30 +33,30 @@ module Bullet
|
|
33
33
|
context ".add_possible_objects" do
|
34
34
|
it "should add possible objects" do
|
35
35
|
CounterCache.add_possible_objects([@post1, @post2])
|
36
|
-
CounterCache.send(:possible_objects).
|
37
|
-
CounterCache.send(:possible_objects).
|
36
|
+
expect(CounterCache.send(:possible_objects)).to be_include(@post1.bullet_ar_key)
|
37
|
+
expect(CounterCache.send(:possible_objects)).to be_include(@post2.bullet_ar_key)
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should add impossible object" do
|
41
41
|
CounterCache.add_impossible_object(@post1)
|
42
|
-
CounterCache.send(:impossible_objects).
|
42
|
+
expect(CounterCache.send(:impossible_objects)).to be_include(@post1.bullet_ar_key)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
context ".conditions_met?" do
|
47
47
|
it "should be true when object is possible, not impossible" do
|
48
48
|
CounterCache.add_possible_objects(@post1)
|
49
|
-
CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations).
|
49
|
+
expect(CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations)).to eq true
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should be false when object is not possible" do
|
53
|
-
CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations).
|
53
|
+
expect(CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations)).to eq false
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should be true when object is possible, and impossible" do
|
57
57
|
CounterCache.add_possible_objects(@post1)
|
58
58
|
CounterCache.add_impossible_object(@post1)
|
59
|
-
CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations).
|
59
|
+
expect(CounterCache.send(:conditions_met?, @post1.bullet_ar_key, :associations)).to eq false
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -12,11 +12,11 @@ module Bullet
|
|
12
12
|
context ".call_association" do
|
13
13
|
it "should set @@checked to true" do
|
14
14
|
NPlusOneQuery.call_association(@post, :associations)
|
15
|
-
NPlusOneQuery.class_variable_get(:@@checked).
|
15
|
+
expect(NPlusOneQuery.class_variable_get(:@@checked)).to eq true
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should add call_object_associations" do
|
19
|
-
NPlusOneQuery.
|
19
|
+
expect(NPlusOneQuery).to receive(:add_call_object_associations).with(@post, :associations)
|
20
20
|
NPlusOneQuery.call_association(@post, :associations)
|
21
21
|
end
|
22
22
|
end
|
@@ -24,71 +24,71 @@ module Bullet
|
|
24
24
|
context ".possible?" do
|
25
25
|
it "should be true if possible_objects contain" do
|
26
26
|
NPlusOneQuery.add_possible_objects(@post)
|
27
|
-
NPlusOneQuery.send(:possible?, @post.bullet_ar_key).
|
27
|
+
expect(NPlusOneQuery.send(:possible?, @post.bullet_ar_key)).to eq true
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
context ".impossible?" do
|
32
32
|
it "should be true if impossible_objects contain" do
|
33
33
|
NPlusOneQuery.add_impossible_object(@post)
|
34
|
-
NPlusOneQuery.send(:impossible?, @post.bullet_ar_key).
|
34
|
+
expect(NPlusOneQuery.send(:impossible?, @post.bullet_ar_key)).to eq true
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
context ".association?" do
|
39
39
|
it "should be true if object, associations pair is already existed" do
|
40
40
|
NPlusOneQuery.add_object_associations(@post, :association)
|
41
|
-
NPlusOneQuery.send(:association?, @post.bullet_ar_key, :association).
|
41
|
+
expect(NPlusOneQuery.send(:association?, @post.bullet_ar_key, :association)).to eq true
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should be false if object, association pair is not existed" do
|
45
45
|
NPlusOneQuery.add_object_associations(@post, :association1)
|
46
|
-
NPlusOneQuery.send(:association?, @post.bullet_ar_key, :associatio2).
|
46
|
+
expect(NPlusOneQuery.send(:association?, @post.bullet_ar_key, :associatio2)).to eq false
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
context ".conditions_met?" do
|
51
51
|
it "should be true if object is possible, not impossible and object, associations pair is not already existed" do
|
52
|
-
NPlusOneQuery.
|
53
|
-
NPlusOneQuery.
|
54
|
-
NPlusOneQuery.
|
55
|
-
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).
|
52
|
+
allow(NPlusOneQuery).to receive(:possible?).with(@post.bullet_ar_key).and_return(true)
|
53
|
+
allow(NPlusOneQuery).to receive(:impossible?).with(@post.bullet_ar_key).and_return(false)
|
54
|
+
allow(NPlusOneQuery).to receive(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
|
55
|
+
expect(NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations)).to eq true
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should be false if object is not possible, not impossible and object, associations pair is not already existed" do
|
59
|
-
NPlusOneQuery.
|
60
|
-
NPlusOneQuery.
|
61
|
-
NPlusOneQuery.
|
62
|
-
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).
|
59
|
+
allow(NPlusOneQuery).to receive(:possible?).with(@post.bullet_ar_key).and_return(false)
|
60
|
+
allow(NPlusOneQuery).to receive(:impossible?).with(@post.bullet_ar_key).and_return(false)
|
61
|
+
allow(NPlusOneQuery).to receive(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
|
62
|
+
expect(NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations)).to eq false
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should be false if object is possible, but impossible and object, associations pair is not already existed" do
|
66
|
-
NPlusOneQuery.
|
67
|
-
NPlusOneQuery.
|
68
|
-
NPlusOneQuery.
|
69
|
-
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).
|
66
|
+
allow(NPlusOneQuery).to receive(:possible?).with(@post.bullet_ar_key).and_return(true)
|
67
|
+
allow(NPlusOneQuery).to receive(:impossible?).with(@post.bullet_ar_key).and_return(true)
|
68
|
+
allow(NPlusOneQuery).to receive(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
|
69
|
+
expect(NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations)).to eq false
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should be false if object is possible, not impossible and object, associations pair is already existed" do
|
73
|
-
NPlusOneQuery.
|
74
|
-
NPlusOneQuery.
|
75
|
-
NPlusOneQuery.
|
76
|
-
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).
|
73
|
+
allow(NPlusOneQuery).to receive(:possible?).with(@post.bullet_ar_key).and_return(true)
|
74
|
+
allow(NPlusOneQuery).to receive(:impossible?).with(@post.bullet_ar_key).and_return(false)
|
75
|
+
allow(NPlusOneQuery).to receive(:association?).with(@post.bullet_ar_key, :associations).and_return(true)
|
76
|
+
expect(NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations)).to eq false
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
80
|
context ".call_association" do
|
81
81
|
it "should create notification if conditions met" do
|
82
|
-
NPlusOneQuery.
|
83
|
-
NPlusOneQuery.
|
84
|
-
NPlusOneQuery.
|
82
|
+
expect(NPlusOneQuery).to receive(:conditions_met?).with(@post.bullet_ar_key, :association).and_return(true)
|
83
|
+
expect(NPlusOneQuery).to receive(:caller_in_project).and_return(["caller"])
|
84
|
+
expect(NPlusOneQuery).to receive(:create_notification).with(["caller"], "Post", :association)
|
85
85
|
NPlusOneQuery.call_association(@post, :association)
|
86
86
|
end
|
87
87
|
|
88
88
|
it "should not create notification if conditions not met" do
|
89
|
-
NPlusOneQuery.
|
90
|
-
NPlusOneQuery.
|
91
|
-
NPlusOneQuery.
|
89
|
+
expect(NPlusOneQuery).to receive(:conditions_met?).with(@post.bullet_ar_key, :association).and_return(false)
|
90
|
+
expect(NPlusOneQuery).not_to receive(:caller_in_project!)
|
91
|
+
expect(NPlusOneQuery).not_to receive(:create_notification).with("Post", :association)
|
92
92
|
NPlusOneQuery.call_association(@post, :association)
|
93
93
|
end
|
94
94
|
end
|
@@ -96,19 +96,19 @@ module Bullet
|
|
96
96
|
context ".add_possible_objects" do
|
97
97
|
it "should add possible objects" do
|
98
98
|
NPlusOneQuery.add_possible_objects([@post, @post2])
|
99
|
-
NPlusOneQuery.send(:possible_objects).
|
100
|
-
NPlusOneQuery.send(:possible_objects).
|
99
|
+
expect(NPlusOneQuery.send(:possible_objects)).to be_include(@post.bullet_ar_key)
|
100
|
+
expect(NPlusOneQuery.send(:possible_objects)).to be_include(@post2.bullet_ar_key)
|
101
101
|
end
|
102
102
|
|
103
103
|
it "should not raise error if object is nil" do
|
104
|
-
|
104
|
+
expect { NPlusOneQuery.add_possible_objects(nil) }.not_to raise_error
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
108
|
context ".add_impossible_object" do
|
109
109
|
it "should add impossible object" do
|
110
110
|
NPlusOneQuery.add_impossible_object(@post)
|
111
|
-
NPlusOneQuery.send(:impossible_objects).
|
111
|
+
expect(NPlusOneQuery.send(:impossible_objects)).to be_include(@post.bullet_ar_key)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
@@ -11,42 +11,42 @@ module Bullet
|
|
11
11
|
|
12
12
|
context ".call_associations" do
|
13
13
|
it "should get empty array if eager_loadings" do
|
14
|
-
UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).
|
14
|
+
expect(UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association]))).to be_empty
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should get call associations if object and association are both in eager_loadings and call_object_associations" do
|
18
18
|
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
19
19
|
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
20
|
-
UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).
|
20
|
+
expect(UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association]))).to eq([:association])
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should not get call associations if not exist in call_object_associations" do
|
24
24
|
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
25
|
-
UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).
|
25
|
+
expect(UnusedEagerLoading.send(:call_associations, @post.bullet_ar_key, Set.new([:association]))).to be_empty
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
context ".diff_object_associations" do
|
30
30
|
it "should return associations not exist in call_association" do
|
31
|
-
UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).
|
31
|
+
expect(UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association]))).to eq([:association])
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should return empty if associations exist in call_association" do
|
35
35
|
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
36
36
|
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
37
|
-
UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).
|
37
|
+
expect(UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association]))).to be_empty
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
context ".check_unused_preload_associations" do
|
42
42
|
it "should set @@checked to true" do
|
43
43
|
UnusedEagerLoading.check_unused_preload_associations
|
44
|
-
UnusedEagerLoading.class_variable_get(:@@checked).
|
44
|
+
expect(UnusedEagerLoading.class_variable_get(:@@checked)).to eq true
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should create notification if object_association_diff is not empty" do
|
48
48
|
UnusedEagerLoading.add_object_associations(@post, :association)
|
49
|
-
UnusedEagerLoading.
|
49
|
+
expect(UnusedEagerLoading).to receive(:create_notification).with("Post", [:association])
|
50
50
|
UnusedEagerLoading.check_unused_preload_associations
|
51
51
|
end
|
52
52
|
|
@@ -55,8 +55,8 @@ module Bullet
|
|
55
55
|
UnusedEagerLoading.add_object_associations(@post, :association)
|
56
56
|
UnusedEagerLoading.add_eager_loadings([@post], :association)
|
57
57
|
UnusedEagerLoading.add_call_object_associations(@post, :association)
|
58
|
-
UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).
|
59
|
-
UnusedEagerLoading.
|
58
|
+
expect(UnusedEagerLoading.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association]))).to be_empty
|
59
|
+
expect(UnusedEagerLoading).not_to receive(:create_notification).with("Post", [:association])
|
60
60
|
UnusedEagerLoading.check_unused_preload_associations
|
61
61
|
end
|
62
62
|
end
|
@@ -64,30 +64,30 @@ module Bullet
|
|
64
64
|
context ".add_eager_loadings" do
|
65
65
|
it "should add objects, associations pair when eager_loadings are empty" do
|
66
66
|
UnusedEagerLoading.add_eager_loadings([@post, @post2], :associations)
|
67
|
-
UnusedEagerLoading.send(:eager_loadings).
|
67
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :associations)
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should add objects, associations pair for existing eager_loadings" do
|
71
71
|
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association1)
|
72
72
|
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association2)
|
73
|
-
UnusedEagerLoading.send(:eager_loadings).
|
74
|
-
UnusedEagerLoading.send(:eager_loadings).
|
73
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :association1)
|
74
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should merge objects, associations pair for existing eager_loadings" do
|
78
78
|
UnusedEagerLoading.add_eager_loadings([@post], :association1)
|
79
79
|
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association2)
|
80
|
-
UnusedEagerLoading.send(:eager_loadings).
|
81
|
-
UnusedEagerLoading.send(:eager_loadings).
|
82
|
-
UnusedEagerLoading.send(:eager_loadings).
|
80
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key], :association1)
|
81
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key], :association2)
|
82
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
83
83
|
end
|
84
84
|
|
85
85
|
it "should delete objects, associations pair for existing eager_loadings" do
|
86
86
|
UnusedEagerLoading.add_eager_loadings([@post, @post2], :association1)
|
87
87
|
UnusedEagerLoading.add_eager_loadings([@post], :association2)
|
88
|
-
UnusedEagerLoading.send(:eager_loadings).
|
89
|
-
UnusedEagerLoading.send(:eager_loadings).
|
90
|
-
UnusedEagerLoading.send(:eager_loadings).
|
88
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key], :association1)
|
89
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post.bullet_ar_key], :association2)
|
90
|
+
expect(UnusedEagerLoading.send(:eager_loadings)).to be_include([@post2.bullet_ar_key], :association1)
|
91
91
|
end
|
92
92
|
end
|
93
93
|
end
|