FIX: When running the wizard and using a custom theme, fallback to the color_scheme name if the base_scheme_id is nil (#8236)

This commit is contained in:
Roman Rizzi 2019-10-25 09:29:51 -03:00 committed by GitHub
parent 5ae35f9906
commit 070a3dcf9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 8 deletions

View File

@ -187,6 +187,10 @@ class ColorScheme < ActiveRecord::Base
@base_color_scheme
end
def self.is_base?(scheme_name)
base_color_scheme_colors.map { |c| c[:id] }.include?(scheme_name)
end
# create_from_base will create a new ColorScheme that overrides Discourse's base color scheme with the given colors.
def self.create_from_base(params)
new_color_scheme = new(name: params[:name])

View File

@ -146,12 +146,10 @@ class Wizard
default_theme = Theme.find_by(id: SiteSetting.default_theme_id)
default_theme_override = SiteSetting.exists?(name: "default_theme_id")
scheme_id =
if default_theme_override
default_theme&.color_scheme&.base_scheme_id
else
ColorScheme::LIGHT_THEME_ID
end
base_scheme = default_theme&.color_scheme&.base_scheme_id
color_scheme_name = default_theme&.color_scheme&.name
scheme_id = default_theme_override ? (base_scheme || color_scheme_name) : ColorScheme::LIGHT_THEME_ID
themes = step.add_field(
id: 'theme_previews',
@ -160,6 +158,14 @@ class Wizard
value: scheme_id
)
# fix for the case when base_scheme is nil
if scheme_id && default_theme_override && base_scheme.nil?
scheme = default_theme.color_scheme
default_colors = scheme.colors.select(:name, :hex)
choice_hash = default_colors.reduce({}) { |choice, color| choice[color.name] = "##{color.hex}"; choice }
themes.add_choice(scheme_id, data: { colors: choice_hash })
end
ColorScheme.base_color_scheme_colors.each do |t|
with_hash = t[:colors].dup
with_hash.map { |k, v| with_hash[k] = "##{v}" }
@ -172,7 +178,7 @@ class Wizard
ColorScheme::LIGHT_THEME_ID
)
next unless scheme_name.present?
next unless scheme_name.present? && ColorScheme.is_base?(scheme_name)
name = I18n.t("color_schemes.#{scheme_name.downcase.gsub(' ', '_')}_theme_name")

View File

@ -162,7 +162,21 @@ describe Wizard::Builder do
end
end
describe "when the default them hass been override" do
describe "when the default theme has been override and the color scheme doesn't have a base scheme" do
let(:color_scheme) { Fabricate(:color_scheme, base_scheme_id: nil) }
before do
SiteSetting.default_theme_id = theme.id
theme.update(color_scheme: color_scheme)
end
it 'fallbacks to the color scheme name' do
expect(field.required).to eq(false)
expect(field.value).to eq(color_scheme.name)
end
end
describe "when the default theme has been override" do
before do
theme.set_default!
end