Skip to content

Commit

Permalink
♻️ Remplace les chaînes de caractères par des constantes de type pour…
Browse files Browse the repository at this point in the history
… les questions dans l'administration et l'import/export
  • Loading branch information
Guitguitou committed Nov 15, 2024
1 parent 1299794 commit 7bd9dfa
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 39 deletions.
12 changes: 6 additions & 6 deletions app/admin/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def erreur_import(error)

def redirection_apres_import
redirection_paths = {
'QuestionClicDansImage' => admin_questions_clic_dans_image_path,
'QuestionGlisserDeposer' => admin_questions_glisser_deposer_path,
'QuestionQcm' => admin_question_qcms_path,
'QuestionSaisie' => admin_questions_saisies_path,
'QuestionSousConsigne' => admin_question_sous_consignes_path,
'QuestionClicDansTexte' => admin_questions_clic_dans_texte_path
QuestionClicDansImage.TYPE => admin_questions_clic_dans_image_path,
QuestionGlisserDeposer.TYPE => admin_questions_glisser_deposer_path,
QuestionQcm.TYPE => admin_question_qcms_path,
QuestionSaisie.TYPE => admin_questions_saisies_path,
QuestionSousConsigne.TYPE => admin_question_sous_consignes_path,
QuestionClicDansTexte.TYPE => admin_questions_clic_dans_texte_path
}

redirection_paths[params[:type]] || admin_questions_path
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

