docbrown/app/controllers/admin/settings/mandatory_settings_controller.rb
Michael Kohl 3837c3a221
Refactor handling of mandatory settings (#13553)
* Refactor handling of mandatory settings

* Add e2e test

* Rename controller
2021-04-29 09:25:16 +07:00

45 lines
1.3 KiB
Ruby

module Admin
module Settings
class MandatorySettingsController < Admin::ApplicationController
def create
errors = upsert_config(settings_params)
if errors.none?
Audit::Logger.log(:internal, current_user, params.dup)
redirect_to admin_config_path, notice: "Site configuration was successfully updated."
else
redirect_to admin_config_path, alert: "😭 #{errors.to_sentence}"
end
end
def upsert_config(configs)
errors = []
configs.each do |key, value|
settings_model = ::Settings::Mandatory::MAPPINGS[key.to_sym]
if value.is_a?(Array)
settings_model.public_send("#{key}=", value.reject(&:blank?)) if value.present?
else
settings_model.public_send("#{key}=", value.strip) unless value.nil?
end
rescue ActiveRecord::RecordInvalid => e
errors << e.message
next
end
errors
end
private
# NOTE: we need to override this since the controller name doesn't reflect
# the model name
def authorization_resource
::Settings::Mandatory
end
def settings_params
params.permit(::Settings::Mandatory.keys)
end
end
end
end