2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class AvatarLookup
|
|
|
|
|
2013-05-25 02:50:39 -04:00
|
|
|
def initialize(user_ids = [])
|
2013-07-20 20:56:48 -04:00
|
|
|
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lookup a user by id
|
|
|
|
def [](user_id)
|
2013-07-12 17:07:54 -04:00
|
|
|
users[user_id]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-25 02:50:39 -04:00
|
|
|
private
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-09 02:02:13 -04:00
|
|
|
def self.lookup_columns
|
2018-08-21 07:58:11 -04:00
|
|
|
@lookup_columns ||= %i{id user_emails.email username name uploaded_avatar_id}
|
2013-09-09 02:02:13 -04:00
|
|
|
end
|
|
|
|
|
2013-07-12 17:07:54 -04:00
|
|
|
def users
|
2013-09-09 02:02:13 -04:00
|
|
|
@users ||= user_lookup_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_lookup_hash
|
|
|
|
hash = {}
|
2017-04-26 14:47:36 -04:00
|
|
|
User.joins(:user_emails)
|
|
|
|
.where(id: @user_ids)
|
2014-05-12 03:32:49 -04:00
|
|
|
.select(AvatarLookup.lookup_columns)
|
|
|
|
.each { |user| hash[user.id] = user }
|
2013-09-09 02:02:13 -04:00
|
|
|
hash
|
2013-05-25 02:50:39 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|