settings_validator.rb revision 787127631abcd463c471486a393ec0981d3ab9b7
294N/Aclass SettingsValidator
294N/A class SettingsValidator::ValidationError < ::StandardError; end
787N/A
789N/A CONFIG_YML_FILES =
789N/A "config/settings[.local].yml or config/settings/#{Rails.env}[.local].yml"
789N/A CONFIG_INITIALIZER_FILES = "config/environments/#{Rails.env}[.local].rb"
789N/A
789N/A def validate!
789N/A settings_validations_wrapper = SettingsValidationWrapper.new
873N/A unless settings_validations_wrapper.valid?
789N/A plain_errors = settings_validations_wrapper.errors.messages
877N/A formatted_errors = format_errors(plain_errors)
789N/A print_errors(formatted_errors)
294N/A exit 1
873N/A end
873N/A end
789N/A
789N/A protected
789N/A
873N/A def print_errors(formatted_errors)
789N/A $stderr.puts "The settings are invalid. Please check your #{CONFIG_YML_FILES}"
789N/A $stderr.puts 'The following errors were detected:'
789N/A $stderr.puts
789N/A formatted_errors.each { |error| $stderr.puts error }
789N/A $stderr.puts
789N/A $stderr.puts 'Stopping the application.'
789N/A end
789N/A
789N/A def format_errors(errors)
789N/A errors.map { |key, messages| format_error(key, messages) }
789N/A end
873N/A
1114N/A def format_error(key, messages)
941N/A category, key_portions = key_info(key)
941N/A opening_line =
994N/A if category == :initializers
1109N/A "#{format_initializer_error(key_portions)} #{format_key(key_portions)}"
1086N/A elsif category == :yml
911N/A format_key(key_portions)
911N/A else
873N/A key
873N/A end
873N/A bad_value = value_of(category, key_portions)
789N/A <<-MESSAGE
787N/A#{opening_line}:
294N/A#{format_messages(messages)}
294N/A Value: #{bad_value.inspect}
294N/A
294N/A MESSAGE
294N/A end
294N/A
294N/A def key_info(key)
1086N/A category, *portions = key.to_s.split('__')
869N/A [category.to_sym, portions]
789N/A end
789N/A
911N/A def format_key(portions)
789N/A portions.join('.')
789N/A end
868N/A
294N/A def format_messages(messages)
294N/A messages.map { |message| format_message(message) }.join("\n")
294N/A end
294N/A
294N/A def format_message(message)
789N/A if message == 'is not included in the list'
294N/A message =
1109N/A "#{message} (see the comments in the config/settings.yml at this key)"
1109N/A end
789N/A indent_message(message, 2)
789N/A end
1109N/A
1109N/A def format_initializer_error(key_portions)
1109N/A key = key_portions.join('.')
1109N/A if 'fqdn' == key
787N/A "The FQDN could not be determined. Please set the hostname in #{CONFIG_YML_FILES}"
869N/A elsif %w(data_root git_root git_daemon_path commits_path).include?(key)
869N/A 'Please check the paths keys in the settings -'
869N/A else
869N/A # other possible values: %w(consider_all_requests_local secret_token log_level)
789N/A "Please set a valid value in the #{CONFIG_INITIALIZER_FILES} for"
789N/A end
789N/A end
789N/A
789N/A def value_of(category, key_portions)
789N/A if !%i(yml initializers).include?(category)
294N/A SettingsValidationWrapper.new.send(category)
789N/A else
294N/A object = SettingsValidationWrapper.base(category.to_s)
294N/A SettingsValidationWrapper.get_value(object, key_portions)
789N/A end
294N/A end
294N/A
294N/A def indent_message(message, indentation)
294N/A message.split("\n").map { |line| "#{' ' * indentation}#{line}"}.join("\n")
294N/A end
294N/Aend
294N/A