diff --git a/app/controllers/user_configs_controller.rb b/app/controllers/user_configs_controller.rb new file mode 100644 index 00000000..124e5d63 --- /dev/null +++ b/app/controllers/user_configs_controller.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class UserConfigsController < ApplicationController + before_action :load_user_config + authorize_resource :user_config + + ALLOWED_KEYS = [ + UserConfig::RECEIVE_COMPLETED_EMAIL + ].freeze + + def create + @user_config.update!(user_config_params) + + head :ok + end + + private + + def load_user_config + return head :not_found unless ALLOWED_KEYS.include?(user_config_params[:key]) + + @user_config = + UserConfig.find_or_initialize_by(user: current_user, key: user_config_params[:key]) + end + + def user_config_params + params.required(:user_config).permit!.tap do |attrs| + attrs[:value] = attrs[:value] == '1' if attrs[:value].in?(%w[1 0]) + end + end +end diff --git a/app/views/notifications_settings/index.html.erb b/app/views/notifications_settings/index.html.erb index bd880f00..632c145d 100644 --- a/app/views/notifications_settings/index.html.erb +++ b/app/views/notifications_settings/index.html.erb @@ -2,6 +2,20 @@ <%= render 'shared/settings_nav' %>