diff --git a/app/assets/javascripts/repositories/repository_datatable.js.erb b/app/assets/javascripts/repositories/repository_datatable.js.erb index 8d8093df5..f7567d307 100644 --- a/app/assets/javascripts/repositories/repository_datatable.js.erb +++ b/app/assets/javascripts/repositories/repository_datatable.js.erb @@ -476,19 +476,43 @@ var RepositoryDatatable = (function(global) { }); }; - global.onClickAssignRecords = function() { + global.openAssignRecordsModal = function() { + $.post( + $('#assignRepositoryRecords').data('assign-url-modal'), + { selected_rows: rowsSelected }) + .done( + function(data) { + $(data.html).appendTo('body').promise().done(function() { + $('#assignRepositoryRecordModal').modal('show'); + }); + } + ); + } + + global.hideAssignUnasignModal = function(id) { + $(id).modal('hide').promise().done( + function() { + $(id).remove(); + } + ) + } + + global.submitAssignRepositoryRecord = function(option) { animateSpinner(); $.ajax({ - url: $('#assignRepositoryRecords').data('assign-url'), + url: $('#assignRepositoryRecordModal').data('assign-url'), type: 'POST', dataType: 'json', - data: { selected_rows: rowsSelected }, + data: { selected_rows: rowsSelected, + downstream: (option === 'downstream') }, success: function(data) { + hideAssignUnasignModal('#assignRepositoryRecordModal'); HelperModule.flashAlertMsg(data.flash, 'success'); onClickCancel(); clearRowSelection(); }, error: function(data) { + hideAssignUnasignModal('#assignRepositoryRecordModal'); HelperModule.flashAlertMsg(data.responseJSON.flash, 'danger'); onClickCancel(); clearRowSelection(); @@ -496,19 +520,35 @@ var RepositoryDatatable = (function(global) { }); } - global.onClickUnassignRecords = function() { + global.openUnassignRecordsModal = function() { + $.post( + $('#unassignRepositoryRecords').data('unassign-url'), + { selected_rows: rowsSelected }) + .done( + function(data) { + $(data.html).appendTo('body').promise().done(function() { + $('#unassignRepositoryRecordModal').modal('show'); + }); + } + ); + } + + global.submitUnassignRepositoryRecord = function(option) { animateSpinner(); $.ajax({ - url: $('#unassignRepositoryRecords').data('unassign-url'), + url: $('#unassignRepositoryRecordModal').data('unassign-url'), type: 'POST', dataType: 'json', - data: {selected_rows: rowsSelected}, + data: { selected_rows: rowsSelected, + downstream: (option === 'downstream') }, success: function(data) { + hideAssignUnasignModal('#unassignRepositoryRecordModal'); HelperModule.flashAlertMsg(data.flash, 'success'); onClickCancel(); clearRowSelection(); }, error: function(data) { + hideAssignUnasignModal('#unassignRepositoryRecordModal'); HelperModule.flashAlertMsg(data.responseJSON.flash, 'danger'); onClickCancel(); clearRowSelection(); diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index 3fb9bf76e..64bdeecd3 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -12,10 +12,14 @@ class MyModulesController < ApplicationController assign_samples unassign_samples delete_samples toggle_task_state samples_index archive complete_my_module repository repository_index - assign_repository_records unassign_repository_records) + assign_repository_records unassign_repository_records + unassign_repository_records_modal + assign_repository_records_modal) before_action :load_vars_nested, only: %i(new create) before_action :load_repository, only: %i(assign_repository_records unassign_repository_records + unassign_repository_records_modal + assign_repository_records_modal repository_index) before_action :load_projects_by_teams, only: %i(protocols results activities samples repository archive) @@ -25,10 +29,13 @@ class MyModulesController < ApplicationController %i(show activities activities_tab protocols results samples samples_index archive) before_action :check_complete_module_permission, only: :complete_my_module - before_action :check_assign_repository_records_permissions, only: - %i(assign_repository_records unassign_repository_records) - before_action :check_assign_samples_permissions, only: %i(assign_samples - unassign_samples) + before_action :check_assign_repository_records_permissions, + only: %i(unassign_repository_records_modal + assign_repository_records_modal + assign_repository_records + unassign_repository_records + assign_samples + unassign_samples) layout 'fluid'.freeze @@ -71,7 +78,7 @@ class MyModulesController < ApplicationController @last_activity_id = params[:from].to_i || 0 @per_page = 10 - @activities = @my_module.last_activities(@last_activity_id, @per_page +1 ) + @activities = @my_module.last_activities(@last_activity_id, @per_page + 1) @more_activities_url = "" @overflown = @activities.length > @per_page @@ -391,18 +398,33 @@ class MyModulesController < ApplicationController def assign_repository_records if params[:selected_rows].present? && params[:repository_id].present? records_names = [] + downstream = ActiveModel::Type::Boolean.new.cast(params[:downstream]) - params[:selected_rows].each do |id| - record = RepositoryRow.find_by_id(id) - next if !record || @my_module.repository_rows.include?(record) - record.last_modified_by = current_user - record.save - records_names << record.name - MyModuleRepositoryRow.create!( - my_module: @my_module, - repository_row: record, - assigned_by: current_user - ) + RepositoryRow + .where(id: params[:selected_rows], + repository_id: params[:repository_id]) + .find_each do |record| + unless @my_module.repository_rows.include?(record) + record.last_modified_by = current_user + record.save + + MyModuleRepositoryRow.create!( + my_module: @my_module, + repository_row: record, + assigned_by: current_user + ) + records_names << record.name + end + + next unless downstream + @my_module.downstream_modules.each do |my_module| + next if my_module.repository_rows.include?(record) + MyModuleRepositoryRow.create!( + my_module: my_module, + repository_row: record, + assigned_by: current_user + ) + end end if records_names.any? @@ -439,17 +461,29 @@ class MyModulesController < ApplicationController def unassign_repository_records if params[:selected_rows].present? && params[:repository_id].present? - records = [] + downstream = ActiveModel::Type::Boolean.new.cast(params[:downstream]) - params[:selected_rows].each do |id| - record = RepositoryRow.find_by_id(id) - next unless record && @my_module.repository_rows.include?(record) - record.last_modified_by = current_user - record.save - records << record - end + records = RepositoryRow.assigned_on_my_module(params[:selected_rows], + @my_module) @my_module.repository_rows.destroy(records & @my_module.repository_rows) + + if downstream + @my_module.downstream_modules.each do |my_module| + assigned_records = RepositoryRow.assigned_on_my_module( + params[:selected_rows], + my_module + ) + my_module.repository_rows.destroy( + assigned_records & my_module.repository_rows + ) + assigned_records.update_all(last_modified_by_id: current_user.id) + end + end + + # update last last_modified_by + records.update_all(last_modified_by_id: current_user.id) + if records.any? Activity.create( type_of: :unassign_repository_record, @@ -482,6 +516,28 @@ class MyModulesController < ApplicationController end end + def unassign_repository_records_modal + selected_rows = params[:selected_rows] + modal = render_to_string( + partial: 'my_modules/modals/unassign_repository_records_modal.html.erb', + locals: { my_module: @my_module, + repository: @repository, + selected_rows: selected_rows } + ) + render json: { html: modal }, status: :ok + end + + def assign_repository_records_modal + selected_rows = params[:selected_rows] + modal = render_to_string( + partial: 'my_modules/modals/assign_repository_records_modal.html.erb', + locals: { my_module: @my_module, + repository: @repository, + selected_rows: selected_rows } + ) + render json: { html: modal }, status: :ok + end + # Complete/uncomplete task def toggle_task_state respond_to do |format| diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index 3725852f1..5fa2b123b 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -21,4 +21,9 @@ class RepositoryRow < ApplicationRecord presence: true, length: { maximum: Constants::NAME_MAX_LENGTH } validates :created_by, presence: true + + def self.assigned_on_my_module(ids, my_module) + where(id: ids).joins(:my_module_repository_rows) + .where('my_module_repository_rows.my_module' => my_module) + end end diff --git a/app/views/my_modules/modals/_assign_repository_records_modal.html.erb b/app/views/my_modules/modals/_assign_repository_records_modal.html.erb new file mode 100644 index 000000000..6983e5af5 --- /dev/null +++ b/app/views/my_modules/modals/_assign_repository_records_modal.html.erb @@ -0,0 +1,37 @@ +