From c7a6733d1fce0c147e58732e97b4e54cabab2847 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Fri, 18 Jan 2019 13:15:26 -0500 Subject: [PATCH] Make Rails check more specific In one of our non-Rails projects we added ActionMailer. It comes along with a number of dependencies and modules namespaced under `Rails`. The problem is that we don't have a Rails app and therefore `Rails.root` is not defined. This causes bullet to blow up when getting `Rails.root`. The fix is to be more specific with the check. I also consolidated the logic to one method since `rails?` was only ever used to check if Bullet should call `Rails.root`. --- lib/bullet.rb | 9 ++++++--- lib/bullet/dependency.rb | 4 ---- lib/bullet/stack_trace_filter.rb | 5 ++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/bullet.rb b/lib/bullet.rb index 6363c02e..cb7c8ca7 100644 --- a/lib/bullet.rb +++ b/lib/bullet.rb @@ -63,6 +63,10 @@ def enable? !!@enable end + def app_root + (defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd).to_s + end + def n_plus_one_query_enable? enable? && !!@n_plus_one_query_enable end @@ -111,9 +115,8 @@ def clear_whitelist def bullet_logger=(active) if active require 'fileutils' - root_path = (rails? ? Rails.root.to_s : Dir.pwd).to_s - FileUtils.mkdir_p(root_path + '/log') - bullet_log_file = File.open("#{root_path}/log/bullet.log", 'a+') + FileUtils.mkdir_p(app_root + '/log') + bullet_log_file = File.open("#{app_root}/log/bullet.log", 'a+') bullet_log_file.sync = true UniformNotifier.customized_logger = bullet_log_file end diff --git a/lib/bullet/dependency.rb b/lib/bullet/dependency.rb index 98f09b4b..1ce4d2ec 100644 --- a/lib/bullet/dependency.rb +++ b/lib/bullet/dependency.rb @@ -10,10 +10,6 @@ def active_record? @active_record ||= defined? ::ActiveRecord end - def rails? - @rails ||= defined? ::Rails - end - def active_record_version @active_record_version ||= begin if active_record40? diff --git a/lib/bullet/stack_trace_filter.rb b/lib/bullet/stack_trace_filter.rb index 1c9382c6..2b179cc8 100644 --- a/lib/bullet/stack_trace_filter.rb +++ b/lib/bullet/stack_trace_filter.rb @@ -5,12 +5,11 @@ module StackTraceFilter VENDOR_PATH = '/vendor' def caller_in_project - app_root = rails? ? Rails.root.to_s : Dir.pwd - vendor_root = app_root + VENDOR_PATH + vendor_root = Bullet.app_root + VENDOR_PATH bundler_path = Bundler.bundle_path.to_s select_caller_locations do |location| caller_path = location_as_path(location) - caller_path.include?(app_root) && !caller_path.include?(vendor_root) && !caller_path.include?(bundler_path) || + caller_path.include?(Bullet.app_root) && !caller_path.include?(vendor_root) && !caller_path.include?(bundler_path) || Bullet.stacktrace_includes.any? { |include_pattern| pattern_matches?(location, include_pattern) } end end