discourse/lib/avatar_lookup.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
605 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-05 14:16:51 -05:00
class AvatarLookup
2017-07-27 21:20:09 -04:00
def initialize(user_ids = [])
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
2013-02-05 14:16:51 -05:00
end
# Lookup a user by id
def [](user_id)
2013-07-12 17:07:54 -04:00
users[user_id]
2013-02-05 14:16:51 -05:00
end
private
2013-02-05 14:16:51 -05:00
def self.lookup_columns
2018-08-21 07:58:11 -04:00
@lookup_columns ||= %i{id user_emails.email username name uploaded_avatar_id}
end
2013-07-12 17:07:54 -04:00
def users
@users ||= user_lookup_hash
end
def user_lookup_hash
hash = {}
User.joins(:user_emails)
2017-07-27 21:20:09 -04:00
.where(id: @user_ids)
.select(AvatarLookup.lookup_columns)
.each { |user| hash[user.id] = user }
hash
end
2013-02-05 14:16:51 -05:00
end