From 684674952c0cbdf44477d497b16febef3138b3d7 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Sat, 9 Jan 2021 21:27:19 +0100 Subject: [PATCH] Add Restore button to the archived Projects screen [SCI-5344] --- app/assets/javascripts/projects/index.js | 18 ++++++------- app/assets/stylesheets/projects.scss | 5 +++- app/controllers/project_folders_controller.rb | 4 +-- app/controllers/projects_controller.rb | 26 +++++++++++++++++-- app/services/projects_overview_service.rb | 20 +++++++++----- app/views/projects/index/_toolbar.html.erb | 13 +++++++++- config/locales/en.yml | 3 +++ config/routes.rb | 1 + 8 files changed, 69 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/projects/index.js b/app/assets/javascripts/projects/index.js index 3eb1798e0..25570ed4c 100644 --- a/app/assets/javascripts/projects/index.js +++ b/app/assets/javascripts/projects/index.js @@ -100,26 +100,26 @@ }); } - // init project toolbar archive/restore function - function initArchiveToolbarButton() { + // init project toolbar archive/restore functions + function initArchiveRestoreToolbarButtons() { $(toolbarWrapper) - .on('ajax:before', '.archive-projects-form', function() { - let archiveForm = $(this); - archiveForm.find('input[name="projects_ids[]"]').remove(); + .on('ajax:before', '.archive-projects-form, .restore-projects-form', function() { + let buttonForm = $(this); + buttonForm.find('input[name="projects_ids[]"]').remove(); selectedProjects.forEach(function(id) { $('').attr({ type: 'hidden', name: 'projects_ids[]', value: id - }).appendTo(archiveForm); + }).appendTo(buttonForm); }); }) - .on('ajax:success', '.archive-projects-form', function(ev, data) { + .on('ajax:success', '.archive-projects-form, .restore-projects-form', function(ev, data) { HelperModule.flashAlertMsg(data.message, 'success'); // Project saved, reload view refreshCurrentView(); }) - .on('ajax:error', '.archive-projects-form', function(ev, data) { + .on('ajax:error', '.archive-projects-form, .restore-projects-form', function(ev, data) { HelperModule.flashAlertMsg(data.responseJSON.message, 'danger'); }); } @@ -365,7 +365,7 @@ initManageUsersModal(); initExportProjectsModal(); initExportProjects(); - initArchiveToolbarButton(); + initArchiveRestoreToolbarButtons(); initUsersEditLink($('.project-card')); initFormSubmitLinks($('.project-card')); diff --git a/app/assets/stylesheets/projects.scss b/app/assets/stylesheets/projects.scss index 182faf4d8..b84ab10ca 100644 --- a/app/assets/stylesheets/projects.scss +++ b/app/assets/stylesheets/projects.scss @@ -550,6 +550,10 @@ li.module-hover { display: inline-block; } + .restore-projects-form { + display: inline-block; + } + .header-actions { align-items: center; display: flex; @@ -1219,4 +1223,3 @@ li.module-hover { margin: 1em 0; } } - diff --git a/app/controllers/project_folders_controller.rb b/app/controllers/project_folders_controller.rb index b138e6b0e..1a11c6279 100644 --- a/app/controllers/project_folders_controller.rb +++ b/app/controllers/project_folders_controller.rb @@ -65,8 +65,8 @@ class ProjectFoldersController < ApplicationController def move_to_modal view_state = current_team.current_view_state(current_user) - @current_view_mode = params[:mode] || :active - @current_sort = view_state.state.dig('projects', @current_view_mode.to_s, 'sort') || 'atoz' + @current_view_mode = params[:mode] || 'active' + @current_sort = view_state.state.dig('projects', @current_view_mode, 'sort') || 'atoz' render json: { html: render_to_string(partial: 'projects/index/modals/move_to_modal_contents.html.erb', diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 00d155f14..dc7546f82 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -21,8 +21,8 @@ class ProjectsController < ApplicationController def index if current_team view_state = current_team.current_view_state(current_user) - @current_view_mode = params[:mode] || :active - @current_sort = view_state.state.dig('projects', @current_view_mode.to_s, 'sort') || 'atoz' + @current_view_mode = params[:mode] || 'active' + @current_sort = view_state.state.dig('projects', @current_view_mode, 'sort') || 'atoz' end end @@ -228,6 +228,28 @@ class ProjectsController < ApplicationController end end + def restore_group + projects = current_team.projects.archived.where(id: params[:projects_ids]) + counter = 0 + projects.each do |project| + next unless can_restore_project?(project) + + project.transaction do + project.restore!(current_user) + log_activity(:restore_project, project) + counter += 1 + rescue StandardError => e + Rails.logger.error e.message + raise ActiveRecord::Rollback + end + end + if counter.positive? + render json: { message: t('projects.restore_group.success_flash', number: counter) } + else + render json: { message: t('projects.restore_group.error_flash') }, status: :unprocessable_entity + end + end + def show redirect_to action: :experiment_archive if @project.archived? # This is the "info" view diff --git a/app/services/projects_overview_service.rb b/app/services/projects_overview_service.rb index dc8da57c6..b70b8ae82 100644 --- a/app/services/projects_overview_service.rb +++ b/app/services/projects_overview_service.rb @@ -48,6 +48,9 @@ class ProjectsOverviewService elsif @params[:folders_search] == 'true' folders = ProjectFolder.inner_folders(@team, nil).or(ProjectFolder.where(id: nil)) fetch_project_records + elsif @view_mode == 'archived' + folders = ProjectFolder.where(id: nil) + fetch_project_records.where(team: @team) else folders = ProjectFolder.where(id: nil) fetch_project_records.where(project_folder: nil, team: @team) @@ -63,8 +66,13 @@ class ProjectsOverviewService end def project_and_folder_cards - cards = filter_project_records(fetch_project_records.where(project_folder: @current_folder)) + - filter_project_folder_records(fetch_project_folder_records.where(parent_folder: @current_folder)) + cards = + if @view_mode == 'archived' + filter_project_records(fetch_project_records) + filter_project_folder_records(fetch_project_folder_records) + else + filter_project_records(fetch_project_records.where(project_folder: @current_folder)) + + filter_project_folder_records(fetch_project_folder_records.where(parent_folder: @current_folder)) + end mixed_sort_records(cards) end @@ -103,8 +111,8 @@ class ProjectsOverviewService end def filter_project_records(records) - records = records.where(archived: true) if @params[:view_mode] == 'archived' - records = records.where(archived: false) if @params[:view_mode] == 'active' + records = records.archived if @view_mode == 'archived' + records = records.active if @view_mode == 'active' records = records.where_attributes_like('projects.name', @params[:search]) if @params[:search].present? records = records.where_attributes_like('projects.name', @params[:search]) if @params[:search].present? records = records.where('user_projects.user_id IN (?)', @params[:members]) if @params[:members]&.any? @@ -118,8 +126,8 @@ class ProjectsOverviewService end def filter_project_folder_records(records) - records = records.where(archived: true) if @params[:view_mode] == 'archived' - records = records.where(archived: false) if @params[:view_mode] == 'active' + records = records.archived if @view_mode == 'archived' + records = records.active if @view_mode == 'active' records = records.where_attributes_like('project_folders.name', @params[:search]) if @params[:search].present? records end diff --git a/app/views/projects/index/_toolbar.html.erb b/app/views/projects/index/_toolbar.html.erb index c69cd460d..7797f394e 100644 --- a/app/views/projects/index/_toolbar.html.erb +++ b/app/views/projects/index/_toolbar.html.erb @@ -5,6 +5,7 @@ <% if current_team && can_create_projects?(current_team) %> <%= link_to new_project_path(project_folder_id: @current_folder), class: 'btn btn-primary new-project-btn', + data: { view_mode: 'active' }, remote: true do %> @@ -14,6 +15,7 @@ <% if current_team && can_create_project_folders?(current_team) %> <%= link_to new_project_folder_path(project_folder_id: @current_folder), class: 'btn btn-light new-project-folder-btn', + data: { view_mode: 'active' }, remote: true do %> @@ -30,12 +32,21 @@ <%= button_to archive_group_projects_path, class: 'btn btn-light archive-projects-btn multiple-object-action project-only-action hidden', form_class: 'archive-projects-form', - data: { for: :archivable }, + data: { for: :archivable, view_mode: 'active' }, remote: true, method: :post do %> <% end %> + <%= button_to restore_group_projects_path, + class: 'btn btn-light restore-projects-btn multiple-object-action project-only-action hidden', + form_class: 'restore-projects-form', + data: { for: :restorable, view_mode: 'archived' }, + remote: true, + method: :post do %> + + + <% end %>