module Bullet

Constants

BULLET_DEBUG
DETECTORS
TRUE
VERSION

Attributes

counter_cache_enable[W]
n_plus_one_query_enable[W]
orm_patches_applied[RW]
skip_html_injection[W]
skip_http_headers[RW]
stacktrace_excludes[W]
stacktrace_includes[W]
unused_eager_loading_enable[W]
whitelist[R]

Public Class Methods

add_whitelist(options) click to toggle source
# File lib/bullet.rb, line 99
def add_whitelist(options)
  reset_whitelist
  @whitelist[options[:type]][options[:class_name]] ||= []
  @whitelist[options[:type]][options[:class_name]] << options[:association].to_sym
end
app_root() click to toggle source
# File lib/bullet.rb, line 75
def app_root
  @app_root ||= (defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd).to_s
end
bullet_logger=(active) click to toggle source
# File lib/bullet.rb, line 124
def bullet_logger=(active)
  if active
    require 'fileutils'
    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
end
clear_whitelist() click to toggle source
# File lib/bullet.rb, line 120
def clear_whitelist
  @whitelist = nil
end
console_enabled?() click to toggle source
# File lib/bullet.rb, line 239
def console_enabled?
  UniformNotifier.active_notifiers.include?(UniformNotifier::JavascriptConsole)
end
counter_cache_enable?() click to toggle source
# File lib/bullet.rb, line 87
def counter_cache_enable?
  enable? && !!@counter_cache_enable
end
debug(title, message) click to toggle source
# File lib/bullet.rb, line 134
def debug(title, message)
  puts "[Bullet][#{title}] #{message}" if ENV[BULLET_DEBUG] == TRUE
end
delete_whitelist(options) click to toggle source
# File lib/bullet.rb, line 105
def delete_whitelist(options)
  reset_whitelist
  @whitelist[options[:type]][options[:class_name]] ||= []
  @whitelist[options[:type]][options[:class_name]].delete(options[:association].to_sym)
  @whitelist[options[:type]].delete_if { |_key, val| val.empty? }
end
enable=(enable) click to toggle source
# File lib/bullet.rb, line 58
def enable=(enable)
  @enable = @n_plus_one_query_enable = @unused_eager_loading_enable = @counter_cache_enable = enable

  if enable?
    reset_whitelist
    unless orm_patches_applied
      self.orm_patches_applied = true
      Bullet::Mongoid.enable if mongoid?
      Bullet::ActiveRecord.enable if active_record?
    end
  end
end
enable?() click to toggle source
# File lib/bullet.rb, line 71
def enable?
  !!@enable
end
end_request() click to toggle source
# File lib/bullet.rb, line 153
def end_request
  Thread.current[:bullet_start] = nil
  Thread.current[:bullet_notification_collector] = nil

  Thread.current[:bullet_object_associations] = nil
  Thread.current[:bullet_call_object_associations] = nil
  Thread.current[:bullet_possible_objects] = nil
  Thread.current[:bullet_impossible_objects] = nil
  Thread.current[:bullet_inversed_objects] = nil
  Thread.current[:bullet_eager_loadings] = nil

  Thread.current[:bullet_counter_possible_objects] = nil
  Thread.current[:bullet_counter_impossible_objects] = nil
end
gather_inline_notifications() click to toggle source
# File lib/bullet.rb, line 183
def gather_inline_notifications
  responses = []
  for_each_active_notifier_with_notification { |notification| responses << notification.notify_inline }
  responses.join("\n")
end
get_whitelist_associations(type, class_name) click to toggle source
# File lib/bullet.rb, line 112
def get_whitelist_associations(type, class_name)
  Array(@whitelist[type][class_name])
end
inject_into_page?() click to toggle source
# File lib/bullet.rb, line 243
def inject_into_page?
  return false if defined?(@skip_html_injection) && @skip_html_injection

  console_enabled? || add_footer
end
n_plus_one_query_enable?() click to toggle source
# File lib/bullet.rb, line 79
def n_plus_one_query_enable?
  enable? && !!@n_plus_one_query_enable
