/
bullet.rb
71 lines (58 loc) · 2.01 KB
/
bullet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'bulletware'
module Bullet
class <<self
attr_accessor :enable, :alert, :console, :growl, :growl_password, :rails_logger, :bullet_logger, :logger, :logger_file
def enable=(enable)
@enable = enable
if enable?
Bullet::ActiveRecord.enable
ActionController::Dispatcher.middleware.use Bulletware
end
end
def enable?
@enable == true
end
def growl=(growl)
if growl
begin
require 'ruby-growl'
growl = Growl.new('localhost', 'ruby-growl', ['Bullet Notification'], nil, @@growl_password)
growl.notify('Bullet Notification', 'Bullet Notification', 'Bullet Growl notifications have been turned on')
rescue MissingSourceFile
raise NotificationError.new('You must install the ruby-growl gem to use Growl notifications: `sudo gem install ruby-growl`')
end
end
@growl = growl
end
def bullet_logger=(bullet_logger)
if @bullet_logger = bullet_logger
@logger_file = File.open(Bullet::BulletLogger::LOG_FILE, 'a+')
@logger = Bullet::BulletLogger.new(@logger_file)
end
end
BULLETS = [Bullet::Association, Bullet::Counter]
def start_request
BULLETS.each {|bullet| bullet.start_request}
end
def end_request
BULLETS.each {|bullet| bullet.end_request}
end
def notification?
BULLETS.any? {|bullet| bullet.notification?}
end
def javascript_notification
BULLETS.collect {|bullet| bullet.javascript_notification if bullet.notification?}.join("\n")
end
def growl_notification
BULLETS.each {|bullet| bullet.growl_notification if bullet.notification?}
end
def log_notification(path)
BULLETS.each {|bullet| bullet.log_notification(path) if bullet.notification?}
end
end
autoload :ActiveRecord, 'bullet/active_record'
autoload :Association, 'bullet/association'
autoload :Counter, 'bullet/counter'
autoload :BulletLogger, 'bullet/logger'
autoload :Notification, 'bullet/notification'
end