Skip to content
Snippets Groups Projects
Commit 9167077c authored by William George's avatar William George
Browse files

Refactor `UserFinder.execute` to `UserFinder.find_by_id_or_username`

parent cb5a2f6e
No related branches found
No related tags found
No related merge requests found
Pipeline #32554109 passed
......@@ -34,7 +34,7 @@ class UserFinder
end
# Tries to find a User by username or id, returning nil if none could be found.
def execute
def find_by_id_or_username
if input_is_id?
find_by_id
else
......@@ -44,7 +44,7 @@ class UserFinder
# Tries to find a User by username or id, raising a `ActiveRecord::RecordNotFound` if it could
# not be found.
def execute!
def find_by_id_or_username!
if input_is_id?
find_by_id!
else
......
......@@ -95,7 +95,7 @@ module API
end
def find_user(id)
UserFinder.new(id).execute
UserFinder.new(id).find_by_id_or_username
end
# rubocop: disable CodeReuse/ActiveRecord
......
......@@ -49,10 +49,10 @@ describe UserFinder do
end
end
describe '#execute' do
describe '#find_by_id_or_username' do
context 'when the user exists (id)' do
it 'returns the user' do
found = described_class.new(user.id).execute
found = described_class.new(user.id).find_by_id_or_username
expect(found).to eq(user)
end
......@@ -60,7 +60,7 @@ describe UserFinder do
context 'when the user exists (id as string)' do
it 'returns the user' do
found = described_class.new(user.id.to_s).execute
found = described_class.new(user.id.to_s).find_by_id_or_username
expect(found).to eq(user)
end
......@@ -68,7 +68,7 @@ describe UserFinder do
context 'when the user exists (username)' do
it 'returns the user' do
found = described_class.new(user.username).execute
found = described_class.new(user.username).find_by_id_or_username
expect(found).to eq(user)
end
......@@ -76,7 +76,7 @@ describe UserFinder do
context 'when the user does not exist (username)' do
it 'returns nil' do
found = described_class.new("non_existent_username").execute
found = described_class.new("non_existent_username").find_by_id_or_username
expect(found).to be_nil
end
......@@ -84,7 +84,7 @@ describe UserFinder do
context 'when the user does not exist' do
it 'returns nil' do
found = described_class.new(1).execute
found = described_class.new(1).find_by_id_or_username
expect(found).to be_nil
end
......@@ -135,10 +135,10 @@ describe UserFinder do
end
end
describe '#execute!' do
describe '#find_by_id_or_username!' do
context 'when the user exists (id)' do
it 'returns the user' do
found = described_class.new(user.id).execute!
found = described_class.new(user.id).find_by_id_or_username!
expect(found).to eq(user)
end
......@@ -146,7 +146,7 @@ describe UserFinder do
context 'when the user exists (id as string)' do
it 'returns the user' do
found = described_class.new(user.id.to_s).execute!
found = described_class.new(user.id.to_s).find_by_id_or_username!
expect(found).to eq(user)
end
......@@ -154,7 +154,7 @@ describe UserFinder do
context 'when the user exists (username)' do
it 'returns the user' do
found = described_class.new(user.username).execute!
found = described_class.new(user.username).find_by_id_or_username!
expect(found).to eq(user)
end
......@@ -164,7 +164,7 @@ describe UserFinder do
it 'raises ActiveRecord::RecordNotFound' do
finder = described_class.new("non_existent_username")
expect { finder.execute! }.to raise_error(ActiveRecord::RecordNotFound)
expect { finder.find_by_id_or_username! }.to raise_error(ActiveRecord::RecordNotFound)
end
end
......@@ -172,7 +172,7 @@ describe UserFinder do
it 'raises ActiveRecord::RecordNotFound' do
finder = described_class.new(1)
expect { finder.execute! }.to raise_error(ActiveRecord::RecordNotFound)
expect { finder.find_by_id_or_username! }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment