diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index eaf44e2b5..a7dce3b7d 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -11,7 +11,7 @@ class ExperimentsController < ApplicationController before_action :set_project, only: %i(new create samples_index samples module_archive clone_modal move_modal delete_samples) - before_action :load_projects_by_teams, only: %i(canvas samples module_archive) + before_action :load_projects_tree, only: %i(canvas samples module_archive) before_action :check_view_permissions, only: %i(canvas module_archive) before_action :check_manage_permissions, only: :edit @@ -348,9 +348,8 @@ class ExperimentsController < ApplicationController params.require(:experiment).permit(:name, :description, :archived) end - def load_projects_by_teams - @projects_by_teams = current_user.projects_by_teams(current_team.id, - nil, false) + def load_projects_tree + @projects_tree = current_user.projects_tree(current_team, nil) end def check_view_permissions diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index 0587c6891..0ddcb053d 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -21,8 +21,8 @@ class MyModulesController < ApplicationController unassign_repository_records_modal assign_repository_records_modal repository_index) - before_action :load_projects_by_teams, only: %i(protocols results activities - samples repository archive) + before_action :load_projects_tree, only: %i(protocols results activities + samples repository archive) before_action :check_manage_permissions_archive, only: %i(update destroy) before_action :check_manage_permissions, only: %i(description due_date) before_action :check_view_permissions, only: @@ -659,9 +659,8 @@ class MyModulesController < ApplicationController render_403 unless can_read_team?(@repository.team) end - def load_projects_by_teams - @projects_by_teams = current_user.projects_by_teams(current_team.id, - nil, false) + def load_projects_tree + @projects_tree = current_user.projects_tree(current_team, nil) end def check_manage_permissions diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index caf923c25..433a5a048 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -9,8 +9,8 @@ class ProjectsController < ApplicationController notifications reports samples experiment_archive delete_samples samples_index) - before_action :load_projects_by_teams, only: %i(index show samples archive - experiment_archive) + before_action :load_projects_tree, only: %i(index show samples archive + experiment_archive) before_action :load_archive_vars, only: :archive before_action :check_view_permissions, only: %i(show reports notifications samples experiment_archive @@ -313,26 +313,24 @@ class ProjectsController < ApplicationController end end - def load_projects_by_teams + def load_projects_tree if current_user.teams.any? - @current_team_id = current_team.id if current_team + @current_team = current_team if current_team - @current_team_id ||= current_user.teams.first.id + @current_team ||= current_user.teams.first @current_sort = params[:sort].to_s - @projects_by_teams = current_user.projects_by_teams(@current_team_id, - @current_sort, - false) + @projects_tree = current_user.projects_tree(@current_team, @current_sort) else - @projects_by_teams = [] + @projects_tree = [] end end def load_archive_vars if current_user.teams.any? @archived_projects_by_teams = - current_user.projects_by_teams(@current_team_id, @current_sort, true) + current_user.projects_by_teams(@current_team.id, @current_sort, true) else - @projects_by_teams = [] + @archived_projects_by_teams = [] end end diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 26c87f98b..fec46ca43 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -19,6 +19,8 @@ class Experiment < ApplicationRecord optional: true has_many :my_modules, inverse_of: :experiment, dependent: :destroy + has_many :active_my_modules, -> { where(archived: false) }, + class_name: 'MyModule' has_many :my_module_groups, inverse_of: :experiment, dependent: :destroy has_many :report_elements, inverse_of: :experiment, dependent: :destroy has_many :activities, inverse_of: :experiment diff --git a/app/models/project.rb b/app/models/project.rb index ce1bf4853..f3f938301 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -32,6 +32,8 @@ class Project < ApplicationRecord has_many :user_projects, inverse_of: :project has_many :users, through: :user_projects has_many :experiments, inverse_of: :project + has_many :active_experiments, -> { where(archived: false) }, + class_name: 'Experiment' has_many :project_comments, foreign_key: :associated_id, dependent: :destroy has_many :activities, inverse_of: :project has_many :tags, inverse_of: :project @@ -156,7 +158,7 @@ class Project < ApplicationRecord return (self.user_projects.select { |up| up.user == user }).first.role end - def active_experiments(sort_by = :new) + def sorted_active_experiments(sort_by = :new) sort = case sort_by when 'old' then { created_at: :asc } when 'atoz' then { name: :asc } diff --git a/app/models/user.rb b/app/models/user.rb index e0f617f08..1453a9367 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -338,6 +338,30 @@ class User < ApplicationRecord result || [] end + def projects_tree(team, sort_by = nil) + result = team.projects.includes(active_experiments: :active_my_modules) + unless is_admin_of_team?(team) + # Only admins see all projects of the team + result = result.includes(:user_projects).where( + 'visibility = 1 OR user_projects.user_id=:user_id)', user_id: id + ) + end + + sort = + case sort_by + when 'old' + { created_at: :asc } + when 'atoz' + { name: :asc } + when 'ztoa' + { name: :desc } + else + { created_at: :desc } + end + + result.where(archived: false).order(sort) + end + # Finds all activities of user that is assigned to project. If user # is not an owner of the project, user must be also assigned to # module. diff --git a/app/views/projects/archive.html.erb b/app/views/projects/archive.html.erb index 9874f0d64..4d9d91207 100644 --- a/app/views/projects/archive.html.erb +++ b/app/views/projects/archive.html.erb @@ -19,7 +19,7 @@