docbrown/app/controllers/users/onboardings_controller.rb
Mac Siri 4b37f2384c
Refactor OnboardingsController (#17329)
* Refactor OnboardingsController

* Fix broken spec

* Update policy

* Fix spec

* Update config/routes.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Touchup

* Update routes

* Screw it, let's move notification_settings too

* Revert "Screw it, let's move notification_settings too"

This reverts commit aead8c05f4dda62cbc46cdd033afd0acdef2ad73.

* Temp .travis.yml changes

* Revert "Temp .travis.yml changes"

This reverts commit c26109843ba027f9a524e66282a9b01f0341f836.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-04-27 16:17:02 -04:00

67 lines
1.9 KiB
Ruby

module Users
class OnboardingsController < ApplicationController
before_action :authenticate_user!
before_action :set_cache_control_headers, only: [:show]
before_action :set_no_cache_header, only: %i[update onboarding_checkbox_update]
after_action :verify_authorized, except: [:show]
ALLOWED_USER_PARAMS = %i[last_onboarding_page username].freeze
ALLOWED_CHECKBOX_PARAMS = %i[checked_code_of_conduct checked_terms_and_conditions].freeze
def show
set_surrogate_key_header "onboarding-slideshow"
end
def update
authorize User, :onboarding_update?
user_params = {}
if params[:user]
if unset_username?
return render json: { errors: I18n.t("users_controller.username_blank") }, status: :unprocessable_entity
end
sanitize_user_params
user_params = params[:user].permit(ALLOWED_USER_PARAMS)
end
update_result = Users::Update.call(current_user, user: user_params, profile: profile_params)
if update_result.success?
render json: {}, status: :ok
else
render json: { errors: update_result.errors_as_sentence }, status: :unprocessable_entity
end
end
def onboarding_checkbox_update
if params[:user]
current_user.assign_attributes(params[:user].permit(ALLOWED_CHECKBOX_PARAMS))
end
current_user.saw_onboarding = true
authorize User
if current_user.save
render json: {}, status: :ok
else
render json: { errors: errors }, status: :unprocessable_entity
end
end
private
def unset_username?
params[:user].key?(:username) && params[:user][:username].blank?
end
def sanitize_user_params
params[:user].compact_blank!
end
def profile_params
params[:profile] ? params[:profile].permit(Profile.static_fields + Profile.attributes) : nil
end
end
end