>From e4ae6ffc50438d3ddeb04e847e6f001d8b594108 Mon Sep 17 00:00:00 2001
From: Tomas Sedovic <tsedovic@redhat.com>
Date: Tue, 31 May 2011 17:29:01 +0200
Subject: [PATCH conductor 1/4] Set ajax accept headers explicitly

jQuery doesn't set the proper accept HTTP headers for ajax by default. This
makes sure than the ajax requests are handled by Rails properly.

As such, there is no longer need for the before_filter that always set
format.js for XHR requests so that's removed.

This solves some issues that would prop up once we start using Backbone.js.
---
 src/app/controllers/application_controller.rb   |    8 --------
 src/features/step_definitions/instance_steps.rb |    1 +
 src/lib/viewstate.rb                            |    5 ++++-
 src/public/javascripts/application.js           |   22 ++++++++++++++++++----
 4 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/app/controllers/application_controller.rb b/src/app/controllers/application_controller.rb
index 812cf2d..cb4a1d3 100644
--- a/src/app/controllers/application_controller.rb
+++ b/src/app/controllers/application_controller.rb
@@ -43,8 +43,6 @@ class ApplicationController < ActionController::Base
 
   helper_method :check_privilege
 
-  before_filter :js_for_xhr
-
   protected
   # permissions checking
 
@@ -255,10 +253,4 @@ class ApplicationController < ActionController::Base
     session[:breadcrumbs] = breadcrumbs
   end
 
-  # XMLHTTPRequest in many browsers sends "Accept: */*", so the first respond_to will match.
-  # See http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/ and
-  # http://www.grauw.nl/blog/entry/470 for more.
-  def js_for_xhr
-    request.format = :js if request.xhr?
-  end
 end
diff --git a/src/features/step_definitions/instance_steps.rb b/src/features/step_definitions/instance_steps.rb
index 3fab8e5..8f69cb0 100644
--- a/src/features/step_definitions/instance_steps.rb
+++ b/src/features/step_definitions/instance_steps.rb
@@ -110,6 +110,7 @@ Given /^I accept JSON$/ do
 end
 
 Given /^I request XHR$/ do
+  header 'accept', 'application/javascript'
   header 'X-Requested-With', 'XMLHttpRequest'
 end
 
diff --git a/src/lib/viewstate.rb b/src/lib/viewstate.rb
index bf52e08..d3752fa 100644
--- a/src/lib/viewstate.rb
+++ b/src/lib/viewstate.rb
@@ -147,7 +147,10 @@ module ActionController
 
       if @viewstate and not viewstate_given?
         params[:viewstate] = @viewstate.id
-        redirect_to params and return
+        respond_to do |format|
+          format.html { redirect_to params }
+          format.js { }
+        end
       end
     end
 
diff --git a/src/public/javascripts/application.js b/src/public/javascripts/application.js
index 33c2be5..3e5aac0 100644
--- a/src/public/javascripts/application.js
+++ b/src/public/javascripts/application.js
@@ -84,7 +84,7 @@ var Conductor = {
       $.get($(this).attr("href"), $(this).serialize(),
         function(result) {
           $("#view").html(result);
-        }, "script");
+        });
       return false;
     });
     $("#filter_view").click(function(){
@@ -93,10 +93,22 @@ var Conductor = {
           $("#view").html(result);
           $('#details-selected').hide();
           $('#details-view').tabs('destroy').tabs();
-        }, "script");
+        });
       return false;
     });
-  }
+  },
+
+  setAjaxHeadersForRails: function() {
+    /* In the Rails' respond_to block, there is no distinction between
+       the regular browser request and jQuery AJAX.
+
+       This sets the accept headers for the ajax requests in a way that will
+       match format.js in Rails. Vanilla browser requests still match format.html.
+    */
+    var acceptsSettings = $.extend({}, $.ajaxSettings.accepts)
+    acceptsSettings.html = "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"
+    $.ajaxSetup({accepts: acceptsSettings, dataType: 'html'})
+  },
 };
 
 /* custom methods */
@@ -181,9 +193,11 @@ var Conductor = {
 /* Conductor JS */
 
 $(document).ready(function () {
+  Conductor.setAjaxHeadersForRails();
+
   $(window).scroll(Conductor.positionFooter).resize(Conductor.positionFooter).scroll();
   $("#notification").enhanceInteraction();
   Conductor.enhanceListView();
   Conductor.enhanceDetailsTabs();
   Conductor.bind_pretty_toggle();
-});
\ No newline at end of file
+});
-- 
1.7.4.4

