Permalink
Browse files

Refresh the rack_bug toolbar with ajax request information (on each a…

…jax request) if using Rails/JQuery
  • Loading branch information...
jrallison committed Mar 26, 2009
1 parent 2cd2fc3 commit 50aeb20bc74f441a5ac5d701e4941df54e43d220
Showing with 49 additions and 3 deletions.
  1. +15 −1 lib/rack/bug/public/__rack_bug__/bug.js
  2. +34 −2 lib/rack/bug/toolbar.rb
@@ -3,7 +3,7 @@ jQuery.noConflict();
jQuery(function($) {
$.rackBug = function(data, klass) {
$.rackBug.init();
- }
+ };
$.extend($.rackBug, {
init: function() {
var current = null;
@@ -37,6 +37,20 @@ jQuery(function($) {
$(document).trigger('close.rackBug');
return false;
});
+
+ // Need to clean this up so we don't have to unbind ajaxComplete
+ // Should be able to replace toolbars without reloading this javascript file
+ $('body').unbind("ajaxComplete");
+ $('body').bind("ajaxComplete", function(e, request) {
+ if (request.getResponseHeader("__Rack_Bug__")) {
+ $.ajax({
+ type: "GET",
+ url: "/__rack_bug__/update_toolbar?key=" + request.getResponseHeader("__Rack_Bug__"),
+ dataType: "script",
+ global: false
+ });
+ }
+ });
},
open: function() {
$(document).bind('keydown.rackBug', function(e) {
View
@@ -31,9 +31,10 @@ class Toolbar
include Render
MIME_TYPES = ["text/html", "application/xhtml+xml"]
+ AJAX_MIME_TYPES = ["text/javascript", "application/json"]
def initialize(app, options = {})
- @app = asset_server(app)
+ @app = Rack::Cascade.new([toolbar_app, asset_server(app)])
initialize_options options
instance_eval(&block) if block_given?
end
@@ -70,6 +71,9 @@ def dispatch
intercept_redirect
elsif modify?
inject_toolbar
+ elsif ajax?
+ store_toolbar
+ inject_toolbar_notification
end
return @response.to_a
@@ -106,10 +110,15 @@ def password_authorized?
def modify?
@response.ok? &&
- @env["X-Requested-With"] != "XMLHttpRequest" &&
+ !ajax? &&
MIME_TYPES.include?(@response.content_type.split(";").first)
end
+ def ajax?
+ @env["X-Requested-With"] == "XMLHttpRequest" ||
+ AJAX_MIME_TYPES.include?(@response.content_type.split(";").first)
+ end
+
def builder
builder = Rack::Builder.new
@@ -122,6 +131,19 @@ def builder
return builder
end
+ class ToolbarApp < Sinatra::Default
+ include Rack::Bug::Render
+
+ get "/__rack_bug__/update_toolbar" do
+ raise "Rails not found... can't read key" unless defined?(Rails)
+ "$('#rack_bug').replaceWith(#{Rails.cache.read(params[:key]).to_json});"
+ end
+ end
+
+ def toolbar_app
+ ToolbarApp.new
+ end
+
def inject_toolbar
full_body = @response.body.join
full_body.sub! /<\/body>/, render + "</body>"
@@ -130,6 +152,16 @@ def inject_toolbar
@response.body = [full_body]
end
+ def store_toolbar
+ return unless defined?(Rails)
+ Rails.cache.write("rack-bug.#{@original_request.path_info}", render)
+ "OK"
+ end
+
+ def inject_toolbar_notification
+ @response["__Rack_Bug__"] = "rack-bug.#{@original_request.path_info}"
+ end
+
def render
render_template("bug", :panels => @env["rack-bug.panels"].reverse)
end

0 comments on commit 50aeb20

Please sign in to comment.