aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/import.rb7
-rw-r--r--spec/models/import_spec.rb18
2 files changed, 25 insertions, 0 deletions
diff --git a/app/models/import.rb b/app/models/import.rb
index 20e7f2d8a..68cb62f86 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -22,6 +22,7 @@ class Import < ActiveRecord::Base
validates_format_of :file, with: %r{\.zip\z}i, message: I18n.t('activerecord.errors.models.import.attributes.file.wrong_file_extension')
before_create :initialize_fields
+ before_destroy :destroy_non_ready_referential
def self.model_name
ActiveModel::Name.new Import, Import, "Import"
@@ -97,6 +98,12 @@ class Import < ActiveRecord::Base
self.token_download = SecureRandom.urlsafe_base64
end
+ def destroy_non_ready_referential
+ if !referential.ready
+ referential.destroy
+ end
+ end
+
def self.symbols_with_indifferent_access(array)
array.flat_map { |symbol| [symbol, symbol.to_s] }
end
diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb
index 7be05908a..dfb947711 100644
--- a/spec/models/import_spec.rb
+++ b/spec/models/import_spec.rb
@@ -57,6 +57,24 @@ RSpec.describe Import, type: :model do
expect(ImportMessage.count).to eq(0)
end
+
+ it "must destroy its associated Referential if ready: false" do
+ referential = create(:referential, ready: false)
+ import = create(:import, referential: referential)
+
+ import.destroy
+
+ expect(referential).to be_destroyed
+ end
+
+ it "must not destroy its associated Referential if ready: true" do
+ referential = create(:referential, ready: true)
+ import = create(:import, referential: referential)
+
+ import.destroy
+
+ expect(referential).not_to be_destroyed
+ end
end
describe "#notify_parent" do