destroying.rb revision 8d3aa811c26916ecb2051e97a249f25fb6feeaf4
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksamodule Repository::Destroying
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa extend ActiveSupport::Concern
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen 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 }
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
1ec6855738a4ea917d91182d1591714dbfcb430cEugen 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!
36ec4872a8ea62879182f57759a24eb1bc708365Eugen Kuksa raise e.class, I18n.t('repository.delete_error', oms: Settings.OMS.with_indefinite_article)
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
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa Rails.logger.info "Mark #{self.class} #{self} (id: #{id}) as is_destroying"
28042dd9ac50a328466a3dd0464b6e26a7c8796dEugen Kuksa self.is_destroying = true
36ec4872a8ea62879182f57759a24eb1bc708365Eugen Kuksa save!
36ec4872a8ea62879182f57759a24eb1bc708365Eugen Kuksa async(:destroy)
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa end
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa
d48ad500ac13e587c1d0d9b278ac9d86cde873efEugen Kuksa def can_be_deleted?
ac0d5f8866a6b0fd2c5b8b9f7a73090ce818d2feEugen Kuksa ontologies.map(&:can_be_deleted_with_whole_repository?).all?
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksa end
214b17ce5309c42f31fa6df96778cc9c85ae927dEugen Kuksaend