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