2013-02-06 03:16:51 +08:00
|
|
|
class AvatarLookup
|
|
|
|
|
2013-05-25 14:50:39 +08:00
|
|
|
def initialize(user_ids=[])
|
2013-07-21 08:56:48 +08:00
|
|
|
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lookup a user by id
|
|
|
|
def [](user_id)
|
2013-07-13 05:07:54 +08:00
|
|
|
users[user_id]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-25 14:50:39 +08:00
|
|
|
private
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-09-09 14:02:13 +08:00
|
|
|
def self.lookup_columns
|
2017-04-27 02:47:36 +08:00
|
|
|
@lookup_columns ||= %i{id user_emails.email username uploaded_avatar_id}
|
2013-09-09 14:02:13 +08:00
|
|
|
end
|
|
|
|
|
2013-07-13 05:07:54 +08:00
|
|
|
def users
|
2013-09-09 14:02:13 +08:00
|
|
|
@users ||= user_lookup_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_lookup_hash
|
|
|
|
# adding tap here is a personal taste thing
|
|
|
|
hash = {}
|
2017-04-27 02:47:36 +08:00
|
|
|
User.joins(:user_emails)
|
|
|
|
.where(id: @user_ids)
|
2014-05-12 15:32:49 +08:00
|
|
|
.select(AvatarLookup.lookup_columns)
|
|
|
|
.each{ |user| hash[user.id] = user }
|
2013-09-09 14:02:13 +08:00
|
|
|
hash
|
2013-05-25 14:50:39 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|