From 719a93c312b9caa6c71de22d67f1ce1a78c1c8b2 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Wed, 21 Aug 2019 16:50:47 +1000 Subject: [PATCH] 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 ``` --- app/models/theme.rb | 10 ++++++++++ lib/stylesheet/importer.rb | 1 + spec/models/theme_spec.rb | 12 ++++++++++++ 3 files changed, 23 insertions(+) diff --git a/app/models/theme.rb b/app/models/theme.rb index d5d855434be..a307d3ac05a 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 7258a99978f..d06b4dbf0d7 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 d760a5fc207..fb47cfb50df 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