Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bulk_users_by_email_load.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: edbd3e214589217cccd3e41ad165b387b83da602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true

module BulkUsersByEmailLoad
  extend ActiveSupport::Concern

  included do
    def users_by_emails(emails)
      Gitlab::SafeRequestLoader.execute(resource_key: user_by_email_resource_key, resource_ids: emails) do |emails|
        # have to consider all emails - even secondary, so use all_emails here
        grouped_users_by_email = User.by_any_email(emails).preload(:emails).group_by(&:all_emails)

        grouped_users_by_email.each_with_object({}) do |(found_emails, users), h|
          found_emails.each { |e| h[e] = users.first if emails.include?(e) } # don't include all emails for an account, only the ones we want
        end
      end
    end

    private

    def user_by_email_resource_key
      "user_by_email_for_#{User.name.underscore.pluralize}:#{self.class}:#{self.id}"
    end
  end
end