2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-11 14:42:41 -04:00
|
|
|
class UsernameSettingValidator
|
2017-10-04 15:08:51 -04:00
|
|
|
|
|
|
|
include RegexSettingValidation
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(opts = {})
|
2014-06-12 18:03:03 -04:00
|
|
|
@opts = opts
|
2017-10-04 15:08:51 -04:00
|
|
|
initialize_regex_opts(opts)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
2017-10-04 15:08:51 -04:00
|
|
|
!val.present? || (User.where(username: val).exists? && regex_match?(val))
|
2014-06-11 14:42:41 -04:00
|
|
|
end
|
|
|
|
|
2014-06-18 10:49:21 -04:00
|
|
|
def error_message
|
2017-10-04 15:08:51 -04:00
|
|
|
if @regex_fail
|
|
|
|
I18n.t(@regex_error)
|
|
|
|
else
|
|
|
|
I18n.t('site_settings.errors.invalid_username')
|
|
|
|
end
|
2014-06-11 14:42:41 -04:00
|
|
|
end
|
|
|
|
end
|