Clean up settings/preferences controller (#2237)
* Add missing fields group on preferences page * Clean up settings/preferences controller * Extract a UserSettingsDecorator
This commit is contained in:
@ -8,25 +8,9 @@ class Settings::PreferencesController < ApplicationController
|
||||
def show; end
|
||||
|
||||
def update
|
||||
current_user.settings['notification_emails'] = {
|
||||
follow: user_params[:notification_emails][:follow] == '1',
|
||||
follow_request: user_params[:notification_emails][:follow_request] == '1',
|
||||
reblog: user_params[:notification_emails][:reblog] == '1',
|
||||
favourite: user_params[:notification_emails][:favourite] == '1',
|
||||
mention: user_params[:notification_emails][:mention] == '1',
|
||||
digest: user_params[:notification_emails][:digest] == '1',
|
||||
}
|
||||
user_settings.update(user_settings_params.to_h)
|
||||
|
||||
current_user.settings['interactions'] = {
|
||||
must_be_follower: user_params[:interactions][:must_be_follower] == '1',
|
||||
must_be_following: user_params[:interactions][:must_be_following] == '1',
|
||||
}
|
||||
|
||||
current_user.settings['default_privacy'] = user_params[:setting_default_privacy]
|
||||
current_user.settings['boost_modal'] = user_params[:setting_boost_modal] == '1'
|
||||
current_user.settings['auto_play_gif'] = user_params[:setting_auto_play_gif] == '1'
|
||||
|
||||
if current_user.update(user_params.except(:notification_emails, :interactions, :setting_default_privacy, :setting_boost_modal, :setting_auto_play_gif))
|
||||
if current_user.update(user_params)
|
||||
redirect_to settings_preferences_path, notice: I18n.t('generic.changes_saved_msg')
|
||||
else
|
||||
render :show
|
||||
@ -35,7 +19,23 @@ class Settings::PreferencesController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def user_settings
|
||||
UserSettingsDecorator.new(current_user)
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(:locale, :setting_default_privacy, :setting_boost_modal, :setting_auto_play_gif, notification_emails: [:follow, :follow_request, :reblog, :favourite, :mention, :digest], interactions: [:must_be_follower, :must_be_following])
|
||||
params.require(:user).permit(
|
||||
:locale
|
||||
)
|
||||
end
|
||||
|
||||
def user_settings_params
|
||||
params.require(:user).permit(
|
||||
:setting_default_privacy,
|
||||
:setting_boost_modal,
|
||||
:setting_auto_play_gif,
|
||||
notification_emails: %i(follow follow_request reblog favourite mention digest),
|
||||
interactions: %i(must_be_follower must_be_following)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
56
app/lib/user_settings_decorator.rb
Normal file
56
app/lib/user_settings_decorator.rb
Normal file
@ -0,0 +1,56 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class UserSettingsDecorator
|
||||
attr_reader :user, :settings
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def update(settings)
|
||||
@settings = settings
|
||||
process_update
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def process_update
|
||||
user.settings['notification_emails'] = merged_notification_emails
|
||||
user.settings['interactions'] = merged_interactions
|
||||
user.settings['default_privacy'] = default_privacy_preference
|
||||
user.settings['boost_modal'] = boost_modal_preference
|
||||
user.settings['auto_play_gif'] = auto_play_gif_preference
|
||||
end
|
||||
|
||||
def merged_notification_emails
|
||||
user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
|
||||
end
|
||||
|
||||
def merged_interactions
|
||||
user.settings['interactions'].merge coerced_settings('interactions').to_h
|
||||
end
|
||||
|
||||
def default_privacy_preference
|
||||
settings['setting_default_privacy']
|
||||
end
|
||||
|
||||
def boost_modal_preference
|
||||
boolean_cast_setting 'setting_boost_modal'
|
||||
end
|
||||
|
||||
def auto_play_gif_preference
|
||||
boolean_cast_setting 'setting_auto_play_gif'
|
||||
end
|
||||
|
||||
def boolean_cast_setting(key)
|
||||
settings[key] == '1'
|
||||
end
|
||||
|
||||
def coerced_settings(key)
|
||||
coerce_values settings.fetch(key, {})
|
||||
end
|
||||
|
||||
def coerce_values(params_hash)
|
||||
params_hash.transform_values { |x| x == '1' }
|
||||
end
|
||||
end
|
@ -18,9 +18,10 @@
|
||||
= ff.input :mention, as: :boolean, wrapper: :with_label
|
||||
= ff.input :digest, as: :boolean, wrapper: :with_label
|
||||
|
||||
= f.simple_fields_for :interactions, hash_to_object(current_user.settings.interactions) do |ff|
|
||||
= ff.input :must_be_follower, as: :boolean, wrapper: :with_label
|
||||
= ff.input :must_be_following, as: :boolean, wrapper: :with_label
|
||||
.fields-group
|
||||
= f.simple_fields_for :interactions, hash_to_object(current_user.settings.interactions) do |ff|
|
||||
= ff.input :must_be_follower, as: :boolean, wrapper: :with_label
|
||||
= ff.input :must_be_following, as: :boolean, wrapper: :with_label
|
||||
|
||||
.fields-group
|
||||
= f.input :setting_boost_modal, as: :boolean, wrapper: :with_label
|
||||
|
Reference in New Issue
Block a user