Skip to content

Commit

Permalink
♻️ Supprime duplication du model QuestionClicDansImage
Browse files Browse the repository at this point in the history
  • Loading branch information
cprodhomme committed Oct 7, 2024
1 parent 057f2ef commit 7eccc24
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 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

validates :zone_cliquable,
Expand All @@ -21,10 +23,7 @@ def clic_multiple?

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)
end

private
Expand Down Expand Up @@ -53,16 +52,23 @@ def valide_zone_cliquable_avec_reponse

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

return unless elements_cliquables.empty?

errors.add(:zone_cliquable, "doit contenir la classe 'bonne_reponse'")
errors.add(:zone_cliquable, "doit contenir la classe '#{CLASS_BONNE_REPONSE}'")
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)
doc = Nokogiri::XML(svg_content, nil, 'UTF-8')
elements_cliquables = doc.css(".#{CLASS_BONNE_REPONSE}")

elements_cliquables.size > 1
end
end
2 changes: 1 addition & 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,7 @@
end

it do
expect(page).to have_content("doit contenir la classe 'bonne_reponse'")
expect(page).to have_content("doit contenir la classe 'bonne-reponse'")
expect(Question.count).to eq(0)
end
end
Expand Down

0 comments on commit 7eccc24

Please sign in to comment.