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
```
This commit is contained in:
Sam Saffron 2019-08-21 16:50:47 +10:00
parent 038bf02e33
commit 719a93c312
3 changed files with 23 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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