destroying.rb revision 245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksamodule Repository::Destroying
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa extend ActiveSupport::Concern
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa
e6d3b53a6ae989ddbb213f916efba58d2170f9eeEugen Kuksa DELETION_WAIT_TIME = 30.days
e6d3b53a6ae989ddbb213f916efba58d2170f9eeEugen Kuksa
be464317febc0e3a3dbfff2df7a31c3f4b982c88Eugen Kuksa included do
be464317febc0e3a3dbfff2df7a31c3f4b982c88Eugen Kuksa scope :destroying, ->() { unscoped.where(is_destroying: true) }
be464317febc0e3a3dbfff2df7a31c3f4b982c88Eugen Kuksa scope :active, ->() { where(is_destroying: false) }
be464317febc0e3a3dbfff2df7a31c3f4b982c88Eugen Kuksa default_scope ->() { active }
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa def self.find_deleted_repository_with_owner(path, user)
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa repository = Repository.unscoped.find_by_path(path)
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa if user.owned_ids('Repository').include?(repository.id)
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa repository
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa end
245dfcfa9c4ef88578b8ae0a19e0fa5462eb61c9Eugen Kuksa end
be464317febc0e3a3dbfff2df7a31c3f4b982c88Eugen Kuksa end
be464317febc0e3a3dbfff2df7a31c3f4b982c88Eugen Kuksa
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa # Only use `destroy_asynchronously` if you want to destroy a repository.
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa # It prepares the deletion by setting a flag, which enables the deletion
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa # of its ontologies.
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa def destroy
bd63e05633307b60a7216b523b2b7a2bcc7b2823Eugen Kuksa Rails.logger.info("Destroy #{self.class} #{self} (id: #{id})")
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa super
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa rescue StandardError => e
8d3aa811c26916ecb2051e97a249f25fb6feeaf4Eugen Kuksa self.is_destroying = false
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa save!
246c94208917051fd0f0d22dce3c80ef60fb94e2Eugen Kuksa raise e.class,
246c94208917051fd0f0d22dce3c80ef60fb94e2Eugen Kuksa I18n.t('repository.delete_error', oms: Settings.OMS.with_indefinite_article),
246c94208917051fd0f0d22dce3c80ef60fb94e2Eugen Kuksa cause: e
36ec4872a8ea62879182f57759a24eb1bc708365Eugen Kuksa end
36ec4872a8ea62879182f57759a24eb1bc708365Eugen Kuksa
f334cf84f4f0421d0c5a65eb446516fe9e066b4eEugen Kuksa def destroy_asynchronously
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa unless can_be_deleted?
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa raise Repository::DeleteError, I18n.t('repository.delete_error')
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa end
bd63e05633307b60a7216b523b2b7a2bcc7b2823Eugen Kuksa Rails.logger.info("Mark #{self.class} #{self} (id: #{id}) as is_destroying")
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa self.is_destroying = true
36ec4872a8ea62879182f57759a24eb1bc708365Eugen Kuksa save!
e6d3b53a6ae989ddbb213f916efba58d2170f9eeEugen Kuksa RepositoryDeletionWorker.perform_in(DELETION_WAIT_TIME, id)
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa end
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa
d48ad500ac13e587c1d0d9b278ac9d86cde873efEugen Kuksa def can_be_deleted?
32fde02d264d59cc052be11a5a5a0c54902a95d3Eugen Kuksa ontologies.all?(&:can_be_deleted_with_whole_repository?)
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa end
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksaend