discourse/lib/avatar_lookup.rb

34 lines
662 B
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
class AvatarLookup
def initialize(user_ids=[])
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
2013-02-06 03:16:51 +08:00
end
# Lookup a user by id
def [](user_id)
2013-07-13 05:07:54 +08:00
users[user_id]
2013-02-06 03:16:51 +08:00
end
private
2013-02-06 03:16:51 +08:00
def self.lookup_columns
@lookup_columns ||= [:id,
:email,
:username,
:uploaded_avatar_id]
end
2013-07-13 05:07:54 +08:00
def users
@users ||= user_lookup_hash
end
def user_lookup_hash
# adding tap here is a personal taste thing
hash = {}
User.where(:id => @user_ids)
.select(AvatarLookup.lookup_columns)
.each{ |user| hash[user.id] = user }
hash
end
2013-02-06 03:16:51 +08:00
end