bullet 2.3.0 → 4.0.0
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.
- data/.travis.yml +5 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +44 -33
- data/Gemfile.rails-2.3.14 +16 -0
- data/Gemfile.rails-2.3.14.lock +65 -0
- data/Gemfile.rails-3.0.12 +17 -0
- data/Gemfile.rails-3.0.12.lock +116 -0
- data/Gemfile.rails-3.1.4 +18 -0
- data/Gemfile.rails-3.1.4.lock +134 -0
- data/README.textile +6 -6
- data/lib/bullet/active_record2.rb +12 -0
- data/lib/bullet/active_record3.rb +12 -0
- data/lib/bullet/detector/association.rb +11 -12
- data/lib/bullet/detector/counter.rb +6 -6
- data/lib/bullet/detector/n_plus_one_query.rb +9 -9
- data/lib/bullet/detector/unused_eager_association.rb +9 -9
- data/lib/bullet/ext/object.rb +5 -0
- data/lib/bullet/ext/string.rb +5 -0
- data/lib/bullet/mongoid.rb +56 -0
- data/lib/bullet/registry/object.rb +4 -6
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +15 -9
- data/spec/bullet/detector/association_spec.rb +20 -16
- data/spec/bullet/detector/counter_spec.rb +8 -9
- data/spec/bullet/detector/n_plus_one_query_spec.rb +22 -22
- data/spec/bullet/detector/unused_eager_association_spec.rb +11 -11
- data/spec/bullet/ext/object_spec.rb +20 -0
- data/spec/bullet/ext/string_spec.rb +13 -0
- data/spec/bullet/registry/object_spec.rb +4 -4
- data/spec/integration/association_spec.rb +463 -401
- data/spec/integration/counter_spec.rb +27 -25
- data/spec/integration/mongoid/association_spec.rb +276 -0
- data/spec/integration/rails2/association_spec.rb +593 -0
- data/spec/integration/rails2/counter_spec.rb +39 -0
- data/spec/models/mongoid/address.rb +5 -0
- data/spec/models/mongoid/category.rb +6 -0
- data/spec/models/mongoid/comment.rb +5 -0
- data/spec/models/mongoid/company.rb +5 -0
- data/spec/models/mongoid/entry.rb +5 -0
- data/spec/models/mongoid/post.rb +8 -0
- data/spec/models/post.rb +12 -6
- data/spec/spec_helper.rb +32 -8
- data/spec/support/bullet_ext.rb +1 -1
- data/spec/support/mongo_seed.rb +41 -0
- data/spec/support/{seed.rb → sqlite_seed.rb} +1 -22
- data/test.result +2293 -0
- metadata +42 -22
- data/spec/integration/association_for_chris_spec.rb +0 -37
- data/spec/integration/association_for_peschkaj_spec.rb +0 -26
- data/spec/models/contact.rb +0 -3
- data/spec/models/deal.rb +0 -4
- data/spec/models/email.rb +0 -3
- data/spec/models/hotel.rb +0 -4
- data/spec/models/location.rb +0 -3
|
@@ -8,21 +8,21 @@ module Bullet
|
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def add_counter_cache(object, associations)
|
|
11
|
-
if conditions_met?(object.
|
|
11
|
+
if conditions_met?(object.bullet_ar_key, associations)
|
|
12
12
|
create_notification object.class.to_s, associations
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def add_possible_objects(object_or_objects)
|
|
17
17
|
if object_or_objects.is_a? Array
|
|
18
|
-
object_or_objects.each { |object| possible_objects.add object.
|
|
18
|
+
object_or_objects.each { |object| possible_objects.add object.bullet_ar_key }
|
|
19
19
|
else
|
|
20
|
-
possible_objects.add object_or_objects.
|
|
20
|
+
possible_objects.add object_or_objects.bullet_ar_key
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def add_impossible_object(object)
|
|
25
|
-
impossible_objects.add object.
|
|
25
|
+
impossible_objects.add object.bullet_ar_key
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
private
|
|
@@ -39,8 +39,8 @@ module Bullet
|
|
|
39
39
|
@@impossible_objects ||= Bullet::Registry::Object.new
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
def conditions_met?(
|
|
43
|
-
possible_objects.include?(
|
|
42
|
+
def conditions_met?(bullet_ar_key, associations)
|
|
43
|
+
possible_objects.include?(bullet_ar_key) && !impossible_objects.include?(bullet_ar_key)
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
end
|
|
@@ -10,7 +10,7 @@ module Bullet
|
|
|
10
10
|
@@checked = true
|
|
11
11
|
add_call_object_associations(object, associations)
|
|
12
12
|
|
|
13
|
-
if conditions_met?(object.
|
|
13
|
+
if conditions_met?(object.bullet_ar_key, associations)
|
|
14
14
|
create_notification caller_in_project, object.class.to_s, associations
|
|
15
15
|
end
|
|
16
16
|
end
|
|
@@ -22,8 +22,8 @@ module Bullet
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
# decide whether the object.associations is unpreloaded or not.
|
|
25
|
-
def conditions_met?(
|
|
26
|
-
possible?(
|
|
25
|
+
def conditions_met?(bullet_ar_key, associations)
|
|
26
|
+
possible?(bullet_ar_key) && !impossible?(bullet_ar_key) && !association?(bullet_ar_key, associations)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def caller_in_project
|
|
@@ -32,17 +32,17 @@ module Bullet
|
|
|
32
32
|
caller.select { |c| c.include?(rails_root) && !c.include?(vendor_root) }
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
def possible?(
|
|
36
|
-
possible_objects.include?
|
|
35
|
+
def possible?(bullet_ar_key)
|
|
36
|
+
possible_objects.include? bullet_ar_key
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
def impossible?(
|
|
40
|
-
impossible_objects.include?
|
|
39
|
+
def impossible?(bullet_ar_key)
|
|
40
|
+
impossible_objects.include? bullet_ar_key
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
# check if object => associations already exists in object_associations.
|
|
44
|
-
def association?(
|
|
45
|
-
value = object_associations[
|
|
44
|
+
def association?(bullet_ar_key, associations)
|
|
45
|
+
value = object_associations[bullet_ar_key]
|
|
46
46
|
if value
|
|
47
47
|
value.each do |v|
|
|
48
48
|
result = v.is_a?(Hash) ? v.has_key?(associations) : v == associations
|
|
@@ -8,11 +8,11 @@ module Bullet
|
|
|
8
8
|
# if association not in call_object_association, then the object => association - call_object_association is ununsed preload assocations
|
|
9
9
|
def check_unused_preload_associations
|
|
10
10
|
@@checked = true
|
|
11
|
-
object_associations.each do |
|
|
12
|
-
object_association_diff =
|
|
11
|
+
object_associations.each do |bullet_ar_key, associations|
|
|
12
|
+
object_association_diff = diff_object_associations bullet_ar_key, associations
|
|
13
13
|
next if object_association_diff.empty?
|
|
14
14
|
|
|
15
|
-
create_notification
|
|
15
|
+
create_notification bullet_ar_key.bullet_class_name, object_association_diff
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
@@ -22,19 +22,19 @@ module Bullet
|
|
|
22
22
|
Bullet.notification_collector.add(notice)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def call_associations(
|
|
25
|
+
def call_associations(bullet_ar_key, associations)
|
|
26
26
|
all = Set.new
|
|
27
|
-
eager_loadings.similarly_associated(
|
|
28
|
-
coa = call_object_associations[
|
|
27
|
+
eager_loadings.similarly_associated(bullet_ar_key, associations).each do |related_bullet_ar_key|
|
|
28
|
+
coa = call_object_associations[related_bullet_ar_key]
|
|
29
29
|
next if coa.nil?
|
|
30
30
|
all.merge coa
|
|
31
31
|
end
|
|
32
32
|
all.to_a
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
def
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
def diff_object_associations(bullet_ar_key, associations)
|
|
36
|
+
potential_associations = associations - call_associations(bullet_ar_key, associations)
|
|
37
|
+
potential_associations.reject { |a| a.is_a?(Hash) }
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module Bullet
|
|
2
|
+
module Mongoid
|
|
3
|
+
def self.enable
|
|
4
|
+
require 'mongoid'
|
|
5
|
+
::Mongoid::Contexts::Mongo.class_eval do
|
|
6
|
+
alias_method :origin_first, :first
|
|
7
|
+
alias_method :origin_last, :last
|
|
8
|
+
alias_method :origin_iterate, :iterate
|
|
9
|
+
|
|
10
|
+
def first
|
|
11
|
+
result = origin_first
|
|
12
|
+
Bullet::Detector::Association.add_impossible_object(result)
|
|
13
|
+
result
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def last
|
|
17
|
+
result = origin_last
|
|
18
|
+
Bullet::Detector::Association.add_impossible_object(result)
|
|
19
|
+
result
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def iterate(&block)
|
|
23
|
+
records = execute.to_a
|
|
24
|
+
if records.size > 1
|
|
25
|
+
Bullet::Detector::Association.add_possible_objects(records)
|
|
26
|
+
elsif records.size == 1
|
|
27
|
+
Bullet::Detector::Association.add_impossible_object(records.first)
|
|
28
|
+
end
|
|
29
|
+
origin_iterate(&block)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
::Mongoid::Relations::Accessors.class_eval do
|
|
34
|
+
alias_method :origin_set_relation, :set_relation
|
|
35
|
+
|
|
36
|
+
def set_relation(name, relation)
|
|
37
|
+
Bullet::Detector::NPlusOneQuery.call_association(self, name)
|
|
38
|
+
origin_set_relation(name, relation)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
::Mongoid::Contexts::Mongo.class_eval do
|
|
43
|
+
alias_method :origin_eager_load, :eager_load
|
|
44
|
+
|
|
45
|
+
def eager_load(docs)
|
|
46
|
+
associations = criteria.inclusions.map(&:name)
|
|
47
|
+
docs.each do |doc|
|
|
48
|
+
Bullet::Detector::Association.add_object_associations(doc, associations)
|
|
49
|
+
end
|
|
50
|
+
Bullet::Detector::Association.add_eager_loadings(docs, associations)
|
|
51
|
+
origin_eager_load(docs)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
module Bullet
|
|
2
2
|
module Registry
|
|
3
3
|
class Object < Base
|
|
4
|
-
def add(
|
|
5
|
-
|
|
6
|
-
super(klazz, object_ar_key)
|
|
4
|
+
def add(bullet_ar_key)
|
|
5
|
+
super(bullet_ar_key.bullet_class_name, bullet_ar_key)
|
|
7
6
|
end
|
|
8
7
|
|
|
9
|
-
def include?(
|
|
10
|
-
|
|
11
|
-
super(klazz, object_ar_key)
|
|
8
|
+
def include?(bullet_ar_key)
|
|
9
|
+
super(bullet_ar_key.bullet_class_name, bullet_ar_key)
|
|
12
10
|
end
|
|
13
11
|
end
|
|
14
12
|
end
|
data/lib/bullet/version.rb
CHANGED
data/lib/bullet.rb
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
require 'set'
|
|
2
2
|
require 'uniform_notifier'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def ar_key
|
|
6
|
-
"#{self.class}:#{self.id}"
|
|
7
|
-
end
|
|
8
|
-
end
|
|
3
|
+
require 'bullet/ext/object'
|
|
4
|
+
require 'bullet/ext/string'
|
|
9
5
|
|
|
10
6
|
module Bullet
|
|
11
7
|
if Rails.version =~ /\A3\.0/
|
|
@@ -16,6 +12,7 @@ module Bullet
|
|
|
16
12
|
autoload :ActiveRecord, 'bullet/active_record2'
|
|
17
13
|
autoload :ActionController, 'bullet/action_controller2'
|
|
18
14
|
end
|
|
15
|
+
autoload :Mongoid, 'bullet/mongoid'
|
|
19
16
|
autoload :Rack, 'bullet/rack'
|
|
20
17
|
autoload :BulletLogger, 'bullet/logger'
|
|
21
18
|
autoload :Notification, 'bullet/notification'
|
|
@@ -44,9 +41,18 @@ module Bullet
|
|
|
44
41
|
def enable=(enable)
|
|
45
42
|
@enable = enable
|
|
46
43
|
if enable?
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
Bullet::
|
|
44
|
+
begin
|
|
45
|
+
require 'mongoid'
|
|
46
|
+
Bullet::Mongoid.enable
|
|
47
|
+
rescue LoadError
|
|
48
|
+
end
|
|
49
|
+
begin
|
|
50
|
+
require 'active_record'
|
|
51
|
+
Bullet::ActiveRecord.enable
|
|
52
|
+
if Rails.version =~ /\A2./
|
|
53
|
+
Bullet::ActionController.enable
|
|
54
|
+
end
|
|
55
|
+
rescue LoadError
|
|
50
56
|
end
|
|
51
57
|
end
|
|
52
58
|
end
|
|
@@ -30,59 +30,63 @@ module Bullet
|
|
|
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).should be_include(@post1.
|
|
33
|
+
Association.send(:object_associations).should 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).should be_include(@post1.
|
|
40
|
+
Association.send(:call_object_associations).should be_include(@post1.bullet_ar_key, :associations)
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
context ".add_possible_objects" do
|
|
45
45
|
it "should add possible objects" do
|
|
46
46
|
Association.add_possible_objects([@post1, @post2])
|
|
47
|
-
Association.send(:possible_objects).should be_include(@post1.
|
|
48
|
-
Association.send(:possible_objects).should be_include(@post2.
|
|
47
|
+
Association.send(:possible_objects).should be_include(@post1.bullet_ar_key)
|
|
48
|
+
Association.send(:possible_objects).should be_include(@post2.bullet_ar_key)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "should not raise error if object is nil" do
|
|
52
|
+
lambda { Association.add_possible_objects(nil) }.should_not raise_error
|
|
49
53
|
end
|
|
50
54
|
end
|
|
51
55
|
|
|
52
56
|
context ".add_impossible_object" do
|
|
53
57
|
it "should add impossible object" do
|
|
54
58
|
Association.add_impossible_object(@post1)
|
|
55
|
-
Association.send(:impossible_objects).should be_include(@post1.
|
|
59
|
+
Association.send(:impossible_objects).should be_include(@post1.bullet_ar_key)
|
|
56
60
|
end
|
|
57
61
|
end
|
|
58
62
|
|
|
59
63
|
context ".add_eager_loadings" do
|
|
60
64
|
it "should add objects, associations pair when eager_loadings are empty" do
|
|
61
65
|
Association.add_eager_loadings([@post1, @post2], :associations)
|
|
62
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
66
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :associations)
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
it "should add objects, associations pair for existing eager_loadings" do
|
|
66
70
|
Association.add_eager_loadings([@post1, @post2], :association1)
|
|
67
71
|
Association.add_eager_loadings([@post1, @post2], :association2)
|
|
68
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
69
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
72
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :association1)
|
|
73
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
|
70
74
|
end
|
|
71
75
|
|
|
72
76
|
it "should merge objects, associations pair for existing eager_loadings" do
|
|
73
|
-
Association.add_eager_loadings(@post1, :association1)
|
|
77
|
+
Association.add_eager_loadings([@post1], :association1)
|
|
74
78
|
Association.add_eager_loadings([@post1, @post2], :association2)
|
|
75
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
76
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
77
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
79
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association1)
|
|
80
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association2)
|
|
81
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key, @post2.bullet_ar_key], :association2)
|
|
78
82
|
end
|
|
79
83
|
|
|
80
84
|
it "should delete objects, associations pair for existing eager_loadings" do
|
|
81
85
|
Association.add_eager_loadings([@post1, @post2], :association1)
|
|
82
|
-
Association.add_eager_loadings(@post1, :association2)
|
|
83
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
84
|
-
Association.send(:eager_loadings).should be_include([@post1.
|
|
85
|
-
Association.send(:eager_loadings).should be_include([@post2.
|
|
86
|
+
Association.add_eager_loadings([@post1], :association2)
|
|
87
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association1)
|
|
88
|
+
Association.send(:eager_loadings).should be_include([@post1.bullet_ar_key], :association2)
|
|
89
|
+
Association.send(:eager_loadings).should be_include([@post2.bullet_ar_key], :association1)
|
|
86
90
|
end
|
|
87
91
|
end
|
|
88
92
|
end
|
|
@@ -11,7 +11,6 @@ module Bullet
|
|
|
11
11
|
|
|
12
12
|
context ".clear" do
|
|
13
13
|
it "should clear all class variables" do
|
|
14
|
-
Counter.clear
|
|
15
14
|
Counter.class_variable_get(:@@possible_objects).should be_nil
|
|
16
15
|
Counter.class_variable_get(:@@impossible_objects).should be_nil
|
|
17
16
|
end
|
|
@@ -19,13 +18,13 @@ module Bullet
|
|
|
19
18
|
|
|
20
19
|
context ".add_counter_cache" do
|
|
21
20
|
it "should create notification if conditions met" do
|
|
22
|
-
Counter.should_receive(:conditions_met?).with(@post1.
|
|
21
|
+
Counter.should_receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(true)
|
|
23
22
|
Counter.should_receive(:create_notification).with("Post", [:comments])
|
|
24
23
|
Counter.add_counter_cache(@post1, [:comments])
|
|
25
24
|
end
|
|
26
25
|
|
|
27
26
|
it "should not create notification if conditions not met" do
|
|
28
|
-
Counter.should_receive(:conditions_met?).with(@post1.
|
|
27
|
+
Counter.should_receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(false)
|
|
29
28
|
Counter.should_receive(:create_notification).never
|
|
30
29
|
Counter.add_counter_cache(@post1, [:comments])
|
|
31
30
|
end
|
|
@@ -34,30 +33,30 @@ module Bullet
|
|
|
34
33
|
context ".add_possible_objects" do
|
|
35
34
|
it "should add possible objects" do
|
|
36
35
|
Counter.add_possible_objects([@post1, @post2])
|
|
37
|
-
Counter.send(:possible_objects).should be_include(@post1.
|
|
38
|
-
Counter.send(:possible_objects).should be_include(@post2.
|
|
36
|
+
Counter.send(:possible_objects).should be_include(@post1.bullet_ar_key)
|
|
37
|
+
Counter.send(:possible_objects).should be_include(@post2.bullet_ar_key)
|
|
39
38
|
end
|
|
40
39
|
|
|
41
40
|
it "should add impossible object" do
|
|
42
41
|
Counter.add_impossible_object(@post1)
|
|
43
|
-
Counter.send(:impossible_objects).should be_include(@post1.
|
|
42
|
+
Counter.send(:impossible_objects).should be_include(@post1.bullet_ar_key)
|
|
44
43
|
end
|
|
45
44
|
end
|
|
46
45
|
|
|
47
46
|
context ".conditions_met?" do
|
|
48
47
|
it "should be true when object is possible, not impossible" do
|
|
49
48
|
Counter.add_possible_objects(@post1)
|
|
50
|
-
Counter.send(:conditions_met?, @post1.
|
|
49
|
+
Counter.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_true
|
|
51
50
|
end
|
|
52
51
|
|
|
53
52
|
it "should be false when object is not possible" do
|
|
54
|
-
Counter.send(:conditions_met?, @post1.
|
|
53
|
+
Counter.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_false
|
|
55
54
|
end
|
|
56
55
|
|
|
57
56
|
it "should be true when object is possible, and impossible" do
|
|
58
57
|
Counter.add_possible_objects(@post1)
|
|
59
58
|
Counter.add_impossible_object(@post1)
|
|
60
|
-
Counter.send(:conditions_met?, @post1.
|
|
59
|
+
Counter.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_false
|
|
61
60
|
end
|
|
62
61
|
end
|
|
63
62
|
end
|
|
@@ -21,69 +21,69 @@ module Bullet
|
|
|
21
21
|
context ".possible?" do
|
|
22
22
|
it "should be true if possible_objects contain" do
|
|
23
23
|
NPlusOneQuery.add_possible_objects(@post)
|
|
24
|
-
NPlusOneQuery.send(:possible?, @post.
|
|
24
|
+
NPlusOneQuery.send(:possible?, @post.bullet_ar_key).should be_true
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
context ".impossible?" do
|
|
29
29
|
it "should be true if impossible_objects contain" do
|
|
30
30
|
NPlusOneQuery.add_impossible_object(@post)
|
|
31
|
-
NPlusOneQuery.send(:impossible?, @post.
|
|
31
|
+
NPlusOneQuery.send(:impossible?, @post.bullet_ar_key).should be_true
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
context ".association?" do
|
|
36
36
|
it "should be true if object, associations pair is already existed" do
|
|
37
37
|
NPlusOneQuery.add_object_associations(@post, :association)
|
|
38
|
-
NPlusOneQuery.send(:association?, @post.
|
|
38
|
+
NPlusOneQuery.send(:association?, @post.bullet_ar_key, :association).should be_true
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "should be false if object, association pair is not existed" do
|
|
42
42
|
NPlusOneQuery.add_object_associations(@post, :association1)
|
|
43
|
-
NPlusOneQuery.send(:association?, @post.
|
|
43
|
+
NPlusOneQuery.send(:association?, @post.bullet_ar_key, :associatio2).should be_false
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
context ".conditions_met?" do
|
|
48
48
|
it "should be true if object is possible, not impossible and object, associations pair is not already existed" do
|
|
49
|
-
NPlusOneQuery.stub(:possible?).with(@post.
|
|
50
|
-
NPlusOneQuery.stub(:impossible?).with(@post.
|
|
51
|
-
NPlusOneQuery.stub(:association?).with(@post.
|
|
52
|
-
NPlusOneQuery.send(:conditions_met?, @post.
|
|
49
|
+
NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(true)
|
|
50
|
+
NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(false)
|
|
51
|
+
NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
|
|
52
|
+
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_true
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it "should be false if object is not possible, not impossible and object, associations pair is not already existed" do
|
|
56
|
-
NPlusOneQuery.stub(:possible?).with(@post.
|
|
57
|
-
NPlusOneQuery.stub(:impossible?).with(@post.
|
|
58
|
-
NPlusOneQuery.stub(:association?).with(@post.
|
|
59
|
-
NPlusOneQuery.send(:conditions_met?, @post.
|
|
56
|
+
NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(false)
|
|
57
|
+
NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(false)
|
|
58
|
+
NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
|
|
59
|
+
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_false
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it "should be false if object is possible, but impossible and object, associations pair is not already existed" do
|
|
63
|
-
NPlusOneQuery.stub(:possible?).with(@post.
|
|
64
|
-
NPlusOneQuery.stub(:impossible?).with(@post.
|
|
65
|
-
NPlusOneQuery.stub(:association?).with(@post.
|
|
66
|
-
NPlusOneQuery.send(:conditions_met?, @post.
|
|
63
|
+
NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(true)
|
|
64
|
+
NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(true)
|
|
65
|
+
NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
|
|
66
|
+
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_false
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it "should be false if object is possible, not impossible and object, associations pair is already existed" do
|
|
70
|
-
NPlusOneQuery.stub(:possible?).with(@post.
|
|
71
|
-
NPlusOneQuery.stub(:impossible?).with(@post.
|
|
72
|
-
NPlusOneQuery.stub(:association?).with(@post.
|
|
73
|
-
NPlusOneQuery.send(:conditions_met?, @post.
|
|
70
|
+
NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(true)
|
|
71
|
+
NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(false)
|
|
72
|
+
NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(true)
|
|
73
|
+
NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_false
|
|
74
74
|
end
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
context ".call_association" do
|
|
78
78
|
it "should create notification if conditions met" do
|
|
79
|
-
NPlusOneQuery.should_receive(:conditions_met?).with(@post.
|
|
79
|
+
NPlusOneQuery.should_receive(:conditions_met?).with(@post.bullet_ar_key, :association).and_return(true)
|
|
80
80
|
NPlusOneQuery.should_receive(:caller_in_project).and_return(["caller"])
|
|
81
81
|
NPlusOneQuery.should_receive(:create_notification).with(["caller"], "Post", :association)
|
|
82
82
|
NPlusOneQuery.call_association(@post, :association)
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
it "should not create notification if conditions not met" do
|
|
86
|
-
NPlusOneQuery.should_receive(:conditions_met?).with(@post.
|
|
86
|
+
NPlusOneQuery.should_receive(:conditions_met?).with(@post.bullet_ar_key, :association).and_return(false)
|
|
87
87
|
NPlusOneQuery.should_not_receive(:caller_in_project!)
|
|
88
88
|
NPlusOneQuery.should_not_receive(:create_notification).with("Post", :association)
|
|
89
89
|
NPlusOneQuery.call_association(@post, :association)
|
|
@@ -8,30 +8,30 @@ module Bullet
|
|
|
8
8
|
|
|
9
9
|
context ".call_associations" do
|
|
10
10
|
it "should get empty array if eager_loadgins" do
|
|
11
|
-
UnusedEagerAssociation.send(:call_associations, @post.
|
|
11
|
+
UnusedEagerAssociation.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it "should get call associations if object and association are both in eager_loadings and call_object_associations" do
|
|
15
|
-
UnusedEagerAssociation.add_eager_loadings(@post, :association)
|
|
15
|
+
UnusedEagerAssociation.add_eager_loadings([@post], :association)
|
|
16
16
|
UnusedEagerAssociation.add_call_object_associations(@post, :association)
|
|
17
|
-
UnusedEagerAssociation.send(:call_associations, @post.
|
|
17
|
+
UnusedEagerAssociation.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should == [:association]
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
it "should not get call associations if not exist in call_object_associations" do
|
|
21
|
-
UnusedEagerAssociation.add_eager_loadings(@post, :association)
|
|
22
|
-
UnusedEagerAssociation.send(:call_associations, @post.
|
|
21
|
+
UnusedEagerAssociation.add_eager_loadings([@post], :association)
|
|
22
|
+
UnusedEagerAssociation.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
context ".
|
|
26
|
+
context ".diff_object_associations" do
|
|
27
27
|
it "should return associations not exist in call_association" do
|
|
28
|
-
UnusedEagerAssociation.send(:
|
|
28
|
+
UnusedEagerAssociation.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should == [:association]
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should return empty if associations exist in call_association" do
|
|
32
|
-
UnusedEagerAssociation.add_eager_loadings(@post, :association)
|
|
32
|
+
UnusedEagerAssociation.add_eager_loadings([@post], :association)
|
|
33
33
|
UnusedEagerAssociation.add_call_object_associations(@post, :association)
|
|
34
|
-
UnusedEagerAssociation.send(:
|
|
34
|
+
UnusedEagerAssociation.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
@@ -50,9 +50,9 @@ module Bullet
|
|
|
50
50
|
it "should not create notification if object_association_diff is empty" do
|
|
51
51
|
UnusedEagerAssociation.clear
|
|
52
52
|
UnusedEagerAssociation.add_object_associations(@post, :association)
|
|
53
|
-
UnusedEagerAssociation.add_eager_loadings(@post, :association)
|
|
53
|
+
UnusedEagerAssociation.add_eager_loadings([@post], :association)
|
|
54
54
|
UnusedEagerAssociation.add_call_object_associations(@post, :association)
|
|
55
|
-
UnusedEagerAssociation.send(:
|
|
55
|
+
UnusedEagerAssociation.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
|
|
56
56
|
UnusedEagerAssociation.should_not_receive(:create_notification).with("Post", [:association])
|
|
57
57
|
UnusedEagerAssociation.check_unused_preload_associations
|
|
58
58
|
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Object do
|
|
4
|
+
context "bullet_ar_key" do
|
|
5
|
+
it "should return class and id composition" do
|
|
6
|
+
post = Post.first
|
|
7
|
+
post.bullet_ar_key.should == "Post:#{post.id}"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
begin
|
|
11
|
+
require 'mongoid'
|
|
12
|
+
|
|
13
|
+
it "should return class with namesapce and id composition" do
|
|
14
|
+
post = Mongoid::Post.first
|
|
15
|
+
post.bullet_ar_key.should == "Mongoid::Post:#{post.id}"
|
|
16
|
+
end
|
|
17
|
+
rescue LoadError
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe String do
|
|
4
|
+
context "bullet_class_name" do
|
|
5
|
+
it "should only return class name" do
|
|
6
|
+
"Post:1".bullet_class_name.should == "Post"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should return class name with namespace" do
|
|
10
|
+
"Mongoid::Post:1234567890".bullet_class_name.should == "Mongoid::Post"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -6,18 +6,18 @@ module Bullet
|
|
|
6
6
|
describe Object do
|
|
7
7
|
let(:post) { Post.first }
|
|
8
8
|
let(:another_post) { Post.last }
|
|
9
|
-
subject { Object.new.tap { |object| object.add(post.
|
|
9
|
+
subject { Object.new.tap { |object| object.add(post.bullet_ar_key) } }
|
|
10
10
|
|
|
11
11
|
context "#include?" do
|
|
12
12
|
it "should include the object" do
|
|
13
|
-
subject.should be_include(post.
|
|
13
|
+
subject.should be_include(post.bullet_ar_key)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
context "#add" do
|
|
18
18
|
it "should add an object" do
|
|
19
|
-
subject.add(another_post.
|
|
20
|
-
subject.should be_include(another_post.
|
|
19
|
+
subject.add(another_post.bullet_ar_key)
|
|
20
|
+
subject.should be_include(another_post.bullet_ar_key)
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|