Sha256: 7b55b9908f4ce2931ae54a4fb0c3e5a78f7ce404988dd3ccde9250acb8e6ca41

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Stopwatch
  class Railtie < Rails::Railtie
    initializer "newplugin.initialize" do |app|
      app.config.middleware.use Rack::LoadSpeed

      # Start processing
      ActiveSupport::Notifications.subscribe "start_processing.action_controller" do |*args|
        Stopwatch::Log.reset_query_count
        Stopwatch::Log.reset_sub_query_count
        Stopwatch::Log.reset_events
      end

      # Every query
      ActiveSupport::Notifications.subscribe "sql.active_record" do |name, start, finish, id, payload|
        if payload[:name] != "CACHE"
          Stopwatch::Log.increment_query_count
          Stopwatch::Log.increment_sub_query_count
        end
      end

      # Every partial render
      ActiveSupport::Notifications.subscribe(/render/) do |name, start, finish, id, payload|
        event = ActiveSupport::Notifications::Event.new(name, start, finish, id, payload)
        stopwatch_event = Stopwatch::Event.new(event)
        stopwatch_event.query_count = Stopwatch::Log.sub_query_count
        Stopwatch::Log.events << stopwatch_event
        Stopwatch::Log.reset_sub_query_count
      end

      # End of processing
      ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
        Stopwatch::Log.event = ActiveSupport::Notifications::Event.new(*args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stopwatch-1.0.1 lib/stopwatch/railtie.rb