Move theme attributes into their own method

This commit is contained in:
Andreas Nedbal 2023-01-29 21:12:33 +01:00
parent b5ab9d67d2
commit 61a31f97b0

View file

@ -8,7 +8,32 @@ class Settings::ThemeController < ApplicationController
def edit; end
def update
update_attributes = params.require(:theme).permit(%i[
if current_user.theme.nil?
current_user.theme = Theme.new theme_attributes
current_user.theme.user_id = current_user.id
if current_user.theme.save
flash[:success] = t(".success")
else
flash[:error] = t(".error", errors: current_user.theme.errors.messages.flatten.join(" "))
end
elsif current_user.theme.update(theme_attributes)
flash[:success] = t(".success")
else
flash[:error] = t(".error", errors: current_user.theme.errors.messages.flatten.join(" "))
end
redirect_to settings_theme_path
end
def destroy
current_user.theme.destroy!
redirect_to edit_settings_theme_path
end
private
def theme_attributes
params.require(:theme).permit(%i[
primary_color primary_text
danger_color danger_text
success_color success_text
@ -22,26 +47,5 @@ class Settings::ThemeController < ApplicationController
muted_text input_color
input_text input_placeholder
])
if current_user.theme.nil?
current_user.theme = Theme.new update_attributes
current_user.theme.user_id = current_user.id
if current_user.theme.save
flash[:success] = t(".success")
else
flash[:error] = t(".error", errors: current_user.theme.errors.messages.flatten.join(" "))
end
elsif current_user.theme.update(update_attributes)
flash[:success] = t(".success")
else
flash[:error] = t(".error", errors: current_user.theme.errors.messages.flatten.join(" "))
end
redirect_to settings_theme_path
end
def destroy
current_user.theme.destroy!
redirect_to edit_settings_theme_path
end
end