lib/bullet.rb in bullet-2.0.0.beta.2 vs lib/bullet.rb in bullet-2.0.0.beta.3

- old
+ new

@@ -1,87 +1,118 @@ -require 'bulletware' +require 'set' module Bullet + class NotificationError < StandardError; end + if Rails.version =~ /^3.0/ autoload :ActiveRecord, 'bullet/active_record3' else autoload :ActiveRecord, 'bullet/active_record2' autoload :ActionController, 'bullet/action_controller2' end - autoload :Association, 'bullet/association' - autoload :Counter, 'bullet/counter' + autoload :Rack, 'bullet/rack' autoload :BulletLogger, 'bullet/logger' autoload :Notification, 'bullet/notification' + autoload :Presenter, 'bullet/presenter' + autoload :Detector, 'bullet/detector' + autoload :Registry, 'bullet/registry' + autoload :NotificationCollector, 'bullet/notification_collector' + + if defined? Rails::Railtie + # compatible with rails 3.0.0.beta4 + class BulletRailtie < Rails::Railtie + initializer "bullet.configure_rails_initialization" do |app| + app.middleware.use Bullet::Rack + end + end + end class <<self - attr_accessor :enable, :alert, :console, :growl, :growl_password, :rails_logger, :bullet_logger, :logger, :logger_file, :disable_browser_cache + attr_accessor :enable, :alert, :console, :growl, :growl_password, :rails_logger, :bullet_logger, :disable_browser_cache, :xmpp + attr_reader :notification_collector + + DETECTORS = [ Bullet::Detector::NPlusOneQuery, + Bullet::Detector::UnusedEagerAssociation, + Bullet::Detector::Counter ] + PRESENTERS = [ Bullet::Presenter::JavascriptAlert, + Bullet::Presenter::JavascriptConsole, + Bullet::Presenter::Growl, + Bullet::Presenter::Xmpp, + Bullet::Presenter::RailsLogger, + Bullet::Presenter::BulletLogger ] + def enable=(enable) @enable = enable if enable? Bullet::ActiveRecord.enable - if Rails.version =~ /^3.0/ - require 'action_controller/metal' - ::ActionController::Metal.middleware_stack.use Bulletware - elsif Rails.version =~/^2.3/ + if Rails.version =~ /^2./ Bullet::ActionController.enable - require 'action_controller/dispatcher' - ::ActionController::Dispatcher.middleware.use Bulletware end 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 + Bullet::Presenter::Growl.setup_connection( self.growl_password ) if growl end + def xmpp=(xmpp) + Bullet::Presenter::Xmpp.setup_connection( xmpp ) if xmpp + 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 + Bullet::Presenter::BulletLogger.setup if bullet_logger end - BULLETS = [Bullet::Association, Bullet::Counter] - def start_request - BULLETS.each {|bullet| bullet.start_request} + @notification_collector ||= Bullet::NotificationCollector.new + @notification_collector.reset + DETECTORS.each {|bullet| bullet.start_request} end def end_request - BULLETS.each {|bullet| bullet.end_request} + DETECTORS.each {|bullet| bullet.end_request} end def clear - BULLETS.each {|bullet| bullet.clear} + DETECTORS.each {|bullet| bullet.clear} end - def notification? - BULLETS.any? {|bullet| bullet.notification?} + def active_presenters + PRESENTERS.select { |presenter| presenter.send :active? } end - def javascript_notification - BULLETS.collect {|bullet| bullet.javascript_notification if bullet.notification?}.join("\n") + def notification? + Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations + @notification_collector.notifications_present? end - def growl_notification - BULLETS.each {|bullet| bullet.growl_notification if bullet.notification?} + def gather_inline_notifications + responses = [] + for_each_active_presenter_with_notification do |notification| + responses << notification.present_inline + end + responses.join( "\n" ) end - def log_notification(path) - BULLETS.each {|bullet| bullet.log_notification(path) if bullet.notification?} + def perform_out_of_channel_notifications + for_each_active_presenter_with_notification do |notification| + notification.present_out_of_channel + end end + + private + def for_each_active_presenter_with_notification + active_presenters.each do |presenter| + @notification_collector.collection.each do |notification| + notification.presenter = presenter + yield notification + end + end + end end -end + +end \ No newline at end of file