From 12420a2256168a122b3a1f5a2c974df52fe802ec Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 16 Oct 2013 22:31:46 +0500 Subject: [PATCH] refactor by_username_or_id --- app/models/user.rb | 6 +----- lib/api/helpers.rb | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 22292de40a6..df14cf34e85 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -199,11 +199,7 @@ class User < ActiveRecord::Base end def by_username_or_id(name_or_id) - if (name_or_id.is_a?(Integer)) - User.find_by_id(name_or_id) - else - User.find_by_username(name_or_id) - end + where('username = ? OR id = ?', name_or_id, name_or_id).first end def build_user(attrs = {}, options= {}) diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 2cac04d3495..1db1f396389 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -11,11 +11,7 @@ module API # If the sudo is the current user do nothing if (identifier && !(@current_user.id == identifier || @current_user.username == identifier)) render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin? - begin - @current_user = User.by_username_or_id(identifier) - rescue => ex - not_found!("No user id or username for: #{identifier}") - end + @current_user = User.by_username_or_id(identifier) not_found!("No user id or username for: #{identifier}") if @current_user.nil? end @current_user