Skip to content

Commit

Permalink
🗃️ Ajoute la zone de dépot url dans glisser deposer
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitguitou committed Sep 25, 2024
1 parent 4cf8ea8 commit cf14738
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/models/question_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ class QuestionGlisserDeposer < Question
dependent: :destroy
accepts_nested_attributes_for :reponses, allow_destroy: true

has_many_attached :zones_depot_url

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

attr_accessor :supprimer_zones_depot_url

before_save :valide_zones_depot_url_avec_reponse
after_update :supprime_zones_depot_url

def as_json(_options = nil)
json = base_json
json.merge!(json_audio_fields, reponses_fields)
Expand All @@ -32,4 +42,39 @@ def reponses_fields
end
{ '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'
end

def valide_zones_depot_url_avec_reponse
return unless zones_depot_url_attached?
return if zones_depot_url_changes_nil?

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

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

def zones_depot_url_attached?
zones_depot_url.attached?
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?
end

def add_invalid_zones_depot_error
errors.add(:zones_depot_url,
"doit contenir les classes 'zone-depot' et 'zone-depot--#{reponse.nom_technique}'")
end
end
31 changes: 31 additions & 0 deletions spec/models/question_glisser_deposer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe QuestionGlisserDeposer, type: :model do
it { is_expected.to have_many(:reponses).with_foreign_key(:question_id) }
it { is_expected.to have_many_attached(:zones_depot_url) }

let(:question) do
create(:question_glisser_deposer,
Expand Down Expand Up @@ -42,4 +43,34 @@
end
end
end

describe 'validations' do
let(:question) do
build(:question_glisser_deposer)
end

context 'avec un attachment au format svg' do
it 'est valide' do
question.zones_depot_url.attach(
io: Rails.root.join('spec/support/accessibilite-sans-reponse.svg').open,
filename: 'valid.svg',
content_type: 'image/svg+xml'
)
expect(question).to be_valid
end
end

context "avec un attachement d'un autre format" do
it "n'est pas valide" do
question.zones_depot_url.attach(
io: Rails.root.join('spec/support/programme_tele.png').open,
filename: 'invalid.png',
content_type: 'image/png'
)
expect(question).not_to be_valid
erreur = question.errors[:zones_depot_url]
expect(erreur).to include("n'est pas un format de fichier valide")
end
end
end
end

0 comments on commit cf14738

Please sign in to comment.