From a875b1c44a5674d924d639f0bb4d9bec14b86d29 Mon Sep 17 00:00:00 2001
From: Wojciech Kocjan <wojciech@kocjan.org>
Date: Thu, 4 Apr 2013 00:26:47 +0200
Subject: [PATCH] Store base URI in Discourse.BaseUri and use it in all places
 where rootURL was previously used

---
 app/assets/javascripts/discourse.js                |  4 +---
 app/assets/javascripts/discourse/components/url.js | 11 ++---------
 .../discourse/routes/discourse_location.js         | 14 +++-----------
 app/views/common/_discourse_javascript.html.erb    |  1 +
 lib/discourse.rb                                   |  4 ++--
 5 files changed, 9 insertions(+), 25 deletions(-)

diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js
index d537d42879c..e9ee419a4fe 100644
--- a/app/assets/javascripts/discourse.js
+++ b/app/assets/javascripts/discourse.js
@@ -22,10 +22,8 @@ Discourse = Ember.Application.createWithMixins({
   // The highest seen post number by topic
   highestSeenByTopic: {},
 
-  rootURL: '/',
-
   getURL: function(url) {
-    var u = this.get('rootURL');
+    var u = (Discourse.BaseUri === undefined ? "/" : Discourse.BaseUri);
     if (u[u.length-1] === '/') {
       u = u.substring(0, u.length-1);
     }
diff --git a/app/assets/javascripts/discourse/components/url.js b/app/assets/javascripts/discourse/components/url.js
index 0aec6544e6d..4191b874006 100644
--- a/app/assets/javascripts/discourse/components/url.js
+++ b/app/assets/javascripts/discourse/components/url.js
@@ -13,14 +13,6 @@ Discourse.URL = {
   // Used for matching a /more URL
   MORE_REGEXP: /\/more$/,
 
-  /**
-    Will be pre-pended to path upon state change
-
-    @property rootURL
-    @default '/'
-  */
-  rootURL: '/',
-
   /**
     @private
 
@@ -73,7 +65,8 @@ Discourse.URL = {
       If the URL is absolute, remove rootURL
      */
     if (path.match(/^\//)) {
-      var rootURL = this.rootURL.replace(/\/$/, '');
+      var rootURL = (Discourse.BaseUri === undefined ? "/" : Discourse.BaseUri);
+      rootURL = rootURL.replace(/\/$/, '');
       path = path.replace(rootURL, '');
     }
 
diff --git a/app/assets/javascripts/discourse/routes/discourse_location.js b/app/assets/javascripts/discourse/routes/discourse_location.js
index e01c1d63619..82dcd003167 100644
--- a/app/assets/javascripts/discourse/routes/discourse_location.js
+++ b/app/assets/javascripts/discourse/routes/discourse_location.js
@@ -35,14 +35,6 @@ Ember.DiscourseLocation = Ember.Object.extend({
     set(this, 'history', window.history);
   },
 
-  /**
-    Will be pre-pended to path upon state change
-
-    @property rootURL
-    @default '/'
-  */
-  rootURL: '/',
-
   /**
     @private
 
@@ -51,7 +43,7 @@ Ember.DiscourseLocation = Ember.Object.extend({
     @method getURL
   */
   getURL: function() {
-    var rootURL = get(this, 'rootURL'),
+    var rootURL = (Discourse.BaseUri === undefined ? "/" : Discourse.BaseUri),
         url = get(this, 'location').pathname;
 
     rootURL = rootURL.replace(/\/$/, '');
@@ -154,7 +146,7 @@ Ember.DiscourseLocation = Ember.Object.extend({
         var currentState = self.get('currentState');
         if (currentState) {
           var url = e.state.path,
-              rootURL = get(self, 'rootURL');
+              rootURL = (Discourse.BaseUri === undefined ? "/" : Discourse.BaseUri);
 
           rootURL = rootURL.replace(/\/$/, '');
           url = url.replace(rootURL, '');
@@ -176,7 +168,7 @@ Ember.DiscourseLocation = Ember.Object.extend({
     @param url {String}
   */
   formatURL: function(url) {
-    var rootURL = get(this, 'rootURL');
+    var rootURL = (Discourse.BaseUri === undefined ? "/" : Discourse.BaseUri);
 
     if (url !== '') {
       rootURL = rootURL.replace(/\/$/, '');
diff --git a/app/views/common/_discourse_javascript.html.erb b/app/views/common/_discourse_javascript.html.erb
index 2232cbf9c83..c0c6ead0c4d 100644
--- a/app/views/common/_discourse_javascript.html.erb
+++ b/app/views/common/_discourse_javascript.html.erb
@@ -35,6 +35,7 @@
 <script>
   Discourse.CDN = '<%= Rails.configuration.action_controller.asset_host  %>';
   Discourse.BaseUrl = '<%= RailsMultisite::ConnectionManagement.current_hostname  %>';
+  Discourse.BaseUri = '<%= Discourse::base_uri "/" %>';
   Discourse.Environment = '<%= Rails.env %>';
   Discourse.Router.map(function() {
     return Discourse.routeBuilder.call(this);
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 1156b86b442..b1b2b9cc620 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -18,11 +18,11 @@ module Discourse
     RailsMultisite::ConnectionManagement.current_hostname
   end
 
-  def self.base_uri
+  def self.base_uri default_value=""
     if !ActionController::Base.config.relative_url_root.blank?
       return ActionController::Base.config.relative_url_root 
     else
-      return ""
+      return default_value
     end
   end