end
notification?() click to toggle source
# File lib/bullet.rb, line 176
def notification?
  return unless start?

  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
  notification_collector.notifications_present?
end
notification_collector() click to toggle source
# File lib/bullet.rb, line 172
def notification_collector
  Thread.current[:bullet_notification_collector]
end
perform_out_of_channel_notifications(env = {}) click to toggle source
# File lib/bullet.rb, line 189
def perform_out_of_channel_notifications(env = {})
  request_uri = build_request_uri(env)
  for_each_active_notifier_with_notification do |notification|
    notification.url = request_uri
    notification.notify_out_of_channel
  end
end
profile() { || ... } click to toggle source
# File lib/bullet.rb, line 219
def profile
  return_value = nil

  if Bullet.enable?
    begin
      Bullet.start_request

      return_value = yield

      Bullet.perform_out_of_channel_notifications if Bullet.notification?
    ensure
      Bullet.end_request
    end
  else
    return_value = yield
  end

  return_value
end
raise=(should_raise) click to toggle source
# File lib/bullet.rb, line 48
def raise=(should_raise)
  UniformNotifier.raise = (should_raise ? Notification::UnoptimizedQueryError : false)
end
reset_whitelist() click to toggle source
# File lib/bullet.rb, line 116
def reset_whitelist
  @whitelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
end
stacktrace_excludes() click to toggle source
# File lib/bullet.rb, line 95
def stacktrace_excludes
  @stacktrace_excludes ||= []
end
stacktrace_includes() click to toggle source
# File lib/bullet.rb, line 91
def stacktrace_includes
  @stacktrace_includes ||= []
end
start?() click to toggle source
# File lib/bullet.rb, line 168
def start?
  enable? && Thread.current[:bullet_start]
end
start_request() click to toggle source
# File lib/bullet.rb, line 138
def start_request
  Thread.current[:bullet_start] = true
  Thread.current[:bullet_notification_collector] = Bullet::NotificationCollector.new

  Thread.current[:bullet_object_associations] = Bullet::Registry::Base.new
  Thread.current[:bullet_call_object_associations] = Bullet::Registry::Base.new
  Thread.current[:bullet_possible_objects] = Bullet::Registry::Object.new
  Thread.current[:bullet_impossible_objects] = Bullet::Registry::Object.new
  Thread.current[:bullet_inversed_objects] = Bullet::Registry::Base.new
  Thread.current[:bullet_eager_loadings] = Bullet::Registry::Association.new

  Thread.current[:bullet_counter_possible_objects] ||= Bullet::Registry::Object.new
  Thread.current[:bullet_counter_impossible_objects] ||= Bullet::Registry::Object.new
end
text_notifications() click to toggle source
# File lib/bullet.rb, line 203
def text_notifications
  info = []
  notification_collector.collection.each do |notification|
    info << notification.notification_data.values.compact.join("\n")
  end
  info
end
unused_eager_loading_enable?() click to toggle source
# File lib/bullet.rb, line 83
def unused_eager_loading_enable?
  enable? && !!@unused_eager_loading_enable
end
warnings() click to toggle source
# File lib/bullet.rb, line 211
def warnings
  notification_collector.collection.each_with_object({}) do |notification, warnings|
    warning_type = notification.class.to_s.split(':').last.tableize
    warnings[warning_type] ||= []
    warnings[warning_type] << notification
  end
end

Private Class Methods

build_request_uri(env) click to toggle source
# File lib/bullet.rb, line 260
def build_request_uri(env)
  return "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']}" if env['REQUEST_URI']

  if env['QUERY_STRING'].present?
    "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}?#{env['QUERY_STRING']}"
  else
    "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
  end
end
for_each_active_notifier_with_notification() { |notification| ... } click to toggle source
# File lib/bullet.rb, line 251
def for_each_active_notifier_with_notification
  UniformNotifier.active_notifiers.each do |notifier|
    notification_collector.collection.each do |notification|
      notification.notifier = notifier
      yield notification
    end
  end
end