From 4f4ab057403416847c575d0be8ecef9a69868625 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 1 Oct 2021 12:20:24 +0200 Subject: [PATCH] Fix project search in quick start controller [SCI-5915] --- app/controllers/dashboard/quick_start_controller.rb | 6 +++--- app/models/concerns/assignable.rb | 6 ++++++ app/models/project.rb | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/controllers/dashboard/quick_start_controller.rb b/app/controllers/dashboard/quick_start_controller.rb index ae1378f53..e85ff1659 100644 --- a/app/controllers/dashboard/quick_start_controller.rb +++ b/app/controllers/dashboard/quick_start_controller.rb @@ -20,9 +20,9 @@ module Dashboard end def project_filter - projects = current_team.projects.search(current_user, false, params[:query], 1, current_team) - .where(user_projects: { user_id: current_user.id, role: %i(owner normal_user) }) - .select(:id, :name) + projects = Project.managable_by_user(current_user) + .search(current_user, false, params[:query], 1, current_team) + .select(:id, :name) projects = projects.map { |i| { value: i.id, label: escape_input(i.name) } } if (projects.map { |i| i[:label] }.exclude? params[:query]) && params[:query].present? projects = [{ value: 0, label: params[:query] }] + projects diff --git a/app/models/concerns/assignable.rb b/app/models/concerns/assignable.rb index 24f3b2589..035151b65 100644 --- a/app/models/concerns/assignable.rb +++ b/app/models/concerns/assignable.rb @@ -16,6 +16,12 @@ module Assignable .where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::READ) } + scope :managable_by_user, lambda { |user| + joins(user_assignments: :user_role) + .where(user_assignments: { user: user }) + .where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::MANAGE) + } + after_create_commit do UserAssignment.create!( user: created_by, diff --git a/app/models/project.rb b/app/models/project.rb index 171cd9e32..bae1cf75a 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -4,6 +4,7 @@ class Project < ApplicationRecord include SearchableByNameModel include ViewableModel include PermissionCheckableModel + include Assignable enum visibility: { hidden: 0, visible: 1 } @@ -42,7 +43,6 @@ class Project < ApplicationRecord belongs_to :team, inverse_of: :projects, touch: true belongs_to :project_folder, inverse_of: :projects, optional: true, touch: true has_many :user_projects, inverse_of: :project - has_many :user_assignments, as: :assignable, dependent: :destroy has_many :users, through: :user_assignments has_many :experiments, inverse_of: :project has_many :active_experiments, -> { where(archived: false) }, @@ -95,7 +95,7 @@ class Project < ApplicationRecord user.id ) end - new_query = new_query.where_attributes_like(:name, query, options) + new_query = new_query.where_attributes_like('projects.name', query, options) if include_archived return new_query