FEATURE: treat theme_uploads as settings in JavaScript

FEATURE: treat theme_uploads as settings in JavaScript

This change allows themes and components access to theme assets.

This means that inside theme js you can now get the URL for an asset with:

settings.theme_uploads.name
diff --git a/app/models/theme.rb b/app/models/theme.rb
index d5d8554..a307d3a 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -430,6 +430,16 @@ class Theme < ActiveRecord::Base
       self.settings.each do |setting|
         hash[setting.name] = setting.value
       end
+
+      theme_uploads = {}
+      theme_fields
+        .joins(:upload)
+        .where(type_id: ThemeField.types[:theme_upload_var]).each do |field|
+
+        theme_uploads[field.name] = field.upload.url
+      end
+      hash['theme_uploads'] = theme_uploads if theme_uploads.present?
+
       hash
     end
   end
diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rb
index 7258a99..d06b4db 100644
--- a/lib/stylesheet/importer.rb
+++ b/lib/stylesheet/importer.rb
@@ -63,6 +63,7 @@ module Stylesheet
       end
 
       theme&.included_settings&.each do |name, value|
+        next if name == "theme_uploads"
         contents << to_scss_variable(name, value)
       end
 
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index d760a5f..fb47cfb 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -546,6 +546,18 @@ HTML
     expect(messages.first.data.map { |d| d[:target] }).to contain_exactly(:admin, :desktop, :desktop_theme, :mobile, :mobile_theme)
   end
 
+  it 'includes theme_uploads in settings' do
+    Theme.destroy_all
+
+    upload = Fabricate(:upload)
+    theme.set_field(type: :theme_upload_var, target: :common, name: "bob", upload_id: upload.id)
+    theme.save!
+
+    json = JSON.parse(cached_settings(theme.id))
+
+    expect(json["theme_uploads"]["bob"]).to eq(upload.url)
+  end
+
   it 'handles settings cache correctly' do
     Theme.destroy_all

GitHub sha: 719a93c3

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there: