forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor User#temp_username (forem#19252)
* Refactor User#temp_username * Remove Authentication::Providers dependency from Users::UsernameGenerator * Adds brief description and yard docs
- Loading branch information
1 parent
15bb2e5
commit 0b4a360
Showing
4 changed files
with
99 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Generates available username based on | ||
# multiple generators in the following order: | ||
# * list of supplied usernames | ||
# * list of supplied usernames with suffix | ||
# * random generated letters | ||
# | ||
# @todo Extract username validation in separate class | ||
module Users | ||
class UsernameGenerator | ||
def self.call(...) | ||
new(...).call | ||
end | ||
|
||
# @param list [Array<String>] a list of usernames | ||
def initialize(list = []) | ||
@list = list | ||
end | ||
|
||
def call | ||
from_list(modified_list) || from_list(list_with_suffix) || from_list(Array.new(3) { random_letters }) | ||
end | ||
|
||
private | ||
|
||
def from_list(list) | ||
list.detect { |username| !username_exists?(username) } | ||
end | ||
|
||
def username_exists?(username) | ||
User.exists?(username: username) || | ||
Organization.exists?(slug: username) || | ||
Page.exists?(slug: username) || | ||
Podcast.exists?(slug: username) | ||
end | ||
|
||
def filtered_list | ||
@list.select { |s| s.is_a?(String) && s.present? } | ||
end | ||
|
||
def modified_list | ||
filtered_list.map { |s| s.downcase.gsub(/[^0-9a-z_]/i, "").delete(" ") } | ||
end | ||
|
||
def list_with_suffix | ||
modified_list.map { |s| [s, rand(100)].join("_") } | ||
end | ||
|
||
def random_letters | ||
("a".."z").to_a.sample(12).join | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Users::UsernameGenerator, type: :service do | ||
let(:user) { create(:user) } | ||
|
||
it "returns randomly generated username if empty list is passed" do | ||
expect(described_class.call([""])).to be_present | ||
end | ||
|
||
it "returns supplied username if does not exist" do | ||
expect(described_class.call(["foo"])).to eq("foo") | ||
end | ||
|
||
it "returns modified username" do | ||
expect(described_class.call(["foo.bar"])).to eq("foobar") | ||
end | ||
|
||
it "returns supplied username with suffix if exists" do | ||
expect(described_class.call([user.username])).to start_with("#{user.username}_") | ||
end | ||
|
||
it "returns randomly generated username" do | ||
expect(described_class.call).to be_present | ||
end | ||
|
||
it "returns nil if all generation methods are exhausted" do | ||
username_generator = described_class.new | ||
allow(username_generator).to receive(:random_letters).and_return(user.username) | ||
expect(username_generator.call).to be_nil | ||
end | ||
end |