docbrown/app/controllers/profiles_controller.rb
Jacob Herrington 45e45491b1
Migrate "static" profile fields into new columns (#13641)
* Move static profile fields to profiles columns

In order to remove some ambiguity around the availability of certain
profile fields, we can designate certain fields as "static." These
fields are intended to exist on every Forem regardless of configuration;
they contain basic personal info that most Forem's will likely use.

Because these fields already exist on some Forems it is necessary to
migrate the data from existing profile's data column to their respective
columns.

This change should behave as expected irrespective of the existence of
the static fields having associated ProfileFields, however, the UI that
is rendered in a user's settings still depends on the presence of the
ProfileFields. We can address that in a future change when we are
prepared to delete those ProfileFields entirely. We should make sure the
migration occurs without issue before moving to that step, in my
opinion.

* Apply suggestions from code review

Co-authored-by: Michael Kohl <me@citizen428.net>

* Add clarifying comments

Co-authored-by: Michael Kohl <me@citizen428.net>

Co-authored-by: Michael Kohl <me@citizen428.net>
2021-05-26 14:40:18 -05:00

26 lines
782 B
Ruby

class ProfilesController < ApplicationController
before_action :authenticate_user!
ALLOWED_USER_PARAMS = %i[name email username profile_image].freeze
def update
update_result = Profiles::Update.call(current_user, update_params)
if update_result.success?
flash[:settings_notice] = "Your profile has been updated"
redirect_to user_settings_path
else
@user = current_user
@tab = "profile"
flash[:error] = "Error: #{update_result.errors_as_sentence}"
render template: "users/edit", locals: {
user: update_params[:user],
profile: update_params[:profile]
}
end
end
private
def update_params
params.permit(profile: Profile.attributes + Profile.static_fields, user: ALLOWED_USER_PARAMS)
end
end