From 462f8ffc474a2839914925d8696904e0d2ac5517 Mon Sep 17 00:00:00 2001 From: artoscinote <85488244+artoscinote@users.noreply.github.com> Date: Wed, 13 Oct 2021 09:50:06 +0200 Subject: [PATCH] Fixed unassigned user methods, order by name [SCI-6104] (#3589) --- app/controllers/user_my_modules_controller.rb | 2 +- app/models/my_module.rb | 18 +++++++++--------- app/models/project.rb | 18 +++++++++++------- app/views/user_my_modules/_index.html.erb | 2 +- app/views/user_my_modules/_index_edit.html.erb | 4 ++-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/controllers/user_my_modules_controller.rb b/app/controllers/user_my_modules_controller.rb index f97add128..c11a49ea3 100644 --- a/app/controllers/user_my_modules_controller.rb +++ b/app/controllers/user_my_modules_controller.rb @@ -33,7 +33,7 @@ class UserMyModulesController < ApplicationController def index_edit @user_my_modules = @my_module.user_my_modules - @unassigned_users = @my_module.unassigned_users + @undesignated_users = @my_module.undesignated_users.order(full_name: :asc) @new_um = UserMyModule.new(my_module: @my_module) respond_to do |format| diff --git a/app/models/my_module.rb b/app/models/my_module.rb index b52172bba..a171070a6 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -191,15 +191,15 @@ class MyModule < ApplicationRecord report_elements.where(repository_id: ids).update(repository: repository) end - def unassigned_users - User.find_by_sql( - "SELECT DISTINCT users.id, users.full_name FROM users " + - "INNER JOIN user_projects ON users.id = user_projects.user_id " + - "INNER JOIN experiments ON experiments.project_id = user_projects.project_id " + - "WHERE experiments.id = #{experiment_id.to_s}" + - " AND users.id NOT IN " + - "(SELECT DISTINCT user_id FROM user_my_modules WHERE user_my_modules.my_module_id = #{id.to_s})" - ) + def undesignated_users + User.joins(:user_assignments) + .joins( + "LEFT OUTER JOIN user_my_modules ON user_my_modules.user_id = users.id "\ + "AND user_my_modules.my_module_id = #{id}" + ) + .where(user_assignments: { assignable: self }) + .where(user_my_modules: { id: nil }) + .distinct end def unassigned_tags diff --git a/app/models/project.rb b/app/models/project.rb index 96daeb496..e92ad43bb 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -196,16 +196,20 @@ class Project < ApplicationRecord end def unassigned_users - User - .joins('INNER JOIN user_teams ON users.id = user_teams.user_id') - .where('user_teams.team_id = ?', team) - .where.not(confirmed_at: nil) - .where('users.id NOT IN (?)', - UserProject.where(project: self).select(:user_id).distinct) + User.joins(:user_teams) + .joins( + "LEFT OUTER JOIN user_assignments ON user_assignments.user_id = users.id "\ + "AND user_assignments.assignable_id = #{id} "\ + "AND user_assignments.assignable_type = 'Project'" + ) + .where(user_teams: { team_id: team_id }) + .where(user_assignments: { id: nil }) + .where.not(confirmed_at: nil) + .distinct end def user_role(user) - user_assignments.find_by_user_id(user)&.user_role.name + user_assignments.includes(:user_role).references(:user_role).find_by(user: user)&.user_role&.name end def sorted_experiments(user, sort_by = :new, archived = false) diff --git a/app/views/user_my_modules/_index.html.erb b/app/views/user_my_modules/_index.html.erb index aa0153f8c..f2f0f1ba4 100644 --- a/app/views/user_my_modules/_index.html.erb +++ b/app/views/user_my_modules/_index.html.erb @@ -11,7 +11,7 @@ <%= image_tag avatar_path(user, :icon_small) %> <% end %> - <% if can_manage_my_module_users?(@my_module) && @my_module.unassigned_users.any? %> + <% if can_manage_my_module_users?(@my_module) && @my_module.undesignated_users.any? %> diff --git a/app/views/user_my_modules/_index_edit.html.erb b/app/views/user_my_modules/_index_edit.html.erb index e61701512..60ea1ac5c 100644 --- a/app/views/user_my_modules/_index_edit.html.erb +++ b/app/views/user_my_modules/_index_edit.html.erb @@ -34,13 +34,13 @@ <% end %> <% end %> -<% if can_manage_my_module_users?(@my_module) && @unassigned_users.count > 0 %> +<% if can_manage_my_module_users?(@my_module) && @undesignated_users.any? %>

  • <%= bootstrap_form_for [@my_module, @new_um], remote: true, format: :json, html: { class: 'add-user-form' } do |f| %>
    - <%= collection_select(:user_my_module, :user_id, @unassigned_users, :id, :full_name, {}, { class: 'selectpicker' }) %> + <%= collection_select(:user_my_module, :user_id, @undesignated_users, :id, :full_name, {}, { class: 'selectpicker' }) %>
    <%= f.button class: 'btn btn-primary' do %>