Refactor: Move onboarding routes out of UsersController (#17189)

This commit is contained in:
Mac Siri 2022-04-13 14:20:16 -04:00 committed by GitHub
parent f1d6291d00
commit 441a76972b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 52 deletions

View file

@ -0,0 +1,62 @@
module Users
class OnboardingsController < ApplicationController
before_action :set_no_cache_header
before_action :authenticate_user!
after_action :verify_authorized
ALLOWED_USER_PARAMS = %i[last_onboarding_page username].freeze
ALLOWED_CHECKBOX_PARAMS = %i[checked_code_of_conduct checked_terms_and_conditions].freeze
def onboarding_update
authorize User
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

View file

@ -3,15 +3,11 @@ class UsersController < ApplicationController
before_action :check_suspended, only: %i[update update_password]
before_action :set_user,
only: %i[update update_password request_destroy full_delete remove_identity]
# rubocop:disable Layout/LineLength
after_action :verify_authorized, except: %i[index signout_confirm add_org_admin remove_org_admin remove_from_org confirm_destroy]
# rubocop:enable Layout/LineLength
before_action :authenticate_user!, only: %i[onboarding_update onboarding_checkbox_update]
after_action :verify_authorized,
except: %i[index signout_confirm add_org_admin remove_org_admin remove_from_org confirm_destroy]
before_action :set_suggested_users, only: %i[index]
before_action :initialize_stripe, only: %i[edit]
ALLOWED_USER_PARAMS = %i[last_onboarding_page username].freeze
ALLOWED_ONBOARDING_PARAMS = %i[checked_code_of_conduct checked_terms_and_conditions].freeze
INDEX_ATTRIBUTES_FOR_SERIALIZATION = %i[id name username summary profile_image].freeze
private_constant :INDEX_ATTRIBUTES_FOR_SERIALIZATION
@ -163,34 +159,6 @@ class UsersController < ApplicationController
redirect_to user_settings_path(@tab)
end
def onboarding_update
authorize User
user_params = {}
if params[:user]
if params[:user].key?(:username) && params[:user][:username].blank?
return render_update_response(false, I18n.t("users_controller.username_blank"))
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)
render_update_response(update_result.success?, update_result.errors_as_sentence)
end
def onboarding_checkbox_update
if params[:user]
current_user.assign_attributes(params[:user].permit(ALLOWED_ONBOARDING_PARAMS))
end
current_user.saw_onboarding = true
authorize User
render_update_response(current_user.save)
end
def join_org
authorize User
if (@organization = Organization.find_by(secret: params[:org_secret].strip))
@ -289,10 +257,6 @@ class UsersController < ApplicationController
private
def sanitize_user_params
params[:user].delete_if { |_k, v| v.blank? }
end
def set_suggested_users
@suggested_users = Settings::General.suggested_users
end
@ -312,14 +276,6 @@ class UsersController < ApplicationController
recent_suggestions.presence || default_suggested_users
end
def render_update_response(success, errors = nil)
status = success ? 200 : 422
respond_to do |format|
format.json { render json: { errors: errors }, status: status }
end
end
def handle_organization_tab
@organizations = @current_user.organizations.order(name: :asc)
if params[:org_id] == "new" || (params[:org_id].blank? && @organizations.size.zero?)
@ -377,10 +333,6 @@ class UsersController < ApplicationController
Feeds::ImportArticlesWorker.perform_async(user.id)
end
def profile_params
params[:profile] ? params[:profile].permit(Profile.static_fields + Profile.attributes) : nil
end
def password_params
params.permit(:current_password, :password, :password_confirmation)
end

View file

@ -211,8 +211,8 @@ Rails.application.routes.draw do
get "/notifications/:filter/:org_id", to: "notifications#index", as: :notifications_filter_org
get "/notification_subscriptions/:notifiable_type/:notifiable_id", to: "notification_subscriptions#show"
post "/notification_subscriptions/:notifiable_type/:notifiable_id", to: "notification_subscriptions#upsert"
patch "/onboarding_update", to: "users#onboarding_update"
patch "/onboarding_checkbox_update", to: "users#onboarding_checkbox_update"
patch "/onboarding_update", to: "users/onboardings#onboarding_update"
patch "/onboarding_checkbox_update", to: "users/onboardings#onboarding_checkbox_update"
patch "/onboarding_notifications_checkbox_update",
to: "users/notification_settings#onboarding_notifications_checkbox_update"
get "email_subscriptions/unsubscribe"