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.
Files changed (54) hide show
  1. data/.travis.yml +5 -0
  2. data/Gemfile +3 -1
  3. data/Gemfile.lock +44 -33
  4. data/Gemfile.rails-2.3.14 +16 -0
  5. data/Gemfile.rails-2.3.14.lock +65 -0
  6. data/Gemfile.rails-3.0.12 +17 -0
  7. data/Gemfile.rails-3.0.12.lock +116 -0
  8. data/Gemfile.rails-3.1.4 +18 -0
  9. data/Gemfile.rails-3.1.4.lock +134 -0
  10. data/README.textile +6 -6
  11. data/lib/bullet/active_record2.rb +12 -0
  12. data/lib/bullet/active_record3.rb +12 -0
  13. data/lib/bullet/detector/association.rb +11 -12
  14. data/lib/bullet/detector/counter.rb +6 -6
  15. data/lib/bullet/detector/n_plus_one_query.rb +9 -9
  16. data/lib/bullet/detector/unused_eager_association.rb +9 -9
  17. data/lib/bullet/ext/object.rb +5 -0
  18. data/lib/bullet/ext/string.rb +5 -0
  19. data/lib/bullet/mongoid.rb +56 -0
  20. data/lib/bullet/registry/object.rb +4 -6
  21. data/lib/bullet/version.rb +1 -1
  22. data/lib/bullet.rb +15 -9
  23. data/spec/bullet/detector/association_spec.rb +20 -16
  24. data/spec/bullet/detector/counter_spec.rb +8 -9
  25. data/spec/bullet/detector/n_plus_one_query_spec.rb +22 -22
  26. data/spec/bullet/detector/unused_eager_association_spec.rb +11 -11
  27. data/spec/bullet/ext/object_spec.rb +20 -0
  28. data/spec/bullet/ext/string_spec.rb +13 -0
  29. data/spec/bullet/registry/object_spec.rb +4 -4
  30. data/spec/integration/association_spec.rb +463 -401
  31. data/spec/integration/counter_spec.rb +27 -25
  32. data/spec/integration/mongoid/association_spec.rb +276 -0
  33. data/spec/integration/rails2/association_spec.rb +593 -0
  34. data/spec/integration/rails2/counter_spec.rb +39 -0
  35. data/spec/models/mongoid/address.rb +5 -0
  36. data/spec/models/mongoid/category.rb +6 -0
  37. data/spec/models/mongoid/comment.rb +5 -0
  38. data/spec/models/mongoid/company.rb +5 -0
  39. data/spec/models/mongoid/entry.rb +5 -0
  40. data/spec/models/mongoid/post.rb +8 -0
  41. data/spec/models/post.rb +12 -6
  42. data/spec/spec_helper.rb +32 -8
  43. data/spec/support/bullet_ext.rb +1 -1
  44. data/spec/support/mongo_seed.rb +41 -0
  45. data/spec/support/{seed.rb → sqlite_seed.rb} +1 -22
  46. data/test.result +2293 -0
  47. metadata +42 -22
  48. data/spec/integration/association_for_chris_spec.rb +0 -37
  49. data/spec/integration/association_for_peschkaj_spec.rb +0 -26
  50. data/spec/models/contact.rb +0 -3
  51. data/spec/models/deal.rb +0 -4
  52. data/spec/models/email.rb +0 -3
  53. data/spec/models/hotel.rb +0 -4
  54. 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.ar_key, associations)
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.ar_key }
18
+ object_or_objects.each { |object| possible_objects.add object.bullet_ar_key }
19
19
  else
20
- possible_objects.add object_or_objects.ar_key
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.ar_key
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?(object_ar_key, associations)
43
- possible_objects.include?(object_ar_key) && !impossible_objects.include?(object_ar_key)
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.ar_key, associations)
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?(object_ar_key, associations)
26
- possible?(object_ar_key) && !impossible?(object_ar_key) && !association?(object_ar_key, associations)
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?(object_ar_key)
36
- possible_objects.include? object_ar_key
35
+ def possible?(bullet_ar_key)
36
+ possible_objects.include? bullet_ar_key
37
37
  end
