Skip to content

A platform for community discussion. Free, open, simple.

FIX: Imported themes should set their color scheme automatically

David Taylor43f3bf71ba93
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 9e8f3ef7cd7..363656c6426 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -177,6 +177,7 @@ class RemoteTheme < ActiveRecord::Base
def update_theme_color_schemes(theme, schemes)
missing_scheme_names = Hash[*theme.color_schemes.pluck(:name, :id).flatten]
+ ordered_schemes = []
schemes&.each do |name, colors|
missing_scheme_names.delete(name)
@@ -189,12 +190,14 @@ class RemoteTheme < ActiveRecord::Base
theme.notify_color_change(c)
end
end
+ ordered_schemes << existing
else
scheme = theme.color_schemes.build(name: name)
ColorScheme.base.colors_hashes.each do |color|
override = normalize_override(colors[color[:name]])
scheme.color_scheme_colors << ColorSchemeColor.new(name: color[:name], hex: override || color[:hex])
end
+ ordered_schemes << scheme
end
end
@@ -202,6 +205,10 @@ class RemoteTheme < ActiveRecord::Base
ColorScheme.where(id: missing_scheme_names.values).delete_all
# we may have stuff pointed at the incorrect scheme?
end
+
+ if theme.new_record?
+ theme.color_scheme = ordered_schemes.first
+ end
end
def github_diff_link
diff --git a/spec/models/remote_theme_spec.rb b/spec/models/remote_theme_spec.rb
index 1e9f23c98a4..42e85dc2625 100644
--- a/spec/models/remote_theme_spec.rb
+++ b/spec/models/remote_theme_spec.rb
@@ -102,6 +102,9 @@ describe RemoteTheme do
expect(scheme.name).to eq("Amazing")
expect(scheme.colors.find_by(name: 'love').hex).to eq('fafafa')
+ expect(@theme.color_scheme_id).to eq(scheme.id)
+ @theme.update(color_scheme_id: nil)
+
File.write("#{initial_repo}/common/header.html", "I AM UPDATED")
File.write("#{initial_repo}/about.json", about_json(love_color: "EAEAEA", about_url: "https://newsite.com/about"))
@@ -125,6 +128,7 @@ describe RemoteTheme do
scheme = ColorScheme.find_by(theme_id: @theme.id)
expect(scheme.name).to eq("Amazing")
expect(scheme.colors.find_by(name: 'love').hex).to eq('eaeaea')
+ expect(@theme.color_scheme_id).to eq(nil) # Should only be set on first import
mapped = Hash[*@theme.theme_fields.map { |f| ["#{f.target_id}-#{f.name}", f.value] }.flatten]