mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 10:58:31 -05:00
5d2a6e3994
less array copying Avoid N queries
24 lines
606 B
Ruby
24 lines
606 B
Ruby
class AvatarLookup
|
|
attr_accessor :user_ids, :users
|
|
|
|
def initialize(user_ids=[])
|
|
self.user_ids = AvatarLookup.filtered_users(user_ids)
|
|
end
|
|
|
|
# Lookup a user by id
|
|
def [](user_id)
|
|
self.users = AvatarLookup.hashed_users(user_ids) if self.users.nil?
|
|
self.users[user_id]
|
|
end
|
|
|
|
private
|
|
def self.filtered_users(user_ids=[])
|
|
user_ids.flatten.tap(&:compact!).tap(&:uniq!)
|
|
end
|
|
|
|
def self.hashed_users(user_ids=[])
|
|
users = User.where(:id => user_ids).select([:id, :email, :username])
|
|
users_with_ids = users.collect {|x| [x.id, x] }.flatten
|
|
Hash[*users_with_ids]
|
|
end
|
|
end
|