timeline_preview
show_staff_badge
bootstrap_timeline_accounts
+ theme
thumbnail
hero
min_invite_role
end
def current_theme
- return Setting.default_settings['theme'] unless Themes.instance.names.include? current_user&.setting_theme
+ return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme
current_user.setting_theme
end
module Settings
class ScopedSettings
+ DEFAULTING_TO_UNSCOPED = %w(
+ theme
+ ).freeze
+
def initialize(object)
@object = object
end
Rails.cache.fetch(Setting.cache_key(key, @object)) do
db_val = thing_scoped.find_by(var: key.to_s)
if db_val
- default_value = Setting.default_settings[key]
+ default_value = ScopedSettings.default_settings[key]
return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash)
db_val.value
else
- Setting.default_settings[key]
+ ScopedSettings.default_settings[key]
end
end
end
+ class << self
+ def default_settings
+ defaulting = DEFAULTING_TO_UNSCOPED.map { |k| [k, Setting[k]] }.to_h
+ Setting.default_settings.merge!(defaulting)
+ end
+ end
+
protected
def thing_scoped
:show_staff_badge=,
:bootstrap_timeline_accounts,
:bootstrap_timeline_accounts=,
+ :theme,
+ :theme=,
:min_invite_role,
:min_invite_role=,
:activity_api_enabled,
%hr/
.fields-group
+ = f.input :theme, collection: Themes.instance.names, label_method: lambda { |theme| I18n.t("themes.#{theme}", default: theme) }, wrapper: :with_label, include_blank: false
= f.input :thumbnail, as: :file, wrapper: :with_block_label, label: t('admin.settings.thumbnail.title'), hint: t('admin.settings.thumbnail.desc_html')
= f.input :hero, as: :file, wrapper: :with_block_label, label: t('admin.settings.hero.title'), hint: t('admin.settings.hero.desc_html')
end
end
+ describe 'helper_method :current_theme' do
+ it 'returns "default" when theme wasn\'t changed in admin settings' do
+ allow(Setting).to receive(:default_settings).and_return({'theme' => 'default'})
+
+ expect(controller.view_context.current_theme).to eq 'default'
+ end
+
+ it 'returns instances\'s theme when user is not signed in' do
+ allow(Setting).to receive(:[]).with('theme').and_return 'contrast'
+
+ expect(controller.view_context.current_theme).to eq 'contrast'
+ end
+
+ it 'returns instances\'s default theme when user didn\'t set theme' do
+ current_user = Fabricate(:user)
+ sign_in current_user
+
+ allow(Setting).to receive(:[]).with('theme').and_return 'contrast'
+
+ expect(controller.view_context.current_theme).to eq 'contrast'
+ end
+
+ it 'returns user\'s theme when it is set' do
+ current_user = Fabricate(:user)
+ current_user.settings['theme'] = 'mastodon-light'
+ sign_in current_user
+
+ allow(Setting).to receive(:[]).with('theme').and_return 'contrast'
+
+ expect(controller.view_context.current_theme).to eq 'mastodon-light'
+ end
+ end
+
context 'ActionController::RoutingError' do
subject do
routes.draw { get 'routing_error' => 'anonymous#routing_error' }