Skip to content

Commit

Permalink
♿️ Ajoute une règles custom rubocop pour svg_tag et svg_attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneCharignon committed Jan 8, 2025
1 parent 3d60e5a commit a90d5ea
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
inherit_from: .rubocop_todo.yml

require:
- ./spec/rubocop/cop/custom/svg_image_alt_tag.rb
- rubocop-rails-accessibility
- rubocop-packaging
- rubocop-rails
Expand All @@ -21,6 +22,9 @@ AllCops:
- 'vendor/**/*'
- 'spec/spec_helper.rb'

Custom/SvgImageAltTag:
Enabled: true

Style/Documentation:
Enabled: false

Expand Down
8 changes: 4 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def rapport_colonne_class
'col-4 px-5 mb-4'
end

def svg_tag_base64(chemin_avec_extension, alt, classes)
def svg_tag_base64(chemin_avec_extension, options = {})
## Ne pas oublier de rajouter l'extension au path sinon ça ne build pas en production

raw = Rails.application.assets_manifest.find_sources(chemin_avec_extension).first
image_tag fichier_encode_en_base64(raw), alt: alt, class: classes
image_tag fichier_encode_en_base64(raw), alt: options[:alt], class: options[:class]
end

def cdn_for(fichier)
Expand All @@ -39,11 +39,11 @@ def cdn_for(fichier)
"#{ENV.fetch('PROTOCOLE_SERVEUR')}://#{ENV.fetch('HOTE_STOCKAGE')}/#{fichier.key}?#{param}"
end

def svg_attachment_base64(attachment, alt, classes)
def svg_attachment_base64(attachment, options = {})
return unless attachment.attached?

file_content = attachment.download
image_tag fichier_encode_en_base64(file_content), alt: alt, class: classes
image_tag fichier_encode_en_base64(file_content), alt: options[:alt], class: options[:class]
end

def inline_svg_content(attachment, options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ div class: 'contenu-sans-evaluations' do
h3 t('.titre')
div t('.action'), class: 'bouton grand-bouton bouton--desactive'
end
div svg_tag_base64 'panneaux.svg', class: 'illustration'
div image_tag 'panneaux.svg', class: 'illustration', alt: ''
div class: 'description' do
text_node md t('.description')
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/evaluations/_competences_transversales.arb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ div id: 'competences_transversales', class: 'page' do
scope: 'admin.evaluations.restitution_competence')
div class: 'lien-metier' do
if pdf
text_node svg_tag_base64 'lien.svg', class: 'image-lien'
text_node svg_tag_base64 'lien.svg', class: 'image-lien', alt: ''
else
text_node image_tag 'lien.svg', alt: '', class: 'image-lien'
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/evaluations/_correspondance_anlci.arb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ div id: 'correspondance_anlci', class: 'page' do
div class: 'row no-gutters recommandation-anlci marges-page' do
div class: 'col-auto' do
if pdf
svg_tag_base64 'logo_anlci.svg', class: 'pr-5'
svg_tag_base64 'logo_anlci.svg', class: 'pr-5', alt: t('logo_anlci')
else
image_tag 'logo_anlci.svg', class: 'pr-5', alt: t('logo_anlci')
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/questions/_input_illustration.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
f.input :illustration, as: :file,
input_html: { accept: illustration_content_types.join(',') },
hint: t('.illustration.hint')
if f.object.illustration.attached?
if resource.illustration.attached?
f.input :supprimer_illustration, as: :boolean,
label: t('.label.supprimer_illustration'),
hint: image_tag(cdn_for(f.object.illustration),
alt: f.object.illustration.filename)
hint: image_tag(cdn_for(resource.illustration),
alt: resource.illustration.filename)
end
8 changes: 6 additions & 2 deletions app/views/admin/questions_clic_dans_image/_form.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ active_admin_form_for [:admin, resource] do |f|
f.input :supprimer_zone_cliquable,
as: :boolean,
label: t('.label.supprimer_zone_cliquable'),
hint: svg_attachment_base64(resource.zone_cliquable, class: 'image-preview')
hint: svg_attachment_base64(resource.zone_cliquable,
class: 'image-preview',
alt: resource.zone_cliquable.filename)
end
f.input :image_au_clic,
label: t('.label.image_au_clic'),
Expand All @@ -24,7 +26,9 @@ active_admin_form_for [:admin, resource] do |f|
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')
hint: svg_attachment_base64(resource.image_au_clic,
class: 'image-preview',
alt: resource.image_au_clic.filename)
end
end

Expand Down
14 changes: 4 additions & 10 deletions app/views/admin/questions_clic_dans_image/_show.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ panel 'Détails de la question' do
locals: { illustration: resource.illustration }
end
row :zone_cliquable do
if resource.zone_cliquable.attached?
link_to(cdn_for(resource.zone_cliquable), target: '_blank', rel: 'noopener') do
raw inline_svg_content(resource.zone_cliquable, class: 'image-preview')
end
end
render partial: 'admin/questions/show_illustration',
locals: { illustration: resource.zone_cliquable }
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
render partial: 'admin/questions/show_illustration',
locals: { illustration: resource.image_au_clic }
end
intitule = question_clic_dans_image.transcription_intitule
row :intitule do
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/questions_glisser_deposer/_form.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ active_admin_form_for [:admin, resource] do |f|
f.input :supprimer_zone_depot,
as: :boolean,
label: t('.label.supprimer_zone_depot'),
hint: svg_attachment_base64(resource.zone_depot, class: 'image-preview')
hint: svg_attachment_base64(resource.zone_depot,
class: 'image-preview',
alt: resource.zone_depot.filename)
end
end
render partial: 'admin/questions/inputs_avec_transcriptions_audios',
Expand Down
7 changes: 2 additions & 5 deletions app/views/admin/questions_glisser_deposer/_show.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ panel 'Détails de la question' do
locals: { illustration: resource.illustration }
end
row :zone_depot do
if resource.zone_depot.attached?
link_to(cdn_for(resource.zone_depot), target: '_blank', rel: 'noopener') do
raw inline_svg_content(resource.zone_depot, class: 'image-preview')
end
end
render partial: 'admin/questions/show_illustration',
locals: { illustration: resource.zone_depot }
end
intitule = question_glisser_deposer.transcription_intitule
row :intitule do
Expand Down
2 changes: 1 addition & 1 deletion app/views/components/_banniere_confirmation_email.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
div class: 'card__banner card__banner--alert w-100 d-flex align-items-center bg-erreur' do
case demande
when :nouvelle_inscription
div svg_tag_base64 'icones/icone-enveloppe.svg', class: 'banner__icone'
div image_tag 'icones/icone-enveloppe.svg', class: 'banner__icone', alt: ''
div do
scope = 'components.prise_en_main.etapes.confirmation_email'
h4 t('titre', scope: scope), class: 'banner__titre'
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cop/custom/svg_image_alt_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Custom
class SvgImageAltTag < Base
MSG = 'svg_tag_base64 et svg_attachment_base64 doivent avoir un attribut alt'

def on_send(node)
unless NodePattern.new('(send nil? :svg_tag_base64 ...)').match(node) ||
NodePattern.new('(send nil? :svg_attachment_base64 ...)').match(node)
return
end

options = node.arguments.last
return unless options&.hash_type?

add_offense(node) unless options.pairs.any? { |pair| pair.key.value == :alt }
end
end
end
end
end

0 comments on commit a90d5ea

Please sign in to comment.