Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supprime duplications pour les models questions #1687

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/models/choix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class Choix < ApplicationRecord

validate :audio_type

AUDIOS_CONTENT_TYPES = ['audio/mpeg', 'audio/mp4'].freeze

acts_as_list scope: :question_id

def as_json(_options = nil)
slice(:id, :intitule, :type_choix, :nom_technique)
end

def audio_type
return unless audio.attached? && !audio.content_type.in?(%w[audio/mpeg audio/mp4])
return unless audio.attached? && !audio.content_type.in?(AUDIOS_CONTENT_TYPES)

errors.add(:audio, 'doit être un fichier MP3 ou MP4')
audio.purge
Expand Down
25 changes: 15 additions & 10 deletions app/models/question_clic_dans_image.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class QuestionClicDansImage < Question
CLASS_BONNE_REPONSE = 'bonne-reponse'

has_one_attached :zone_cliquable
has_one_attached :image_au_clic

Expand All @@ -24,11 +26,7 @@ def clic_multiple?
return false unless zone_cliquable.attached?

svg_content = zone_cliquable.download

doc = Nokogiri::XML(svg_content, nil, 'UTF-8')
elements_cliquables = doc.css('.bonne-reponse')

elements_cliquables.size > 1
svg_contient_class_bonne_reponse?(svg_content, 2)
end

private
Expand Down Expand Up @@ -60,12 +58,19 @@ def valide_zone_cliquable_avec_reponse
return if attachment_changes['zone_cliquable'].nil?

file = attachment_changes['zone_cliquable'].attachable
doc = Nokogiri::XML(file, nil, 'UTF-8')
elements_cliquables = doc.css('.bonne-reponse')

return unless elements_cliquables.empty?
return if svg_contient_class_bonne_reponse?(file, 1)

errors.add(:zone_cliquable, "doit contenir la classe 'bonne_reponse'")
errors.add(:zone_cliquable, :class_bonne_reponse_not_found)
throw(:abort)
end

def fichier_encode_base64(attachment)
file_content = attachment.download
ApplicationController.helpers.fichier_encode_en_base64(file_content)
end

def svg_contient_class_bonne_reponse?(svg_content, minimum)
doc = Nokogiri::XML(svg_content, nil, 'UTF-8')
doc.css(".#{CLASS_BONNE_REPONSE}").size >= minimum
end
end
2 changes: 1 addition & 1 deletion app/models/transcription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Transcription < ApplicationRecord
enum :categorie, { intitule: 0, modalite_reponse: 1 }

def audio_type
return unless audio.attached? && !audio.content_type.in?(%w[audio/mpeg audio/mp4])
return unless audio.attached? && !audio.content_type.in?(AUDIOS_CONTENT_TYPES)

errors.add(:audio, 'doit être un fichier MP3 ou MP4')
audio.purge
Expand Down
6 changes: 6 additions & 0 deletions config/locales/models/question.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ fr:
bonne_reponse: Bonne réponse
question_glisser_deposer:
reponses: Réponses
errors:
models:
question_clic_dans_image:
attributes:
zone_cliquable:
class_bonne_reponse_not_found: "doit contenir la classe 'bonne-reponse'"
formtastic:
actions:
question: &question_actions
Expand Down
4 changes: 3 additions & 1 deletion spec/features/admin/question_clic_dans_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@
end

it do
expect(page).to have_content("doit contenir la classe 'bonne_reponse'")
expect(page).to have_content(
"doit contenir la classe '#{QuestionClicDansImage::CLASS_BONNE_REPONSE}'"
)
expect(Question.count).to eq(0)
end
end
Expand Down