# frozen_string_literal: true
class UserDefaultsController < DashboardController
def update
community = nil
server = nil
if (community_id = permitted_params[:default_community_id].presence)
community = Community.find_by_community_id(community_id)
if community.nil?
redirect_to(
user_aliases_path(current_user.discord_id),
error: "Unable to find the selected community. Please refresh this page and try again"
)
return
end
end
if (server_id = permitted_params[:default_server_id].presence)
server = Server.find_by_server_id(server_id)
if server.nil?
redirect_to(
user_aliases_path(current_user.discord_id),
error: "Unable to find the selected server. Please refresh this page and try again"
)
return
end
end
current_user.id_defaults.update!(server_id: server&.id, community_id: community&.id)
flash[:success] = "Updated defaults"
redirect_to edit_user_path(current_user.discord_id)
end
private
def permitted_params
@permitted_params ||= params.require(:user_defaults).permit(
:default_community_id, :default_server_id
)
end
end