FIX: Fix broken theme field URLs. (#6622)

FIX: Fix broken theme field URLs. (#6622)

From 172b3bf4d3479cf9361c15f9ef3c05638661ffaa Mon Sep 17 00:00:00 2001
From: Bianca Nenciu <nbianca@users.noreply.github.com>
Date: Fri, 23 Nov 2018 17:11:05 +0200
Subject: [PATCH] FIX: Fix broken theme field URLs. (#6622)


diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6
index d5cb75b..7c7d92b 100644
--- a/app/assets/javascripts/discourse/models/category.js.es6
+++ b/app/assets/javascripts/discourse/models/category.js.es6
@@ -47,9 +47,9 @@ const Category = RestModel.extend({
     return Discourse.getURL("/c/") + Category.slugFor(this);
   },
 
-  @computed("url")
-  fullSlug(url) {
-    return url.slice(3).replace("/", "-");
+  @computed
+  fullSlug() {
+    return Category.slugFor(this).replace(/\//g, "-");
   },
 
   @computed("name")
diff --git a/app/models/category.rb b/app/models/category.rb
index d494d42..c7c1caf 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -535,7 +535,8 @@ class Category < ActiveRecord::Base
   end
 
   def full_slug(separator = "-")
-    url[3..-1].gsub("/", separator)
+    start_idx = "#{Discourse.base_uri}/c/".length
+    url[start_idx..-1].gsub("/", separator)
   end
 
   def url
diff --git a/lib/global_path.rb b/lib/global_path.rb
index e7f89c0..cea5985 100644
--- a/lib/global_path.rb
+++ b/lib/global_path.rb
@@ -4,7 +4,7 @@ module GlobalPath
   end
 
   def cdn_path(p)
-    "#{GlobalSetting.cdn_url}#{path(p)}"
+    GlobalSetting.cdn_url.blank? ? p : "#{GlobalSetting.cdn_url}#{path(p)}"
   end
 
   def upload_cdn_path(p)
diff --git a/spec/components/stylesheet/importer_spec.rb b/spec/components/stylesheet/importer_spec.rb
index 3b65ec3..60cd2c2 100644
--- a/spec/components/stylesheet/importer_spec.rb
+++ b/spec/components/stylesheet/importer_spec.rb
@@ -15,7 +15,7 @@ describe Stylesheet::Importer do
 
     expect(compile_css("category_backgrounds")).to include("body.category-#{category.full_slug}{background-image:url(#{background.url})}")
 
-    GlobalSetting.expects(:cdn_url).returns("//awesome.cdn")
+    GlobalSetting.stubs(:cdn_url).returns("//awesome.cdn")
     expect(compile_css("category_backgrounds")).to include("body.category-#{category.full_slug}{background-image:url(//awesome.cdn#{background.url})}")
   end
 
@@ -33,4 +33,30 @@ describe Stylesheet::Importer do
     expect(compile_css("category_backgrounds")).to include("body.category-#{category.full_slug}{background-image:url(https://s3.cdn/original")
   end
 
+  context "#theme_variables" do
+
+    let(:theme) { Fabricate(:theme) }
+
+    let(:importer) { described_class.new(theme: theme) }
+
+    let(:upload) { Fabricate(:upload) }
+    let(:upload_s3) { Fabricate(:upload_s3) }
+
+    let(:theme_field) { ThemeField.create!(theme: theme, target_id: 0, name: "var", upload: upload, value: "", type_id: ThemeField.types[:theme_upload_var]) }
+    let(:theme_field_s3) { ThemeField.create!(theme: theme, target_id: 1, name: "var_s3", upload: upload_s3, value: "", type_id: ThemeField.types[:theme_upload_var]) }
+
+    it "should contain the URL" do
+      theme_field.save!
+      import = importer.imports("theme_variables", nil)
+      expect(import.source).to include(upload.url)
+    end
+
+    it "should contain the S3 URL" do
+      theme_field_s3.save!
+      import = importer.imports("theme_variables", nil)
+      expect(import.source).to include(upload_s3.url)
+    end
+
+  end
+
 end

GitHub