From 60c2718f29c135097719f1751dc5448a5f2db4eb Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 24 Nov 2021 09:04:20 +0000 Subject: [PATCH] models: remove custom code for file size validation message With active_storage_validations 0.9.6, we can use the %{max_size} variable directly in the error message. --- app/models/attestation_template.rb | 5 ++--- app/models/avis.rb | 9 ++++----- app/models/champs/piece_justificative_champ.rb | 3 +-- app/models/champs/titre_identite_champ.rb | 15 ++------------- app/models/commentaire.rb | 3 +-- app/models/concerns/file_validation_concern.rb | 12 ------------ app/models/procedure.rb | 7 +++---- config/i18n-tasks.yml | 1 + config/locales/active_storage_validations.en.yml | 4 ++-- config/locales/active_storage_validations.fr.yml | 4 ++-- 10 files changed, 18 insertions(+), 45 deletions(-) delete mode 100644 app/models/concerns/file_validation_concern.rb diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 4414167aa..16f463d7f 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -14,7 +14,6 @@ class AttestationTemplate < ApplicationRecord include ActionView::Helpers::NumberHelper include TagsSubstitutionConcern - include FileValidationConcern belongs_to :procedure, optional: false @@ -24,8 +23,8 @@ class AttestationTemplate < ApplicationRecord validates :footer, length: { maximum: 190 } FILE_MAX_SIZE = 1.megabytes - validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE) - validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE) + validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE } + validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE } DOSSIER_STATE = Dossier.states.fetch(:accepte) diff --git a/app/models/avis.rb b/app/models/avis.rb index 2f56b1b89..3065aadf8 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -17,7 +17,6 @@ # class Avis < ApplicationRecord include EmailSanitizableConcern - include FileValidationConcern belongs_to :dossier, inverse_of: :avis, touch: true, optional: false belongs_to :experts_procedure, optional: false @@ -31,16 +30,16 @@ class Avis < ApplicationRecord FILE_MAX_SIZE = 20.megabytes validates :piece_justificative_file, content_type: AUTHORIZED_CONTENT_TYPES, - size: file_size_validation(FILE_MAX_SIZE) + size: { less_than: FILE_MAX_SIZE } validates :introduction_file, content_type: AUTHORIZED_CONTENT_TYPES, - size: file_size_validation(FILE_MAX_SIZE) + size: { less_than: FILE_MAX_SIZE } validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :claimant, presence: true - validates :piece_justificative_file, size: file_size_validation(FILE_MAX_SIZE) - validates :introduction_file, size: file_size_validation(FILE_MAX_SIZE) + validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE } + validates :introduction_file, size: { less_than: FILE_MAX_SIZE } before_validation -> { sanitize_email(:email) } default_scope { joins(:dossier) } diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 5076f963a..ecd534fd7 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -20,11 +20,10 @@ # type_de_champ_id :integer # class Champs::PieceJustificativeChamp < Champ - include FileValidationConcern FILE_MAX_SIZE = 200.megabytes validates :piece_justificative_file, - size: file_size_validation(FILE_MAX_SIZE), + size: { less_than: FILE_MAX_SIZE }, if: -> { !type_de_champ.skip_pj_validation } validates :piece_justificative_file, diff --git a/app/models/champs/titre_identite_champ.rb b/app/models/champs/titre_identite_champ.rb index 1d74c3245..7fa81233d 100644 --- a/app/models/champs/titre_identite_champ.rb +++ b/app/models/champs/titre_identite_champ.rb @@ -20,20 +20,9 @@ # type_de_champ_id :integer # class Champs::TitreIdentiteChamp < Champ - include FileValidationConcern FILE_MAX_SIZE = 20.megabytes - - ACCEPTED_FORMATS = [ - "image/png", - "image/jpeg" - ] - - # TODO: once we're running on Rails 6, re-enable this validation. - # See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926 - # - validates :piece_justificative_file, - content_type: ACCEPTED_FORMATS, - size: file_size_validation(FILE_MAX_SIZE) + ACCEPTED_FORMATS = ['image/png', 'image/jpeg'] + validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE } def main_value_name :piece_justificative_file diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 6e430649e..b98cc70c3 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -13,7 +13,6 @@ # instructeur_id :bigint # class Commentaire < ApplicationRecord - include FileValidationConcern include Discard::Model self.ignored_columns = [:user_id] @@ -31,7 +30,7 @@ class Commentaire < ApplicationRecord FILE_MAX_SIZE = 20.megabytes validates :piece_jointe, content_type: AUTHORIZED_CONTENT_TYPES, - size: file_size_validation(FILE_MAX_SIZE) + size: { less_than: FILE_MAX_SIZE } default_scope { order(created_at: :asc) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } diff --git a/app/models/concerns/file_validation_concern.rb b/app/models/concerns/file_validation_concern.rb deleted file mode 100644 index a3237d43f..000000000 --- a/app/models/concerns/file_validation_concern.rb +++ /dev/null @@ -1,12 +0,0 @@ -module FileValidationConcern - extend ActiveSupport::Concern - class_methods do - # This method works around missing `%{min_size}` and `%{max_size}` variables in active_record_validation - # default error message. - # - # Hopefully this will be fixed upstream in https://github.com/igorkasyanchuk/active_storage_validations/pull/134 - def file_size_validation(file_max_size = 200.megabytes) - { less_than: file_max_size, message: I18n.t('errors.messages.file_size_out_of_range', file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(file_max_size)) } - end - end -end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 2d2289148..cb2bfad02 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -54,7 +54,6 @@ class Procedure < ApplicationRecord self.ignored_columns = [:duree_conservation_dossiers_hors_ds] include ProcedureStatsConcern include EncryptableConcern - include FileValidationConcern include Discard::Model self.discard_column = :hidden_at @@ -253,7 +252,7 @@ class Procedure < ApplicationRecord "image/jpg", "image/png", "text/plain" - ], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 2, 28) } + ], size: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 2, 28) } validates :deliberation, content_type: [ "application/msword", @@ -264,11 +263,11 @@ class Procedure < ApplicationRecord "image/jpg", "image/png", "text/plain" - ], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 4, 29) } + ], size: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 4, 29) } LOGO_MAX_SIZE = 5.megabytes validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], - size: file_size_validation(LOGO_MAX_SIZE), + size: { less_than: LOGO_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 11, 13) } validates :api_entreprise_token, jwt_token: true, allow_blank: true diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 0d03716b7..a4e992c04 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -101,6 +101,7 @@ ignore_unused: - 'activerecord.errors.*' - 'errors.messages.blank' - 'errors.messages.content_type_invalid' +- 'errors.messages.file_size_out_of_range' - 'pluralize.*' - 'views.pagination.*' - 'time.formats.default' diff --git a/config/locales/active_storage_validations.en.yml b/config/locales/active_storage_validations.en.yml index a73f5e3a6..5297cf5a9 100644 --- a/config/locales/active_storage_validations.en.yml +++ b/config/locales/active_storage_validations.en.yml @@ -1,5 +1,5 @@ en: errors: messages: - content_type_invalid: "is not of an accepted type" - file_size_out_of_range: "is too big. The file must be at most %{file_size_limit}." + content_type_invalid: is not of an accepted type + file_size_out_of_range: is too big. The file must be at most %{max_size}. diff --git a/config/locales/active_storage_validations.fr.yml b/config/locales/active_storage_validations.fr.yml index 1fcec73ce..596e836b8 100644 --- a/config/locales/active_storage_validations.fr.yml +++ b/config/locales/active_storage_validations.fr.yml @@ -1,5 +1,5 @@ fr: errors: messages: - content_type_invalid: "n’est pas d’un type accepté" - file_size_out_of_range: "est trop lourd(e). Le fichier doit faire au plus %{file_size_limit}." + content_type_invalid: n’est pas d’un type accepté + file_size_out_of_range: est trop lourd(e). Le fichier doit faire au plus %{max_size}.