Skip to content

Commit

Permalink
Reduce object count
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed Aug 4, 2015
1 parent 5143b9f commit 37e62d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module Bullet
autoload :Registry, 'bullet/registry'
autoload :NotificationCollector, 'bullet/notification_collector'

BULLET_DEBUG = 'BULLET_DEBUG'.freeze
TRUE = 'true'.freeze

if defined? Rails::Railtie
class BulletRailtie < Rails::Railtie
initializer "bullet.configure_rails_initialization" do |app|
Expand Down Expand Up @@ -98,7 +101,7 @@ def bullet_logger=(active)
end

def debug(title, message)
puts "[Bullet][#{title}] #{message}" if ENV['BULLET_DEBUG'] == 'true'
puts "[Bullet][#{title}] #{message}" if ENV[BULLET_DEBUG] == TRUE
end

def start_request
Expand Down
4 changes: 3 additions & 1 deletion lib/bullet/active_record3.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Bullet
module ActiveRecord
LOAD_TARGET = 'load_target'.freeze

def self.enable
require 'active_record'
::ActiveRecord::Relation.class_eval do
Expand Down Expand Up @@ -138,7 +140,7 @@ def load_target
# avoid stack level too deep
result = origin_load_target
if Bullet.start?
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless caller.any? { |c| c.include?("load_target") }
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless caller.any? { |c| c.include?(LOAD_TARGET) }
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
end
result
Expand Down
35 changes: 35 additions & 0 deletions spec/bullet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,39 @@
end
end
end

describe '#debug' do
before(:each) do
$stdout = StringIO.new
end

after(:each) do
$stdout = STDOUT
end

context 'when debug is enabled' do
before(:each) do
ENV['BULLET_DEBUG'] = 'true'
end

after(:each) do
ENV['BULLET_DEBUG'] = 'false'
end

it 'should output debug information' do
Bullet.debug('debug_message', 'this is helpful information')

expect($stdout.string)
.to eq("[Bullet][debug_message] this is helpful information\n")
end
end

context 'when debug is disabled' do
it 'should output debug information' do
Bullet.debug('debug_message', 'this is helpful information')

expect($stdout.string).to be_empty
end
end
end
end

0 comments on commit 37e62d1

Please sign in to comment.