mirror of
https://github.com/discourse/discourse.git
synced 2024-12-30 06:50:38 +08:00
25a226279a
The #pluck_first freedom patch, first introduced by @danielwaterworth has served us well, and is used widely throughout both core and plugins. It seems to have been a common enough use case that Rails 6 introduced it's own method #pick with the exact same implementation. This allows us to retire the freedom patch and switch over to the built-in ActiveRecord method. There is no replacement for #pluck_first!, but a quick search shows we are using this in a very limited capacity, and in some cases incorrectly (by assuming a nil return rather than an exception), which can quite easily be replaced with #pick plus some extra handling.
34 lines
869 B
Ruby
34 lines
869 B
Ruby
# frozen_string_literal: true
|
|
|
|
class MinUsernameLengthValidator
|
|
MIN_USERNAME_LENGTH_RANGE = 1..60
|
|
|
|
def initialize(opts = {})
|
|
@opts = opts
|
|
end
|
|
|
|
def valid_value?(value)
|
|
if !MIN_USERNAME_LENGTH_RANGE.cover?(value)
|
|
@min_range_violation = true
|
|
return false
|
|
end
|
|
return false if value > SiteSetting.max_username_length
|
|
@username = User.where("length(username) < ?", value).pick(:username)
|
|
@username.blank?
|
|
end
|
|
|
|
def error_message
|
|
if @min_length_violation
|
|
I18n.t(
|
|
"site_settings.errors.invalid_integer_min_max",
|
|
min: MIN_USERNAME_LENGTH_RANGE.begin,
|
|
max: MIN_USERNAME_LENGTH_RANGE.end,
|
|
)
|
|
elsif @username.blank?
|
|
I18n.t("site_settings.errors.min_username_length_range")
|
|
else
|
|
I18n.t("site_settings.errors.min_username_length_exists", username: @username)
|
|
end
|
|
end
|
|
end
|