discourse/lib/avatar_lookup.rb
Victor Cruz Dueñas 5d2a6e3994 refactored and tested AvatarLookup
less array copying

Avoid N queries
2013-05-30 11:04:05 +02:00

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