From 7e4f47f6677b95d51ef4a4bda111538992e2ca54 Mon Sep 17 00:00:00 2001 From: zmagoD Date: Sat, 12 Jun 2021 10:27:53 +0200 Subject: [PATCH] update experiment and my_module search --- .../generate_user_assignments_job.rb | 2 + app/models/experiment.rb | 48 +++++++++---------- app/models/my_module.rb | 48 ++++++++++--------- .../propagate_assignment_job_spec.rb | 18 ++++++- 4 files changed, 68 insertions(+), 48 deletions(-) diff --git a/app/jobs/user_assignments/generate_user_assignments_job.rb b/app/jobs/user_assignments/generate_user_assignments_job.rb index 828edf5b7..249426d5b 100644 --- a/app/jobs/user_assignments/generate_user_assignments_job.rb +++ b/app/jobs/user_assignments/generate_user_assignments_job.rb @@ -35,6 +35,8 @@ module UserAssignments user_role = parent_user_assignment.user_role user = parent_user_assignment.user new_user_assignment = UserAssignment.find_or_initialize_by(user: user, assignable: object) + return if new_user_assignment.manually_assigned? + new_user_assignment.user_role = user_role new_user_assignment.assigned_by = @assigned_by new_user_assignment.assigned = :automatically diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 0b88c9d6d..84e11d371 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -45,6 +45,13 @@ class Experiment < ApplicationRecord end } + scope :experiment_search_scope, lambda { |project_ids, user| + joins(:user_assignments).where( + project: project_ids, + user_assignments: { user: user } + ) + } + def self.search( user, include_archived, @@ -53,37 +60,30 @@ class Experiment < ApplicationRecord current_team = nil, options = {} ) - project_ids = + experiment_scope = experiment_search_scope( Project - .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) - .pluck(:id) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) + .pluck(:id), + user + ) if current_team - projects_ids = + new_query = experiment_search_scope( Project - .search(user, - include_archived, - nil, - 1, - current_team) - .select('id') - - new_query = - Experiment - .where('experiments.project_id IN (?)', projects_ids) - .where_attributes_like([:name, :description], query, options) + .search(user, + include_archived, + nil, + 1, + current_team) + .select('id'), + user + ).where_attributes_like([:name, :description], query, options) return include_archived ? new_query : new_query.active elsif include_archived - new_query = - Experiment - .where(project: project_ids) - .where_attributes_like([:name, :description], query, options) + new_query = experiment_scope.where_attributes_like([:name, :description], query, options) else - new_query = - Experiment - .active - .where(project: project_ids) - .where_attributes_like([:name, :description], query, options) + new_query = experiment_scope.active + .where_attributes_like([:name, :description], query, options) end # Show all results if needed diff --git a/app/models/my_module.rb b/app/models/my_module.rb index ade28a206..9d5e21a45 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -69,6 +69,13 @@ class MyModule < ApplicationRecord scope :workflow_ordered, -> { order(workflow_order: :asc) } scope :uncomplete, -> { where(state: 'uncompleted') } + scope :my_module_search_scope, lambda { |experiment_ids, user| + joins(:user_assignments).where( + experiment: experiment_ids, + user_assignments: { user: user } + ).distinct + } + # A module takes this much space in canvas (x, y) in database WIDTH = 30 HEIGHT = 14 @@ -81,23 +88,24 @@ class MyModule < ApplicationRecord current_team = nil, options = {} ) - exp_ids = + my_module_scope = my_module_search_scope( Experiment - .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) - .pluck(:id) + .search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT) + .pluck(:id), + user + ) if current_team - experiments_ids = Experiment - .search(user, - include_archived, - nil, - 1, - current_team) - .select('id') - new_query = MyModule - .distinct - .where('my_modules.experiment_id IN (?)', experiments_ids) - .where_attributes_like([:name, :description], query, options) + new_query = my_module_search_scope( + Experiment + .search(user, + include_archived, + nil, + 1, + current_team) + .select('id'), + user + ).where_attributes_like([:name, :description], query, options) if include_archived return new_query @@ -105,16 +113,10 @@ class MyModule < ApplicationRecord return new_query.where('my_modules.archived = ?', false) end elsif include_archived - new_query = MyModule - .distinct - .where('my_modules.experiment_id IN (?)', exp_ids) - .where_attributes_like([:name, :description], query, options) + new_query = my_module_scope.where_attributes_like([:name, :description], query, options) else - new_query = MyModule - .distinct - .where('my_modules.experiment_id IN (?)', exp_ids) - .where('my_modules.archived = ?', false) - .where_attributes_like([:name, :description], query, options) + new_query = my_module_scope.where('my_modules.archived = ?', false) + .where_attributes_like([:name, :description], query, options) end # Show all results if needed diff --git a/spec/jobs/user_assignments/propagate_assignment_job_spec.rb b/spec/jobs/user_assignments/propagate_assignment_job_spec.rb index 42180f0ff..247aebf45 100644 --- a/spec/jobs/user_assignments/propagate_assignment_job_spec.rb +++ b/spec/jobs/user_assignments/propagate_assignment_job_spec.rb @@ -60,10 +60,26 @@ module UserAssignments end it 'does not propagate the user assignment if the object was manually assigned' do - experiment_assignment = create :user_assignment, assignable: experiment_one, user: user_two, user_role: owner_role, assigned_by: user_one, assigned: :manually + experiment_assignment = create :user_assignment, + assignable: experiment_one, + user: user_two, + user_role: owner_role, + assigned_by: user_one, + assigned: :manually described_class.perform_now(project, user_two, technician_role, user_one) expect(experiment_assignment.reload.user_role).to eq owner_role end + + it 'does propagate the user assignment if the object was automatically assigned' do + experiment_assignment = create :user_assignment, + assignable: experiment_one, + user: user_two, + user_role: owner_role, + assigned_by: user_one, + assigned: :automatically + described_class.perform_now(project, user_two, technician_role, user_one) + expect(experiment_assignment.reload.user_role).to eq technician_role + end end end end