blob: b4ce61a869c58e44c716536e3ac12ffbe7f75b95 (
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# frozen_string_literal: true
module Preloaders
# This class preloads the max access level (role) for the users within the given projects and
# stores the values in requests store via the ProjectTeam class.
class UsersMaxAccessLevelInProjectsPreloader
def initialize(projects:, users:)
@projects = projects
@users = users
end
def execute
return unless @projects.present? && @users.present?
access_levels.each do |(project_id, user_id), access_level|
project = projects_by_id[project_id]
project.team.write_member_access_for_user_id(user_id, access_level)
end
end
private
def access_levels
ProjectAuthorization
.where(project_id: project_ids, user_id: user_ids)
.group(:project_id, :user_id)
.maximum(:access_level)
end
# Use reselect to override the existing select to prevent
# the error `subquery has too many columns`
# NotificationsController passes in an Array so we need to check the type
def project_ids
@projects.is_a?(ActiveRecord::Relation) ? @projects.reselect(:id) : @projects
end
def user_ids
@users.is_a?(ActiveRecord::Relation) ? @users.reselect(:id) : @users
end
def projects_by_id
@projects_by_id ||= @projects.index_by(&:id)
end
end
end
|