Module: Bullet

Extended by:
Dependency
Defined in:
lib/bullet.rb,
lib/bullet/rack.rb,
lib/bullet/version.rb,
lib/bullet/detector.rb,
lib/bullet/registry.rb,
lib/bullet/mongoid2x.rb,
lib/bullet/mongoid3x.rb,
lib/bullet/mongoid4x.rb,
lib/bullet/dependency.rb,
lib/bullet/notification.rb,
lib/bullet/detector/base.rb,
lib/bullet/registry/base.rb,
lib/bullet/active_record3.rb,
lib/bullet/active_record4.rb,
lib/bullet/active_record3x.rb,
lib/bullet/active_record41.rb,
lib/bullet/active_record42.rb,
lib/bullet/registry/object.rb,
lib/bullet/notification/base.rb,
lib/bullet/detector/association.rb,
lib/bullet/registry/association.rb,
lib/bullet/detector/counter_cache.rb,
lib/bullet/notification_collector.rb,
lib/bullet/detector/n_plus_one_query.rb,
lib/bullet/notification/counter_cache.rb,
lib/bullet/detector/unused_eager_loading.rb,
lib/bullet/notification/n_plus_one_query.rb,
lib/bullet/notification/unused_eager_loading.rb

Defined Under Namespace

Modules: ActiveRecord, Dependency, Detector, Mongoid, Notification, Registry Classes: BulletRailtie, NotificationCollector, Rack

Constant Summary collapse

DETECTORS =
[ Bullet::Detector::NPlusOneQuery,
Bullet::Detector::UnusedEagerLoading,
Bullet::Detector::CounterCache ]
VERSION =
"4.14.8"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Dependency

active_record30?, active_record31?, active_record32?, active_record3?, active_record40?, active_record41?, active_record42?, active_record4?, active_record?, active_record_version, mongoid2x?, mongoid3x?, mongoid4x?, mongoid?, mongoid_version, rails?

Class Attribute Details

Returns the value of attribute add_footer.



30
31
32
# File 'lib/bullet.rb', line 30

def add_footer
  @add_footer
end

.counter_cache_enable=(value) ⇒ Object (writeonly)

Sets the attribute counter_cache_enable

Parameters:

  • value

    the value to set the attribute counter_cache_enable to.



28
29
30
# File 'lib/bullet.rb', line 28

def counter_cache_enable=(value)
  @counter_cache_enable = value
end

.enable=(enable) ⇒ Object (writeonly)

Sets the attribute enable

Parameters:

  • value

    the value to set the attribute enable to.



28
29
30
# File 'lib/bullet.rb', line 28

def enable=(value)
  @enable = value
end

.n_plus_one_query_enable=(value) ⇒ Object (writeonly)

Sets the attribute n_plus_one_query_enable

Parameters:

  • value

    the value to set the attribute n_plus_one_query_enable to.



28
29
30
# File 'lib/bullet.rb', line 28

def n_plus_one_query_enable=(value)
  @n_plus_one_query_enable = value
end

.notification_collectorObject (readonly)

Returns the value of attribute notification_collector.



29
30
31
# File 'lib/bullet.rb', line 29

def notification_collector
  @notification_collector
end

.orm_pathches_appliedObject

Returns the value of attribute orm_pathches_applied.



30
31
32
# File 'lib/bullet.rb', line 30

def orm_pathches_applied
  @orm_pathches_applied
end

.stacktrace_includesObject



72
73
74
# File 'lib/bullet.rb', line 72

def stacktrace_includes
  @stacktrace_includes || []
end

.unused_eager_loading_enable=(value) ⇒ Object (writeonly)

Sets the attribute unused_eager_loading_enable

Parameters:

  • value

    the value to set the attribute unused_eager_loading_enable to.



28
29
30
# File 'lib/bullet.rb', line 28

def unused_eager_loading_enable=(value)
  @unused_eager_loading_enable = value
end

.whitelistObject (readonly)

Returns the value of attribute whitelist.



