diff --git a/app/models/concerns/archivable_model.rb b/app/models/concerns/archivable_model.rb index d0ecef2df..fdde1757c 100644 --- a/app/models/concerns/archivable_model.rb +++ b/app/models/concerns/archivable_model.rb @@ -15,7 +15,7 @@ module ArchivableModel # Helper for archiving project. Timestamp of archiving is handler by # before_save callback. # Sets the archived_by value to the current user. - def archive (current_user) + def archive(current_user) self.archived = true self.archived_by = current_user save @@ -29,7 +29,7 @@ module ArchivableModel # Helper for restoring project from archive. # Sets the restored_by value to the current user. - def restore (current_user) + def restore(current_user) self.archived = false self.restored_by = current_user save diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 4b8a3e13b..a2827dc39 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -256,12 +256,6 @@ class Experiment < ApplicationRecord private - # Archive all modules. Receives an array of module integer IDs. - def archive_modules(module_ids) - my_modules.where(id: module_ids).each(&:archive!) - my_modules.reload - end - # Archive all modules. Receives an array of module integer IDs # and current user. def archive_modules(module_ids, current_user) diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 30fb3b7ec..1486addb4 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -26,9 +26,7 @@ class MyModule < ApplicationRecord validate :check_status, if: :my_module_status_id_changed? validate :check_status_conditions, if: :my_module_status_id_changed? - validate :check_status_implications, unless: proc { |mm| - (mm.changed_attributes.keys - %w(my_module_status_id x y my_module_group_id workflow_order status_changing)).blank? - } + validate :check_status_implications belongs_to :created_by, foreign_key: 'created_by_id', @@ -164,18 +162,19 @@ class MyModule < ApplicationRecord # Remove association with module group. self.my_module_group = nil + was_archived = false + MyModule.transaction do - archived = super + was_archived = super # Unassociate all samples from module. - archived = SampleMyModule.where(my_module: self).destroy_all if archived + was_archived = SampleMyModule.where(my_module: self).destroy_all if was_archived # Remove all connection between modules. - archived = Connection.where(input_id: id).delete_all if archived - archived = Connection.where(output_id: id).delete_all if archived - unless archived - raise ActiveRecord::Rollback - end + was_archived = Connection.where(input_id: id).destroy_all if was_archived + was_archived = Connection.where(output_id: id).destroy_all if was_archived + + raise ActiveRecord::Rollback unless was_archived end - archived + was_archived end # Similar as super restore, but also calculate new module position