Add User.find_by_username!
This commit is contained in:
parent
a364d4260d
commit
5ce6d18681
|
@ -50,7 +50,7 @@ class UsersController < ApplicationController
|
|||
private
|
||||
|
||||
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?
|
||||
return authenticate_user!
|
||||
|
|
|
@ -274,6 +274,10 @@ class User < ActiveRecord::Base
|
|||
value: login.to_s.downcase).first
|
||||
end
|
||||
|
||||
def find_by_username!(username)
|
||||
find_by!('lower(username) = ?', username.downcase)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
@ -442,6 +442,18 @@ describe User do
|
|||
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
|
||||
it { is_expected.to have_many(:keys).dependent(:destroy) }
|
||||
|
||||
|
|
Loading…
Reference in New Issue