Skip to content

Commit

Permalink
✨ Mets à jour les champs spécifiques d'une question glisser déposer
Browse files Browse the repository at this point in the history
  • Loading branch information
marouria committed Oct 31, 2024
1 parent aaabf5a commit 0521e5d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
30 changes: 23 additions & 7 deletions app/models/import_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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)
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions spec/models/import_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0521e5d

Please sign in to comment.