Commit 5ce6d186 authored by Robert Speicher's avatar Robert Speicher

Add User.find_by_username!

parent a364d426
...@@ -50,7 +50,7 @@ class UsersController < ApplicationController ...@@ -50,7 +50,7 @@ class UsersController < ApplicationController
private private
def set_user def set_user
@user = User.find_by!('lower(username) = ?', params[:username].downcase) @user = User.find_by_username!(params[:username])
unless current_user || @user.public_profile? unless current_user || @user.public_profile?
return authenticate_user! return authenticate_user!
......
...@@ -274,6 +274,10 @@ class User < ActiveRecord::Base ...@@ -274,6 +274,10 @@ class User < ActiveRecord::Base
value: login.to_s.downcase).first value: login.to_s.downcase).first
end end
def find_by_username!(username)
find_by!('lower(username) = ?', username.downcase)
end
def by_username_or_id(name_or_id) def by_username_or_id(name_or_id)
where('users.username = ? OR users.id = ?', name_or_id.to_s, name_or_id.to_i).first where('users.username = ? OR users.id = ?', name_or_id.to_s, name_or_id.to_i).first
end end
......
...@@ -442,6 +442,18 @@ describe User do ...@@ -442,6 +442,18 @@ describe User do
end end
end end
describe '.find_by_username!' do
it 'raises RecordNotFound' do
expect { described_class.find_by_username!('JohnDoe') }.
to raise_error(ActiveRecord::RecordNotFound)
end
it 'is case-insensitive' do
user = create(:user, username: 'JohnDoe')
expect(described_class.find_by_username!('JOHNDOE')).to eq user
end
end
describe 'all_ssh_keys' do describe 'all_ssh_keys' do
it { is_expected.to have_many(:keys).dependent(:destroy) } it { is_expected.to have_many(:keys).dependent(:destroy) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment