docbrown/app/services/settings/upsert.rb
Michael Kohl 5406b0576e
Split Settings::Authentication from SiteConfig (#13095)
* Split Settings::Authentication from SiteConfig

* Move specs

* Sort fields

* Update settings usages

* Update recaptcha usages

* Add data update script

* Update spec

* Rename SiteConfigParams concern

* Fixes, new route, new controller

* Controller and service refactoring

* More controller and service updates

* Spec updates

* More spec fixes

* Move file

* Fix FeedbackMessagesController

* Update admin/configs_spec

* Fix remaining specs in admin/configs_spec

* Fix configs API

* Formatting

* Clean up old service object

* Various fixes

* Update DUS

* Add model argument to admin_config_label

* Fix key name

* Fix specs

* Add distinct request caches for settings classes

* Fix e2e tests

* Fix remaining system spec

* Make DUS idempotent

* Move routes block

* Cleanup

* Switch to ActiveSupport::CurrentAttributes

* Pinned rails-settings-cached

* Update e2e test

* Update lib/data_update_scripts/20210316091354_move_authentication_settings.rb

Co-authored-by: rhymes <rhymes@hey.com>

* Add guard to DUS

* Temporarily re-add two SiteConfig fields

* Fix config show view

Co-authored-by: rhymes <rhymes@hey.com>
2021-04-12 09:41:09 +02:00

69 lines
1.7 KiB
Ruby

module Settings
class Upsert
EMOJI_ONLY_FIELDS = %w[community_emoji].freeze
VALID_DOMAIN = /^[a-zA-Z0-9]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.freeze
PARAMS_TO_BE_CLEANED = %i[sidebar_tags suggested_tags suggested_users].freeze
attr_reader :errors
def self.call(configs)
new(configs).call
end
def initialize(configs)
@configs = configs
@success = false
end
def call
clean_up_params
@errors = []
upsert_configs
return self if @errors.any?
@success = true
after_upsert_tasks
self
end
def success?
@success
end
def upsert_configs
@configs.each do |key, value|
if value.is_a?(Array)
SiteConfig.public_send("#{key}=", value.reject(&:blank?)) unless value.empty?
elsif value.respond_to?(:to_h)
SiteConfig.public_send("#{key}=", value.to_h) unless value.empty?
else
SiteConfig.public_send("#{key}=", value.strip) unless value.nil?
end
rescue ActiveRecord::RecordInvalid => e
@errors << e.message
next
end
end
def after_upsert_tasks
create_tags_if_not_created
end
def clean_up_params
PARAMS_TO_BE_CLEANED.each do |param|
@configs[param] = @configs[param]&.downcase&.delete(" ") if @configs[param]
end
@configs[:credit_prices_in_cents]&.transform_values!(&:to_i)
end
def create_tags_if_not_created
# Bulk create tags if they should exist.
# This is an acts-as-taggable-on as used on saving of an Article, etc.
return unless (@configs.keys & %w[suggested_tags sidebar_tags]).any?
Tag.find_or_create_all_with_like_by_name(SiteConfig.suggested_tags + SiteConfig.sidebar_tags)
end
end
end