diff --git a/app/models/import_question.rb b/app/models/import_question.rb index 873ba2ccd..974994240 100644 --- a/app/models/import_question.rb +++ b/app/models/import_question.rb @@ -124,11 +124,17 @@ def update_saisie end def cree_choix - extraie_colonne_choix.each_value do |data_choix| + extraie_colonne('choix').each_value do |data_choix| cree_chaque_choix(data_choix) end end + def cree_reponses + extraie_colonne('reponse').each_value do |data_reponse| + cree_reponse(data_reponse) + end + end + def cree_chaque_choix(data) choix = Choix.create!( intitule: data['intitule'], @@ -139,6 +145,16 @@ def cree_chaque_choix(data) choix.audio.attach(telecharge_fichier(data['audio'])) end + def cree_reponse(data) + reponse = Choix.create!( + nom_technique: data['nom_technique'], + position_client: data['position_client'], + type_choix: data['type_choix'], + question_id: @question.id + ) + reponse.illustration.attach(telecharge_fichier(data['illustration'])) + end + def telecharge_fichier(url) @current_download = url fichier = Down.download(url) @@ -151,17 +167,17 @@ def telecharge_fichier(url) } end - def extraie_colonne_choix + def extraie_colonne(reponse) headers_data = {} @headers.each_with_index do |header, index| - next unless header.to_s.match(/choix_(\d+)_/) + next unless header.to_s.match(/#{reponse}_(\d+)_/) - choix = header.match(/choix_(\d+)_/)[1].to_i - data_type = header.match(/choix_\d+_(.*)/)[1] + item = header.match(/#{reponse}_(\d+)_/)[1].to_i + data_type = header.match(/#{reponse}_\d+_(.*)/)[1] - headers_data[choix] = {} if headers_data[choix].nil? - headers_data[choix][data_type] = @data[index] + headers_data[item] = {} if headers_data[item].nil? + headers_data[item][data_type] = @data[index] end headers_data diff --git a/spec/models/import_question_spec.rb b/spec/models/import_question_spec.rb index 54db1a875..f568c0605 100644 --- a/spec/models/import_question_spec.rb +++ b/spec/models/import_question_spec.rb @@ -87,4 +87,36 @@ expect(choix2.audio.attached?).to be true end end + + describe 'pour une question de type glisser deposer' do + subject(:service) do + described_class.new('QuestionGlisserDeposer') + end + + let(:file) do + fixture_file_upload('spec/support/import_question_glisser.xls', 'text/xls') + end + + it 'importe les données spécifiques' do + service.remplis_donnees(file) + question = Question.last + expect(question.zone_depot.attached?).to eq true + end + + it 'crée les réponses' do + service.remplis_donnees(file) + reponses = Question.last.reponses + expect(reponses.count).to eq 2 + reponse1 = reponses.first + expect(reponse1.nom_technique).to eq 'reponse1' + expect(reponse1.position_client).to eq 2 + expect(reponse1.type_choix).to eq 'bon' + expect(reponse1.illustration.attached?).to be true + reponse2 = reponses.last + expect(reponse2.nom_technique).to eq 'reponse2' + expect(reponse2.position_client).to eq 1 + expect(reponse1.type_choix).to eq 'bon' + expect(reponse2.illustration.attached?).to be true + end + end end