diff options
author | Matt Jankowski <mjankowski@thoughtbot.com> | 2017-04-20 21:26:52 -0400 |
---|---|---|
committer | Eugen <eugen@zeonfederated.com> | 2017-04-21 03:26:52 +0200 |
commit | 2dda356e3fc7000ea630b802588cb91df296a89f (patch) | |
tree | 0a0b05f9d196fbc9706e15af9a5f950d31a0bdd7 /app | |
parent | 972f6bc861affd9bc40181492833108f905a04b6 (diff) |
Clean up settings/preferences controller (#2237)
* Add missing fields group on preferences page * Clean up settings/preferences controller * Extract a UserSettingsDecorator
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/settings/preferences_controller.rb | 40 | ||||
-rw-r--r-- | app/lib/user_settings_decorator.rb | 56 | ||||
-rw-r--r-- | app/views/settings/preferences/show.html.haml | 7 |
3 files changed, 80 insertions, 23 deletions
diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 31a204420..6762faee4 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -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', - } - - 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)) + user_settings.update(user_settings_params.to_h) + + 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 diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb new file mode 100644 index 000000000..6104773ad --- /dev/null +++ b/app/lib/user_settings_decorator.rb @@ -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 diff --git a/app/views/settings/preferences/show.html.haml b/app/views/settings/preferences/show.html.haml index ce3929629..d009e51ec 100644 --- a/app/views/settings/preferences/show.html.haml +++ b/app/views/settings/preferences/show.html.haml @@ -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 |