FIX: Remove default val for colors step if a default theme has been set.

Running through the wizard after a default theme has been configured
will always revert the default theme to the light theme.
This commit is contained in:
Guo Xiang Tan 2019-05-09 17:22:28 +08:00
parent d110f252bb
commit 4e91839c97
4 changed files with 74 additions and 6 deletions

View File

@ -101,6 +101,8 @@ class ColorScheme < ActiveRecord::Base
# rubocop:enable Layout/AlignHash
LIGHT_THEME_ID = 'Light'
def self.base_color_scheme_colors
base_with_hash = {}
@ -109,7 +111,7 @@ class ColorScheme < ActiveRecord::Base
end
list = [
{ id: 'Light', colors: base_with_hash }
{ id: LIGHT_THEME_ID, colors: base_with_hash }
]
CUSTOM_SCHEMES.each do |k, v|

View File

@ -147,9 +147,22 @@ class Wizard
@wizard.append_step('colors') do |step|
default_theme = Theme.find_by(id: SiteSetting.default_theme_id)
scheme_id = default_theme&.color_scheme&.base_scheme_id || 'Light'
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
themes = step.add_field(
id: 'theme_previews',
type: 'component',
required: !default_theme_override,
value: scheme_id
)
themes = step.add_field(id: 'theme_previews', type: 'component', required: true, value: scheme_id)
ColorScheme.base_color_scheme_colors.each do |t|
with_hash = t[:colors].dup
with_hash.map { |k, v| with_hash[k] = "##{v}" }
@ -157,7 +170,13 @@ class Wizard
end
step.on_update do |updater|
scheme_name = updater.fields[:theme_previews] || 'Light'
scheme_name = (
(updater.fields[:theme_previews] || "") ||
ColorScheme::LIGHT_THEME_ID
)
next unless scheme_name.present?
name = I18n.t("color_schemes.#{scheme_name.downcase.gsub(' ', '_')}_theme_name")
theme = nil

View File

@ -183,6 +183,20 @@ describe Wizard::StepUpdater do
end
end
context "with an existing default theme" do
fab!(:theme) { Fabricate(:theme) }
before do
theme.set_default!
end
it "should not update the default theme when no option has been selected" do
expect do
wizard.create_updater('colors', {}).update
end.to_not change { SiteSetting.default_theme_id }
end
end
context "without an existing theme" do
before do
Theme.delete_all
@ -203,14 +217,19 @@ describe Wizard::StepUpdater do
context 'light theme' do
it "creates the theme" do
updater = wizard.create_updater('colors', {})
updater = wizard.create_updater('colors',
theme_previews: ColorScheme::LIGHT_THEME_ID
)
expect { updater.update }.to change { Theme.count }.by(1)
theme = Theme.last
expect(theme.user_id).to eq(wizard.user.id)
expect(theme.color_scheme).to eq(ColorScheme.find_by(name: 'Light'))
expect(theme.color_scheme).to eq(ColorScheme.find_by(name:
ColorScheme::LIGHT_THEME_ID
))
end
end
end

View File

@ -145,4 +145,32 @@ describe Wizard::Builder do
expect(login_required_field.id).to eq('privacy')
end
end
context "colors step" do
fab!(:theme) { Fabricate(:theme) }
let(:colors_step) { wizard.steps.find { |s| s.id == 'colors' } }
let(:field) { colors_step.fields.first }
describe "when the default theme has not been override" do
before do
SiteSetting.find_by(name: "default_theme_id").destroy!
end
it 'should set the right default values' do
expect(field.required).to eq(true)
expect(field.value).to eq(ColorScheme::LIGHT_THEME_ID)
end
end
describe "when the default them hass been override" do
before do
theme.set_default!
end
it 'should set the right default values' do
expect(field.required).to eq(false)
expect(field.value).to eq(nil)
end
end
end
end