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

✨ Permet d'ajouter une image au clic à positionner pour une question … #1690

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
2 changes: 2 additions & 0 deletions app/admin/questions_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
permit_params :libelle, :nom_technique, :description,
:illustration, :supprimer_illustration,
:supprimer_audio_intitule,
:image_au_clic,
:zone_cliquable, :supprimer_zone_cliquable,
:supprimer_audio_modalite_reponse,
:supprimer_image_au_clic,
transcriptions_attributes: %i[id categorie ecrit audio _destroy]

filter :libelle
Expand Down
12 changes: 10 additions & 2 deletions app/models/question_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

class QuestionClicDansImage < Question
has_one_attached :zone_cliquable
has_one_attached :image_au_clic

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

attr_accessor :supprimer_zone_cliquable
attr_accessor :supprimer_zone_cliquable, :supprimer_image_au_clic

before_save :valide_zone_cliquable_avec_reponse
after_update :supprime_zone_cliquable
after_update :supprime_zone_cliquable, :supprime_image_au_clic

def as_json(_options = nil)
json = base_json
json['image_au_clic'] = fichier_encode_base64(image_au_clic) if image_au_clic.attached?
json.merge!(json_audio_fields, additional_json_fields)
end

Expand Down Expand Up @@ -47,6 +51,10 @@ def supprime_zone_cliquable
zone_cliquable.purge_later if zone_cliquable.attached? && supprimer_zone_cliquable == '1'
end

def supprime_image_au_clic
image_au_clic.purge_later if image_au_clic.attached? && supprimer_image_au_clic == '1'
end

def valide_zone_cliquable_avec_reponse
return unless zone_cliquable.attached?
return if attachment_changes['zone_cliquable'].nil?
Expand Down
9 changes: 9 additions & 0 deletions app/views/admin/questions_clic_dans_image/_form.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ active_admin_form_for [:admin, resource] do |f|
label: t('.label.supprimer_zone_cliquable'),
hint: svg_attachment_base64(resource.zone_cliquable, class: 'image-preview')
end
f.input :image_au_clic,
label: t('.label.image_au_clic'),
as: :file,
input_html: { accept: 'image/svg+xml' }
if f.object.image_au_clic.attached?
f.input :supprimer_image_au_clic,
as: :boolean,
hint: svg_attachment_base64(resource.image_au_clic, class: 'image-preview')
end
end

f.inputs do
Expand Down
7 changes: 7 additions & 0 deletions app/views/admin/questions_clic_dans_image/_show.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ panel 'Détails de la question' do
end
end
end
row :image_au_clic do
if resource.image_au_clic.attached?
link_to(cdn_for(resource.image_au_clic), target: '_blank', rel: 'noopener') do
raw inline_svg_content(resource.image_au_clic, class: 'image-preview')
end
end
end
intitule = question_clic_dans_image.transcription_intitule
row :intitule do
div intitule.ecrit if intitule&.ecrit
Expand Down
6 changes: 6 additions & 0 deletions config/locales/views/questions_clic_dans_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fr:
admin:
questions_clic_dans_image:
form:
label:
image_au_clic: Image à positionner au clic
7 changes: 6 additions & 1 deletion spec/models/question_clic_dans_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe QuestionClicDansImage, type: :model do
it { is_expected.to have_one_attached(:zone_cliquable) }
it { is_expected.to have_one_attached(:image_au_clic) }

describe '#as_json' do
let(:question_clic_dans_image) do
Expand All @@ -13,6 +14,9 @@
),
zone_cliquable: Rack::Test::UploadedFile.new(
Rails.root.join('spec/support/accessibilite-avec-reponse.svg')
),
image_au_clic: Rack::Test::UploadedFile.new(
Rails.root.join('spec/support/accessibilite-avec-reponse.svg')
))
end

Expand All @@ -28,7 +32,7 @@
json = question_clic_dans_image.as_json
expect(json.keys)
.to match_array(%w[description id intitule audio_url nom_technique type illustration
modalite_reponse zone_cliquable])
modalite_reponse zone_cliquable image_au_clic])
expect(json['type']).to eql('clic-dans-image')
expect(json['intitule']).to eql('Mon Intitulé')
expect(json['illustration']).to eql(Rails.application.routes.url_helpers.url_for(
Expand All @@ -39,6 +43,7 @@
))
expect(json['modalite_reponse']).to eql(modalite.ecrit)
expect(json['zone_cliquable']).to start_with('data:image/svg+xml;base64,')
expect(json['image_au_clic']).to start_with('data:image/svg+xml;base64,')
end
end

Expand Down