-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Crée une tâche rake pour créé les attachements avec active storage
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rake_logger' | ||
|
||
# rubocop:disable Naming/VariableNumber | ||
NOM_TECHNIQUES_QCM = { | ||
lodi_1: 'programme_tele', | ||
lodi_2: 'programme_tele_cirque', | ||
lodi_3: 'programme_tele_18h55', | ||
lodi_4: 'programme_tele_18h55', | ||
lodi_5: 'programme_tele_18h55', | ||
lodi_6: 'programme_tele_zoom', | ||
lodi_7: 'programme_tele_zoom', | ||
lodi_8: 'programme_tele_zoom', | ||
lodi_9: 'programme_tele_zoom', | ||
lodi_10: 'programme_tele_zoom', | ||
lodi_11: 'programme_tele_zoom', | ||
lodi_12: 'programme_tele_zoom', | ||
lodi_13: 'programme_tele_zoom', | ||
titre_1: 'liste_titres_musique', | ||
titre_2: 'liste_titres_musique', | ||
titre_10: 'liste_titres_musique', | ||
titre_3: 'liste_titres_musique', | ||
titre_11: 'liste_titres_musique', | ||
titre_6: 'liste_titres_musique', | ||
titre_8: 'liste_titres_musique', | ||
titre_5: 'liste_titres_musique', | ||
titre_4: 'liste_titres_musique', | ||
titre_7: 'liste_titres_musique', | ||
acrd_6: 'magazine_sans_texte', | ||
acrd_7: 'magazine_sans_texte', | ||
acrd_8: 'magazine_sans_texte', | ||
acrd_9: 'magazine_sans_texte', | ||
acrd_10: 'magazine_sans_texte', | ||
hpar_2: 'journal_avec_nouvelle', | ||
hpar_3: 'journal_avec_nouvelle_zoom', | ||
hcvf_3: 'rubrique_environnement', | ||
hcvf_4: 'rubrique_environnement' | ||
}.freeze | ||
# rubocop:enable Naming/VariableNumber | ||
|
||
namespace :questions do | ||
desc 'Attache les assets de cafe de la place aux instances de Question correpondantes' | ||
task attache_assets: :environment do | ||
NOM_TECHNIQUES_QCM.each do |nom_technique_qcm, nom_illustration| | ||
question = Question.find_by(nom_technique: nom_technique_qcm) | ||
puts "Pas de question trouvée pour le nom_technique '#{nom_technique_qcm}'" unless question | ||
next unless question | ||
|
||
attache_illustration(question, nom_technique_qcm, nom_illustration) | ||
attach_audio_intitule(question, nom_technique_qcm) | ||
attach_audio_choix(question, nom_technique_qcm) | ||
end | ||
puts 'Création des assets finie.' | ||
end | ||
|
||
# task audio_to_questions: :environment do | ||
# attache_audio | ||
# puts 'Création des audios finie.' | ||
# end | ||
end | ||
|
||
def attache_illustration(question, nom_technique_qcm, nom_illustration) | ||
chemin = chemin_du_fichier(nom_illustration, 'images', 'png') | ||
attach_file_to_question(question, chemin, :illustration, nom_technique_qcm) | ||
end | ||
|
||
def attach_audio_intitule(question, nom_technique_qcm) | ||
chemin = chemin_du_fichier(nom_technique_qcm, 'audio_questions', 'mp3') | ||
attach_file_to_question(question.transcription_intitule, chemin, :audio, | ||
nom_technique_qcm) | ||
end | ||
|
||
def attach_audio_choix(question, nom_technique_qcm) | ||
question.choix.each do |choix| | ||
chemin = chemin_du_fichier(choix.nom_technique, 'audio_reponses', 'mp3') | ||
attach_file_to_question(choix, chemin, :audio, nom_technique_qcm) | ||
end | ||
end | ||
|
||
def attach_file_to_question(instance, chemin_du_fichier, attachment_type, nom_technique_qcm) | ||
instance.send(attachment_type).attach( | ||
io: File.open(chemin_du_fichier), | ||
filename: File.basename(chemin_du_fichier), | ||
content_type: content_type_for(chemin_du_fichier) | ||
) | ||
puts "#{File.basename(chemin_du_fichier)} est rattaché à la question ##{nom_technique_qcm}" | ||
end | ||
|
||
def content_type_for(chemin_du_fichier) | ||
case File.extname(chemin_du_fichier) | ||
when '.mp3' then 'audio/mpeg' | ||
when '.png' then 'image/png' | ||
else 'application/octet-stream' | ||
end | ||
end | ||
|
||
def chemin_du_fichier(nom_fichier, folder, extension) | ||
Rails.root.join("app/assets/cafe_de_la_place/#{folder}/#{nom_fichier}.#{extension}") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe 'questions:attache_assets' do | ||
include_context 'rake' | ||
let(:logger) { RakeLogger.logger } | ||
let!(:question) { create :question_qcm, nom_technique: 'lodi_1' } | ||
let!(:transcription) { create :transcription, question_id: question.id, categorie: :intitule } | ||
let!(:choix) { create :choix, :bon, question_id: question.id, nom_technique: 'LOdi/couverture' } | ||
let!(:choix2) do | ||
create :choix, :mauvais, question_id: question.id, nom_technique: 'ALrd/jazz_a_dimoudon' | ||
end | ||
|
||
before do | ||
allow(logger).to receive :info | ||
subject.invoke | ||
end | ||
|
||
context "attache l'illustration à la question correspondante" do | ||
it { expect(question.reload.illustration.attached?).to be true } | ||
end | ||
|
||
context "attache l'audio à l'intitulé de la question correspondante" do | ||
it { expect(question.reload.transcription_intitule.audio.attached?).to be true } | ||
end | ||
|
||
context "attache l'audio aux choix de la question correspondante" do | ||
it { expect(question.reload.choix[0].audio.attached?).to be true } | ||
it { expect(question.reload.choix[1].audio.attached?).to be true } | ||
end | ||
end |