2018-07-30 11:45:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A simple finding for obtaining a single User.
|
|
|
|
#
|
|
|
|
# While using `User.find_by` directly is straightforward, it can lead to a lot
|
|
|
|
# of code duplication. Sometimes we just want to find a user by an ID, other
|
|
|
|
# times we may want to exclude blocked user. By using this finder (and extending
|
|
|
|
# it whenever necessary) we can keep this logic in one place.
|
|
|
|
class UserFinder
|
2018-10-18 05:06:44 -04:00
|
|
|
def initialize(username_or_id)
|
|
|
|
@username_or_id = username_or_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tries to find a User by id, returning nil if none could be found.
|
|
|
|
def find_by_id
|
|
|
|
User.find_by_id(@username_or_id)
|
|
|
|
end
|
2018-07-30 11:45:49 -04:00
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
# Tries to find a User by id, raising a `ActiveRecord::RecordNotFound` if it could
|
|
|
|
# not be found.
|
|
|
|
def find_by_id!
|
|
|
|
User.find(@username_or_id)
|
2018-07-30 11:45:49 -04:00
|
|
|
end
|
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
# Tries to find a User by username, returning nil if none could be found.
|
|
|
|
def find_by_username
|
|
|
|
User.find_by_username(@username_or_id)
|
2018-07-30 11:45:49 -04:00
|
|
|
end
|
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
# Tries to find a User by username, raising a `ActiveRecord::RecordNotFound` if it could
|
2018-07-30 11:45:49 -04:00
|
|
|
# not be found.
|
2018-10-18 05:06:44 -04:00
|
|
|
def find_by_username!
|
|
|
|
User.find_by_username!(@username_or_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tries to find a User by username or id, returning nil if none could be found.
|
|
|
|
def find_by_id_or_username
|
|
|
|
if input_is_id?
|
|
|
|
find_by_id
|
|
|
|
else
|
|
|
|
find_by_username
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tries to find a User by username or id, raising a `ActiveRecord::RecordNotFound` if it could
|
|
|
|
# not be found.
|
|
|
|
def find_by_id_or_username!
|
|
|
|
if input_is_id?
|
|
|
|
find_by_id!
|
|
|
|
else
|
|
|
|
find_by_username!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-09 20:06:44 -04:00
|
|
|
def find_by_ssh_key_id
|
|
|
|
return unless input_is_id?
|
|
|
|
|
|
|
|
User.find_by_ssh_key_id(@username_or_id)
|
|
|
|
end
|
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
def input_is_id?
|
|
|
|
@username_or_id.is_a?(Numeric) || @username_or_id =~ /^\d+$/
|
2018-07-30 11:45:49 -04:00
|
|
|
end
|
|
|
|
end
|