bullet 4.14.8 → 5.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/lib/bullet.rb CHANGED
@@ -16,6 +16,9 @@ module Bullet
16
16
  autoload :Registry, 'bullet/registry'
17
17
  autoload :NotificationCollector, 'bullet/notification_collector'
18
18
 
19
+ BULLET_DEBUG = 'BULLET_DEBUG'.freeze
20
+ TRUE = 'true'.freeze
21
+
19
22
  if defined? Rails::Railtie
20
23
  class BulletRailtie < Rails::Railtie
21
24
  initializer "bullet.configure_rails_initialization" do |app|
@@ -25,7 +28,7 @@ module Bullet
25
28
  end
26
29
 
27
30
  class << self
28
- attr_writer :enable, :n_plus_one_query_enable, :unused_eager_loading_enable, :counter_cache_enable, :stacktrace_includes
31
+ attr_writer :enable, :n_plus_one_query_enable, :unused_eager_loading_enable, :counter_cache_enable, :stacktrace_includes, :stacktrace_excludes
29
32
  attr_reader :notification_collector, :whitelist
30
33
  attr_accessor :add_footer, :orm_pathches_applied
31
34
 
@@ -73,7 +76,12 @@ module Bullet
73
76
  @stacktrace_includes || []
74
77
  end
75
78
 
79
+ def stacktrace_excludes
80
+ @stacktrace_excludes || []
81
+ end
82
+
76
83
  def add_whitelist(options)
84
+ reset_whitelist
77
85
  @whitelist[options[:type]][options[:class_name].classify] ||= []
78
86
  @whitelist[options[:type]][options[:class_name].classify] << options[:association].to_sym
79
87
  end
@@ -83,7 +91,11 @@ module Bullet
83
91
  end
84
92
 
85
93
  def reset_whitelist
86
- @whitelist = {:n_plus_one_query => {}, :unused_eager_loading => {}, :counter_cache => {}}
94
+ @whitelist ||= {:n_plus_one_query => {}, :unused_eager_loading => {}, :counter_cache => {}}
95
+ end
96
+
97
+ def clear_whitelist
98
+ @whitelist = nil
87
99
  end
88
100
 
89
101
  def bullet_logger=(active)
@@ -98,7 +110,7 @@ module Bullet
98
110
  end
99
111
 
100
112
  def debug(title, message)
101
- puts "[Bullet][#{title}] #{message}" if ENV['BULLET_DEBUG'] == 'true'
113
+ puts "[Bullet][#{title}] #{message}" if ENV[BULLET_DEBUG] == TRUE
102
114
  end
103
115
 
104
116
  def start_request
@@ -132,7 +144,7 @@ module Bullet
132
144
  end
133
145
 
134
146
  def start?
135
- Thread.current[:bullet_start]
147
+ enable? && Thread.current[:bullet_start]
136
148
  end
137
149
 
138
150
  def notification_collector
@@ -85,6 +85,21 @@ module Bullet
85
85
  expect(NPlusOneQuery).not_to receive(:create_notification).with("Post", :association)
86
86
  NPlusOneQuery.call_association(@post, :association)
87
87
  end
88
+
89
+ context "stacktrace_excludes" do
90
+ before { Bullet.stacktrace_excludes = [ 'def' ] }
91
+ after { Bullet.stacktrace_excludes = nil }
92
+
93
+ it "should not create notification when stacktrace contains paths that are in the exclude list" do
94
+ in_project = File.join(Dir.pwd, 'abc', 'abc.rb')
95
+ included_path = '/ghi/ghi.rb'
96
+ excluded_path = '/def/def.rb'
97
+
98
+ expect(NPlusOneQuery).to receive(:caller).and_return([in_project, included_path, excluded_path])
99
+ expect(NPlusOneQuery).to_not receive(:create_notification)
100
+ NPlusOneQuery.call_association(@post, :association)
101
+ end
102
+ end
88
103
  end
89
104
 
90
105
  context ".caller_in_project" do
@@ -30,14 +30,14 @@ module Bullet
30
30
  end
31
31
 
32
32
  it "should return blank if no user available" do
33
- temp_env_variable("USER","") do
33
+ temp_env_variable("USER", "") do
34
34
  expect(subject).to receive(:`).with("whoami").and_return("")
35
35
  expect(subject.whoami).to eq("")
36
36
  end
37
37
  end
38
38
 
39
39
  it "should return blank if whoami is not available" do
40
- temp_env_variable("USER","") do
40
+ temp_env_variable("USER", "") do
41
41
  expect(subject).to receive(:`).with("whoami").and_raise(Errno::ENOENT)
