mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 07:43:43 +08:00
5d2a6e3994
less array copying Avoid N queries
25 lines
606 B
Ruby
25 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
|