refactor by_username_or_id

This commit is contained in:
Nihad Abbasov 2013-10-16 22:31:46 +05:00
parent e09cc195a7
commit 12420a2256
2 changed files with 2 additions and 10 deletions

View File

@ -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= {})

View File

@ -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