29
30
31
# File 'lib/bullet.rb', line 29

def whitelist
  @whitelist
end

Class Method Details

.add_whitelist(options) ⇒ Object



76
77
78
79
# File 'lib/bullet.rb', line 76

def add_whitelist(options)
  @whitelist[options[:type]][options[:class_name].classify] ||= []
  @whitelist[options[:type]][options[:class_name].classify] << options[:association].to_sym
end

.bullet_logger=(active) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/bullet.rb', line 89

def bullet_logger=(active)
  if active
    require 'fileutils'
    root_path = "#{rails? ? Rails.root.to_s : Dir.pwd}"
    FileUtils.mkdir_p(root_path + '/log')
    bullet_log_file = File.open("#{root_path}/log/bullet.html", 'a+')
    bullet_log_file.sync = true
    UniformNotifier.customized_logger = bullet_log_file
  end
end

.counter_cache_enable?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/bullet.rb', line 68

def counter_cache_enable?
  self.enable? && !!@counter_cache_enable
end

.debug(title, message) ⇒ Object



100
101
102
# File 'lib/bullet.rb', line 100

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

.enable?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/bullet.rb', line 56

def enable?
  !!@enable
end

.end_requestObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bullet.rb', line 119

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


163
164
165
166
167
168
169
# File 'lib/bullet.rb', line 163

def footer_info
  info = []
  notification_collector.collection.each do |notification|
    info << notification.short_notice
  end
  info
end

.gather_inline_notificationsObject



148
149
150
151
152
153
154
# File 'lib/bullet.rb', line 148

def gather_inline_notifications
  responses = []
  for_each_active_notifier_with_notification do |notification|
    responses << notification.notify_inline
  end
  responses.join( "\n" )
end

.get_whitelist_associations(type, class_name) ⇒ Object



81
82
83
# File 'lib/bullet.rb', line 81

def get_whitelist_associations(type, class_name)
  Array(@whitelist[type][class_name])
end

.n_plus_one_query_enable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/bullet.rb', line 60

def n_plus_one_query_enable?
  self.enable? && !!@n_plus_one_query_enable
end

.notification?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/bullet.rb', line 142

def notification?
  return unless start?
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
  notification_collector.notifications_present?
end

.perform_out_of_channel_notifications(env = {}) ⇒ Object



156
157
158
159
160
161
# File 'lib/bullet.rb', line 156

def perform_out_of_channel_notifications(env = {})
  for_each_active_notifier_with_notification do |notification|
    notification.url = env['REQUEST_URI']
    notification.notify_out_of_channel
  end
end

.profileObject



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bullet.rb', line 180

def profile
  if Bullet.enable?
    begin
      Bullet.start_request

      yield

      Bullet.perform_out_of_channel_notifications if Bullet.notification?
    ensure
      Bullet.end_request
    end
  end
end

.raise=(should_raise) ⇒ Object



36
37
38
# File 'lib/bullet.rb', line 36

def raise=(should_raise)
  UniformNotifier.raise=(should_raise ? Notification::UnoptimizedQueryError : false)
end

.reset_whitelistObject



85
86
87
# File 'lib/bullet.rb', line 85

def reset_whitelist
  @whitelist = {:n_plus_one_query => {}, :unused_eager_loading => {}, :counter_cache => {}}
end

.start?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/bullet.rb', line 134

def start?
  Thread.current[:bullet_start]
end

.start_requestObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bullet.rb', line 104

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

.unused_eager_loading_enable?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/bullet.rb', line 64

def unused_eager_loading_enable?
  self.enable? && !!@unused_eager_loading_enable
end

.warningsObject



171
172
173
174
175
176
177
178
# File 'lib/bullet.rb', line 171

def warnings
  notification_collector.collection.inject({}) do |warnings, notification|
    warning_type = notification.class.to_s.split(':').last.tableize
    warnings[warning_type] ||= []
    warnings[warning_type] << notification
    warnings
  end
end