action_item :importer_question, only: :index do
link_to 'Importer questions clic dans image',
admin_import_xls_path(type: 'QuestionClicDansImage')
admin_import_xls_path(type: QuestionClicDansImage.TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_clic_dans_texte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

action_item :importer_question, only: :index do
link_to 'Importer questions Clic dans texte',
admin_import_xls_path(type: 'QuestionClicDansTexte')
admin_import_xls_path(type: QuestionClicDansTexte.TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

action_item :importer_question, only: :index do
link_to 'Importer questions glisser déposer',
admin_import_xls_path(type: 'QuestionGlisserDeposer')
admin_import_xls_path(type: QuestionGlisserDeposer.TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_qcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
form partial: 'form'

action_item :importer_question, only: :index do
link_to 'Importer questions QCM', admin_import_xls_path(type: 'QuestionQcm')
link_to 'Importer questions QCM', admin_import_xls_path(type: QuestionQcm.TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_saisie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
filter :nom_technique

action_item :importer_question, only: :index do
link_to 'Importer questions saisies', admin_import_xls_path(type: 'QuestionSaisie')
link_to 'Importer questions saisies', admin_import_xls_path(type: QuestionSaisie.TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
3 changes: 2 additions & 1 deletion app/admin/questions_sous_consigne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
filter :nom_technique

action_item :importer_question, only: :index do
link_to 'Importer questions sous consigne', admin_import_xls_path(type: 'QuestionSousConsigne')
link_to 'Importer questions sous consigne',
admin_import_xls_path(type: QuestionSousConsigne.TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
8 changes: 4 additions & 4 deletions app/models/import_export/questions/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def remplis_champs_additionnels

def remplis_champs_specifiques
case @question.type
when 'QuestionClicDansImage' then remplis_champs_clic_dans_image
when 'QuestionGlisserDeposer' then remplis_champs_glisser_deposer
when QuestionClicDansImage.TYPE then remplis_champs_clic_dans_image
when QuestionGlisserDeposer.TYPE then remplis_champs_glisser_deposer
when 'QuestionQcm' then remplis_champs_qcm
when 'QuestionSaisie' then remplis_champs_saisie
when 'QuestionClicDansTexte' then remplis_champs_clic_dans_texte
when QuestionSaisie.TYPE then remplis_champs_saisie
when QuestionClicDansTexte.TYPE then remplis_champs_clic_dans_texte
end
end

Expand Down
10 changes: 5 additions & 5 deletions app/models/import_export/questions/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def cree_transcription(categorie, audio_url, ecrit)

def update_champs_specifiques
updates = {
'QuestionClicDansImage' => :update_clic_dans_image,
'QuestionGlisserDeposer' => :update_glisser_deposer,
'QuestionQcm' => :update_qcm,
'QuestionSaisie' => :update_saisie,
'QuestionClicDansTexte' => :update_clic_dans_texte
QuestionClicDansImage.TYPE => :update_clic_dans_image,
QuestionGlisserDeposer.TYPE => :update_glisser_deposer,
QuestionQcm.TYPE => :update_qcm,
QuestionSaisie.TYPE => :update_saisie,
QuestionClicDansTexte.TYPE => :update_clic_dans_texte
}
send(updates[@type]) if updates.key?(@type)
end
Expand Down
12 changes: 6 additions & 6 deletions app/models/import_export/questions/import_export_donnees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class ImportExportDonnees
intitule_audio].freeze
HEADERS_COMMUN = %i[libelle nom_technique illustration intitule_ecrit intitule_audio
consigne_ecrit consigne_audio description].freeze
HEADERS_ATTENDUS = { 'QuestionClicDansImage' => HEADERS_COMMUN + HEADERS_CLIC_DANS_IMAGE,
'QuestionClicDansTexte' => HEADERS_COMMUN + HEADERS_CLIC_DANS_TEXTE,
'QuestionGlisserDeposer' => HEADERS_COMMUN + HEADERS_GLISSER_DEPOSER,
'QuestionQcm' => HEADERS_COMMUN + HEADERS_QCM,
'QuestionSaisie' => HEADERS_COMMUN + HEADERS_SAISIE,
'QuestionSousConsigne' => HEADERS_SOUS_CONSIGNE }.freeze
HEADERS_ATTENDUS = { QuestionClicDansImage.TYPE => HEADERS_COMMUN + HEADERS_CLIC_DANS_IMAGE,
QuestionClicDansTexte.TYPE => HEADERS_COMMUN + HEADERS_CLIC_DANS_TEXTE,
QuestionGlisserDeposer.TYPE => HEADERS_COMMUN + HEADERS_GLISSER_DEPOSER,
QuestionQcm.TYPE => HEADERS_COMMUN + HEADERS_QCM,
QuestionSaisie.TYPE => HEADERS_COMMUN + HEADERS_SAISIE,
QuestionSousConsigne.TYPE => HEADERS_SOUS_CONSIGNE }.freeze

def initialize(questions: nil, type: nil)
@questions = questions
Expand Down
18 changes: 9 additions & 9 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class Question < ApplicationRecord
CATEGORIE = %i[situation scolarite sante appareils].freeze
AUDIO_TYPES = %i[intitule modalite_reponse consigne].freeze
INTERACTION_TYPES = {
'QuestionQcm' => 'qcm',
'QuestionClicDansImage' => 'clic dans image',
'QuestionGlisserDeposer' => 'glisser deposer',
'QuestionSousConsigne' => 'sous consigne',
'QuestionSaisie' => 'saisie',
'QuestionClicDansTexte' => 'clic dans texte'
QuestionQcm.TYPE => 'qcm',
QuestionClicDansImage.TYPE => 'clic dans image',
QuestionGlisserDeposer.TYPE => 'glisser deposer',
QuestionSousConsigne.TYPE => 'sous consigne',
QuestionSaisie.TYPE => 'saisie',
QuestionClicDansTexte.TYPE => 'clic dans texte'
}.freeze

enum :categorie, CATEGORIE.zip(CATEGORIE.map(&:to_s)).to_h, prefix: true
Expand Down Expand Up @@ -84,15 +84,15 @@ def interaction
end

def saisie?
type == 'QuestionSaisie'
type == QuestionSaisie.TYPE
end

def qcm?
type == 'QuestionQcm'
type == QuestionQcm.TYPE
end

def sous_consigne?
type == 'QuestionSousConsigne'
type == QuestionSousConsigne.TYPE
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/question_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class QuestionClicDansImage < Question
CLASS_BONNE_REPONSE = 'bonne-reponse'
TYPE = 'QuestionClicDansImage'

has_one_attached :zone_cliquable
has_one_attached :image_au_clic
Expand Down
2 changes: 1 addition & 1 deletion app/models/question_clic_dans_texte.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class QuestionClicDansTexte < Question
# validates :texte_sur_illustration, presence: true
TYPE = 'QuestionClicDansTexte'

def as_json(_options = nil)
base_json
Expand Down
1 change: 1 addition & 0 deletions app/models/question_glisser_deposer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class QuestionGlisserDeposer < Question
TYPE = 'QuestionGlisserDeposer'
has_many :reponses, -> { order(position: :asc) },
foreign_key: :question_id,
class_name: 'Choix',
Expand Down
1 change: 1 addition & 0 deletions app/models/question_qcm.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class QuestionQcm < Question
TYPE = 'QuestionQcm'
enum :metacompetence, { numeratie: 0, ccf: 1, 'syntaxe-orthographe': 2 }
enum :type_qcm, { standard: 0, jauge: 1 }
has_many :choix, lambda {
Expand Down
2 changes: 2 additions & 0 deletions app/models/question_saisie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class QuestionSaisie < Question
QUESTION_REDACTION = 'redaction_note'
TYPE = 'QuestionSaisie'

enum :type_saisie, { redaction: 0, numerique: 1 }

has_one :bonne_reponse, class_name: 'Choix', foreign_key: :question_id, dependent: :destroy
Expand Down
2 changes: 2 additions & 0 deletions app/models/question_sous_consigne.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class QuestionSousConsigne < Question
TYPE = 'QuestionSousConsigne'

def as_json(_options = nil)
json = slice(:id, :nom_technique)
json['type'] = 'sous-consigne'
Expand Down
4 changes: 2 additions & 2 deletions app/models/restitution/questions_reponses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def choix_repondu(question, evenement_reponse)

def questions_repondues
questions_ids = reponses.collect { |r| r.donnees['question'] }
Question.where(id: questions_ids, type: 'QuestionQcm').includes(:choix) +
Question.where(id: questions_ids).where.not(type: 'QuestionQcm')
Question.where(id: questions_ids, type: QuestionQcm.TYPE).includes(:choix) +
Question.where(id: questions_ids).where.not(type: QuestionQcm.TYPE)
end
end
end

0 comments on commit 7bd9dfa

Please sign in to comment.