Files
docuseal/app/controllers/personalization_settings_controller.rb
T

68 lines
1.7 KiB
Ruby
Raw Normal View History

2023-08-16 22:57:59 +03:00
# frozen_string_literal: true
class PersonalizationSettingsController < ApplicationController
2024-02-29 23:59:49 +02:00
ALLOWED_KEYS = [
AccountConfig::FORM_COMPLETED_BUTTON_KEY,
AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY,
AccountConfig::SUBMITTER_DOCUMENTS_COPY_EMAIL_KEY,
2024-04-05 16:16:30 +03:00
AccountConfig::SUBMITTER_COMPLETED_EMAIL_KEY,
AccountConfig::FORM_COMPLETED_MESSAGE_KEY,
*(Docuseal.multitenant? ? [] : [AccountConfig::POLICY_LINKS_KEY])
2024-02-29 23:59:49 +02:00
].freeze
InvalidKey = Class.new(StandardError)
before_action :load_and_authorize_account_config, only: :create
2023-09-17 00:45:57 +03:00
def show
authorize!(:read, AccountConfig)
end
2023-08-16 22:57:59 +03:00
def create
2024-04-24 01:19:07 +03:00
if @account_config.value.is_a?(Hash)
@account_config.value = @account_config.value.reject do |_, v|
v.blank? && v != false
end
end
2024-04-23 20:47:12 +03:00
2024-03-09 23:29:59 +02:00
if @account_config.value != false && @account_config.value.blank?
@account_config.destroy!
else
@account_config.save!
end
2023-08-16 22:57:59 +03:00
2024-09-18 19:47:47 +03:00
redirect_back(fallback_location: settings_personalization_path, notice: I18n.t('settings_have_been_saved'))
2023-08-16 22:57:59 +03:00
end
private
2024-02-29 23:59:49 +02:00
def load_and_authorize_account_config
@account_config =
current_account.account_configs.find_or_initialize_by(key: account_config_params[:key])
@account_config.assign_attributes(account_config_params)
authorize!(:create, @account_config)
raise InvalidKey unless ALLOWED_KEYS.include?(@account_config.key)
@account_config
end
2024-01-14 01:59:13 +02:00
def account_config_params
2025-02-24 23:59:30 +02:00
attrs = params.require(:account_config).permit(:key, :value, { value: {} }, { value: [] })
2024-01-14 01:59:13 +02:00
2024-03-09 23:29:59 +02:00
return attrs if attrs[:value].is_a?(String)
2024-01-14 01:59:13 +02:00
attrs[:value]&.transform_values! do |value|
if value.in?(%w[true false])
value == 'true'
else
value
end
end
attrs
2023-08-16 22:57:59 +03:00
end
end