discourse/lib/avatar_lookup.rb

25 lines
606 B
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
class AvatarLookup
attr_accessor :user_ids, :users
2013-02-05 14:16:51 -05:00
def initialize(user_ids=[])
self.user_ids = AvatarLookup.filtered_users(user_ids)
2013-02-05 14:16:51 -05:00
end
# Lookup a user by id
def [](user_id)
self.users = AvatarLookup.hashed_users(user_ids) if self.users.nil?
self.users[user_id]
2013-02-05 14:16:51 -05:00
end
private
def self.filtered_users(user_ids=[])
user_ids.flatten.tap(&:compact!).tap(&:uniq!)
end
2013-02-05 14:16:51 -05:00
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
2013-02-05 14:16:51 -05:00
end