2013-02-05 13:16:51 -06:00
|
|
|
class AvatarLookup
|
|
|
|
|
2013-05-25 01:50:39 -05:00
|
|
|
def initialize(user_ids=[])
|
2013-07-20 19:56:48 -05:00
|
|
|
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lookup a user by id
|
|
|
|
def [](user_id)
|
2013-07-12 16:07:54 -05:00
|
|
|
users[user_id]
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-05-25 01:50:39 -05:00
|
|
|
private
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-09-09 01:02:13 -05:00
|
|
|
def self.lookup_columns
|
|
|
|
@lookup_columns ||= [:id,
|
2014-05-12 02:32:49 -05:00
|
|
|
:email,
|
|
|
|
:username,
|
|
|
|
:uploaded_avatar_id]
|
2013-09-09 01:02:13 -05:00
|
|
|
end
|
|
|
|
|
2013-07-12 16:07:54 -05:00
|
|
|
def users
|
2013-09-09 01:02:13 -05:00
|
|
|
@users ||= user_lookup_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_lookup_hash
|
|
|
|
# adding tap here is a personal taste thing
|
|
|
|
hash = {}
|
2014-05-12 02:32:49 -05:00
|
|
|
User.where(:id => @user_ids)
|
|
|
|
.select(AvatarLookup.lookup_columns)
|
|
|
|
.each{ |user| hash[user.id] = user }
|
2013-09-09 01:02:13 -05:00
|
|
|
hash
|
2013-05-25 01:50:39 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|