discourse/lib/primary_group_lookup.rb

38 lines
888 B
Ruby
Raw Normal View History

class PrimaryGroupLookup
2017-07-27 20:20:09 -05:00
def initialize(user_ids = [])
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
end
# Lookup primary group for a given user id
def [](user_id)
users[user_id]
end
private
def self.lookup_columns
@lookup_columns ||= %i{id name flair_url flair_bg_color flair_color}
end
def users
@users ||= user_lookup_hash
end
def user_lookup_hash
users_with_primary_group = User.where(id: @user_ids)
2017-07-27 20:20:09 -05:00
.where.not(primary_group_id: nil)
.select(:id, :primary_group_id)
group_lookup = {}
group_ids = users_with_primary_group.map(&:primary_group_id).compact
Group.where(id: group_ids).select(self.class.lookup_columns)
2017-07-27 20:20:09 -05:00
.each { |g| group_lookup[g.id] = g }
hash = {}
users_with_primary_group.each do |u|
hash[u.id] = group_lookup[u.primary_group_id]
end
hash
end
end