42
42
  expect(subject.whoami).to eq("")
43
43
  end
data/spec/bullet_spec.rb CHANGED
@@ -38,4 +38,51 @@ describe Bullet, focused: true do
38
38
  end
39
39
  end
40
40
  end
41
+
42
+ describe '#start?' do
43
+ context 'when bullet is disabled' do
44
+ before(:each) do
45
+ Bullet.enable = false
46
+ end
47
+
48
+ it 'should not be started' do
49
+ expect(Bullet).not_to be_start
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#debug' do
55
+ before(:each) do
56
+ $stdout = StringIO.new
57
+ end
58
+
59
+ after(:each) do
60
+ $stdout = STDOUT
61
+ end
62
+
63
+ context 'when debug is enabled' do
64
+ before(:each) do
65
+ ENV['BULLET_DEBUG'] = 'true'
66
+ end
67
+
68
+ after(:each) do
69
+ ENV['BULLET_DEBUG'] = 'false'
70
+ end
71
+
72
+ it 'should output debug information' do
73
+ Bullet.debug('debug_message', 'this is helpful information')
74
+
75
+ expect($stdout.string)
76
+ .to eq("[Bullet][debug_message] this is helpful information\n")
77
+ end
78
+ end
79
+
80
+ context 'when debug is disabled' do
81
+ it 'should output debug information' do
82
+ Bullet.debug('debug_message', 'this is helpful information')
83
+
84
+ expect($stdout.string).to be_empty
85
+ end
86
+ end
87
+ end
41
88
  end
@@ -624,7 +624,7 @@ if !mongoid? && active_record3?
624
624
 
625
625
  context "whitelist n plus one query" do
626
626
  before { Bullet.add_whitelist :type => :n_plus_one_query, :class_name => "Post", :association => :comments }
627
- after { Bullet.reset_whitelist }
627
+ after { Bullet.clear_whitelist }
628
628
 
629
629
  it "should not detect n plus one query" do
630
630
  Post.all.each do |post|
@@ -647,7 +647,7 @@ if !mongoid? && active_record3?
647
647
 
648
648
  context "whitelist unused eager loading" do
649
649
  before { Bullet.add_whitelist :type => :unused_eager_loading, :class_name => "Post", :association => :comments }
650
- after { Bullet.reset_whitelist }
650
+ after { Bullet.clear_whitelist }
651
651
 
652
652
  it "should not detect unused eager loading" do
653
653
  Post.includes(:comments).map(&:name)
@@ -560,6 +560,27 @@ if !mongoid? && active_record4?
560
560
  end
561
561
  end
562
562
 
563
+ describe Bullet::Detector::Association, "query immediately after creation" do
564
+ context "document => children" do
565
+ it 'should not detect non preload associations' do
566
+ document1 = Document.new
567
+ document1.children.build
568
+ document1.save
569
+
570
+ document2 = Document.new(parent: document1)
571
+ document2.save
572
+ document2.parent
573
+
574
+ document1.children.each.first
575
+
576
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
577
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
578
+
579
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
580
+ end
581
+ end
582
+ end
583
+
563
584
  describe Bullet::Detector::Association, "STI" do
564
585
  context "page => author" do
565
586
  it "should detect non preload associations" do
@@ -647,7 +668,7 @@ if !mongoid? && active_record4?
647
668
 
648
669
  context "whitelist n plus one query" do
649
670
  before { Bullet.add_whitelist :type => :n_plus_one_query, :class_name => "Post", :association => :comments }
650
- after { Bullet.reset_whitelist }
671
+ after { Bullet.clear_whitelist }
651
672
 
652
673
  it "should not detect n plus one query" do
653
674
  Post.all.each do |post|
@@ -670,7 +691,7 @@ if !mongoid? && active_record4?
670
691
 
671
692
  context "whitelist unused eager loading" do
672
693
  before { Bullet.add_whitelist :type => :unused_eager_loading, :class_name => "Post", :association => :comments }
673
- after { Bullet.reset_whitelist }
694
+ after { Bullet.clear_whitelist }
674
695
 
675
696
  it "should not detect unused eager loading" do
676
697
  Post.includes(:comments).map(&:name)