Skip to content

Commit

Permalink
♻️ Passe en has_one_attached pour la zone de dépot
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitguitou authored and cprodhomme committed Oct 1, 2024
1 parent 8aa7d97 commit e304665
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/admin/questions_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

permit_params :libelle, :nom_technique, :description, :illustration, :supprimer_illustration,
:supprimer_audio_modalite_reponse, :supprimer_audio_intitule,
:zones_depot_url, :supprimer_zones_depot_url,
:supprimer_zone_depot_url, :zone_depot_url,
transcriptions_attributes: %i[id categorie ecrit audio _destroy],
reponses_attributes: %i[id illustration position type_choix position_client
nom_technique _destroy]
Expand Down
50 changes: 20 additions & 30 deletions app/models/question_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class QuestionGlisserDeposer < Question
dependent: :destroy
accepts_nested_attributes_for :reponses, allow_destroy: true

has_many_attached :zones_depot_url
has_one_attached :zone_depot_url

validates :zones_depot_url,
validates :zone_depot_url,
blob: { content_type: 'image/svg+xml' }

attr_accessor :supprimer_zones_depot_url
attr_accessor :supprimer_zone_depot_url

before_save :valide_zones_depot_url_avec_reponse
after_update :supprime_zones_depot_url
before_save :valide_zone_depot_url_avec_reponse
after_update :supprime_zone_depot_url

def as_json(_options = nil)
json = base_json
Expand Down Expand Up @@ -43,38 +43,28 @@ def reponses_fields
{ 'reponsesNonClassees' => reponses_non_classees }
end

def supprime_zones_depot_url
zones_depot_url.purge_later if zones_depot_url.attached? && supprimer_zones_depot_url == '1'
def supprime_zone_depot_url
zone_depot_url.purge_later if zone_depot_url.attached? && supprimer_zone_depot_url == '1'
end

def valide_zones_depot_url_avec_reponse
return unless zones_depot_url_attached?
return if zones_depot_url_changes_nil?
def valide_zone_depot_url_avec_reponse
return unless zone_depot_url.attached?
return if attachment_changes['zone_depot_url'].nil?

attachment_changes['zones_depot_url'].attachable.each do |file|
doc = Nokogiri::XML(file.download, nil, 'UTF-8')
file = attachment_changes['zone_depot_url'].attachable
doc = Nokogiri::XML(file, nil, 'UTF-8')
return unless elements_depot(doc).empty?

if zones_depot_invalid?(doc)
add_invalid_zones_depot_error
throw(:abort)
end
end
end

def zones_depot_url_attached?
zones_depot_url.attached?
invalid_zone_depot_error(reponse)
end

def zones_depot_url_changes_nil?
attachment_changes['zones_depot_url'].nil?
end

def zones_depot_invalid?(doc)
doc.css('.zone-depot').empty? || doc.css(".zone-depot--#{reponse.nom_technique}").empty?
def invalid_zone_depot_error(reponse)
errors.add(:zone_depot_url,
"doit contenir les classes 'zone-depot' et 'zone-depot--#{reponse.nom_technique}'")
throw(:abort)
end

def add_invalid_zones_depot_error
errors.add(:zones_depot_url,
"doit contenir les classes 'zone-depot' et 'zone-depot--#{reponse.nom_technique}'")
def elements_depot(doc)
doc.css('.zone-depot') || doc.css(".zone-depot--#{reponse.nom_technique}")
end
end
19 changes: 11 additions & 8 deletions app/views/admin/questions_glisser_deposer/_form.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ active_admin_form_for [:admin, resource] do |f|
f.input :nom_technique
f.input :description
render partial: 'admin/questions/input_illustration', locals: { f: f }
f.input :zones_depot_url,
f.input :zone_depot_url,
as: :file,
hint: "L'un des éléments cliquables doit contenir la classe css `zone-depot` " \
hint: 'Les éléments doivent contenir la classe css `zone-depot` ' \
'et la classe css `zone-depot--reponse.nom_technique`',
input_html: { accept: 'image/svg+xml', multiple: true }
if f.object.zones_depot_url.attached? && f.object.errors[:zones_depot_url].blank?
f.input :supprimer_zones_depot_url,
as: :boolean,
label: t('.label.supprimer_zones_depot_url'),
hint: svg_attachment_base64(resource.zones_depot_url, class: 'image-preview')
input_html: { accept: 'image/svg+xml' }

if f.object.zone_depot_url.attached? && f.object.errors[:zone_depot_url].blank?
f.object.zone_depot_url.each_with_index do |file, index|
f.input "supprimer_zone_depot_url_#{index}",
as: :boolean,
label: t('.label.supprimer_zone_depot_url'),
hint: svg_attachment_base64(file, class: 'image-preview')
end
end
end
f.inputs do
Expand Down
14 changes: 4 additions & 10 deletions app/views/admin/questions_glisser_deposer/_show.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ panel 'Détails de la question' do
end
end
end
row :zones_depot_url do
if resource.zones_depot_url.attached?
ul do
resource.zones_depot_url.each do |zone_depot_url|
li do
link_to(cdn_for(zone_depot_url), target: '_blank', rel: 'noopener') do
inline_svg_content(zone_depot_url, class: 'image-preview')
end
end
end
row :zone_depot_url do
if resource.zone_depot_url.attached?
link_to(cdn_for(resource.zone_depot_url), target: '_blank', rel: 'noopener') do
raw inline_svg_content(resource.zone_depot_url, class: 'image-preview')
end
end
end
Expand Down

0 comments on commit e304665

Please sign in to comment.