diff --git a/app/helpers/theme_helper.rb b/app/helpers/theme_helper.rb index b0f1649f99..1e7f8489be 100644 --- a/app/helpers/theme_helper.rb +++ b/app/helpers/theme_helper.rb @@ -47,9 +47,15 @@ module ThemeHelper end def current_theme - return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme + available_themes = Themes.instance.names - current_user.setting_theme + user_theme = current_user&.setting_theme + return user_theme if user_theme && available_themes.include?(user_theme) + + site_theme = Setting.theme + return site_theme if available_themes.include?(site_theme) + + 'default' # Fallback end def color_scheme diff --git a/spec/helpers/theme_helper_spec.rb b/spec/helpers/theme_helper_spec.rb index 4c8702af74..b6b50a7bfa 100644 --- a/spec/helpers/theme_helper_spec.rb +++ b/spec/helpers/theme_helper_spec.rb @@ -109,10 +109,19 @@ RSpec.describe ThemeHelper do end context 'when theme is changed in settings' do - before { Setting.theme = 'contrast' } + before do + allow(Themes.instance).to receive(:names).and_return(%w(default contrast)) + Setting.theme = 'contrast' + end it { is_expected.to eq('contrast') } end + + context 'when theme is changed to invalid value' do + before { Setting.theme = 'fakethemename' } + + it { is_expected.to eq('default') } + end end context 'when user is signed in' do