2013-09-06 17:35:29 +08:00
|
|
|
class UsernameCheckerService
|
|
|
|
|
|
|
|
def check_username(username, email)
|
2013-11-20 03:15:05 +08:00
|
|
|
if username && username.length > 0
|
|
|
|
validator = UsernameValidator.new(username)
|
|
|
|
if !validator.valid_format?
|
|
|
|
{errors: validator.errors}
|
|
|
|
else
|
2016-04-01 05:33:25 +08:00
|
|
|
check_username_availability(username, email)
|
2013-11-20 03:15:05 +08:00
|
|
|
end
|
2013-09-06 17:35:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def check_username_availability(username, email)
|
2013-09-06 17:35:29 +08:00
|
|
|
if User.username_available?(username)
|
2016-04-01 05:33:25 +08:00
|
|
|
{ available: true, is_developer: is_developer?(email) }
|
2013-09-06 17:35:29 +08:00
|
|
|
else
|
|
|
|
{ available: false, suggestion: UserNameSuggester.suggest(username) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def is_developer?(value)
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(value)
|
|
|
|
end
|
|
|
|
|
2016-08-05 23:57:13 +08:00
|
|
|
|
|
|
|
def self.is_developer?(email)
|
|
|
|
UsernameCheckerService.new.is_developer?(email)
|
|
|
|
end
|
|
|
|
|
2013-09-06 17:35:29 +08:00
|
|
|
end
|