38
38
 
39
- def impossible?(object_ar_key)
40
- impossible_objects.include? object_ar_key
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?(object_ar_key, associations)
45
- value = object_associations[object_ar_key]
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 |object_ar_key, association|
12
- object_association_diff = diff_object_association object_ar_key, association
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 object_ar_key.split(":").first, object_association_diff
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(object_ar_key, association)
25
+ def call_associations(bullet_ar_key, associations)
26
26
  all = Set.new
27
- eager_loadings.similarly_associated(object_ar_key, association).each do |related_object_ar_key|
28
- coa = call_object_associations[related_object_ar_key]
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 diff_object_association(object_ar_key, association)
36
- potential_objects = association - call_associations(object_ar_key, association)
37
- potential_objects.reject { |a| a.is_a?(Hash) }
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,5 @@
1
+ class Object
2
+ def bullet_ar_key
3
+ "#{self.class}:#{self.id}"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def bullet_class_name
3
+ self.sub(/:[^:]*?$/, "")
4
+ end
5
+ 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(object_ar_key)
5
- klazz = object_ar_key.split(":").first
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?(object_ar_key)
10
- klazz = object_ar_key.split(":").first
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
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  module Bullet
3
- VERSION = "2.3.0"
3
+ VERSION = "4.0.0"
4
4
  end
5
5
 
data/lib/bullet.rb CHANGED
@@ -1,11 +1,7 @@
1
1
  require 'set'
2
2
  require 'uniform_notifier'
3
-
4
- class Object
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
- Bullet::ActiveRecord.enable
48
- if Rails.version =~ /\A2./
49
- Bullet::ActionController.enable
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.ar_key, :associations)
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.ar_key, :associations)
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.ar_key)
48
- Association.send(:possible_objects).should be_include(@post2.ar_key)
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.ar_key)
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.ar_key, @post2.ar_key], :associations)
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.ar_key, @post2.ar_key], :association1)
69
- Association.send(:eager_loadings).should be_include([@post1.ar_key, @post2.ar_key], :association2)
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.ar_key], :association1)
76
- Association.send(:eager_loadings).should be_include([@post1.ar_key], :association2)
77
- Association.send(:eager_loadings).should be_include([@post1.ar_key, @post2.ar_key], :association2)
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.ar_key], :association1)
84
- Association.send(:eager_loadings).should be_include([@post1.ar_key], :association2)
85
- Association.send(:eager_loadings).should be_include([@post2.ar_key], :association1)
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.ar_key, [:comments]).and_return(true)
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.ar_key, [:comments]).and_return(false)
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.ar_key)
38
- Counter.send(:possible_objects).should be_include(@post2.ar_key)
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.ar_key)
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.ar_key, :associations).should be_true
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.ar_key, :associations).should be_false
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.ar_key, :associations).should be_false
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.ar_key).should be_true
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.ar_key).should be_true
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.ar_key, :association).should be_true
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.ar_key, :associatio2).should be_false
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.ar_key).and_return(true)
50
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(false)
51
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(false)
52
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_true
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.ar_key).and_return(false)
57
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(false)
58
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(false)
59
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_false
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.ar_key).and_return(true)
64
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(true)
65
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(false)
66
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_false
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.ar_key).and_return(true)
71
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(false)
72
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(true)
73
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_false
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.ar_key, :association).and_return(true)
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.ar_key, :association).and_return(false)
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.ar_key, Set.new([:association])).should be_empty
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.ar_key, Set.new([:association])).should == [:association]
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.ar_key, Set.new([:association])).should be_empty
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 ".diff_object_association" do
26
+ context ".diff_object_associations" do
27
27
  it "should return associations not exist in call_association" do
28
- UnusedEagerAssociation.send(:diff_object_association, @post.ar_key, Set.new([:association])).should == [:association]
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(:diff_object_association, @post.ar_key, Set.new([:association])).should be_empty
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(:diff_object_association, @post.ar_key, Set.new([:association])).should be_empty
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.ar_key) } }
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.ar_key)
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.ar_key)
20
- subject.should be_include(another_post.ar_key)
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