Add File Validation concern after refactoring

This commit is contained in:
Ismael MOUSSA S 2021-09-20 19:51:20 +02:00 committed by Pierre de La Morinerie
parent 7c7c9c9ea4
commit 36dc9c2ca2
7 changed files with 28 additions and 37 deletions

View file

@ -14,6 +14,7 @@
class AttestationTemplate < ApplicationRecord class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern include TagsSubstitutionConcern
include FileValidationConcern
belongs_to :procedure, optional: false belongs_to :procedure, optional: false
@ -23,11 +24,8 @@ class AttestationTemplate < ApplicationRecord
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE = 1.megabytes FILE_MAX_SIZE = 1.megabytes
file_size_validation = Proc.new do validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE)
{ 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)) } validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE)
end
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation.call
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation.call
DOSSIER_STATE = Dossier.states.fetch(:accepte) DOSSIER_STATE = Dossier.states.fetch(:accepte)

View file

@ -17,6 +17,7 @@
# #
class Avis < ApplicationRecord class Avis < ApplicationRecord
include EmailSanitizableConcern include EmailSanitizableConcern
include FileValidationConcern
belongs_to :dossier, inverse_of: :avis, touch: true, optional: false belongs_to :dossier, inverse_of: :avis, touch: true, optional: false
belongs_to :experts_procedure, optional: false belongs_to :experts_procedure, optional: false
@ -28,21 +29,18 @@ class Avis < ApplicationRecord
has_one :procedure, through: :experts_procedure has_one :procedure, through: :experts_procedure
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
file_size_validation = Proc.new do
{ 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
validates :piece_justificative_file, validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation.call size: file_size_validation(FILE_MAX_SIZE)
validates :introduction_file, validates :introduction_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation.call size: file_size_validation(FILE_MAX_SIZE)
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :claimant, presence: true validates :claimant, presence: true
validates :piece_justificative_file, size: file_size_validation.call validates :piece_justificative_file, size: file_size_validation(FILE_MAX_SIZE)
validates :introduction_file, size: file_size_validation.call validates :introduction_file, size: file_size_validation(FILE_MAX_SIZE)
before_validation -> { sanitize_email(:email) } before_validation -> { sanitize_email(:email) }
default_scope { joins(:dossier) } default_scope { joins(:dossier) }

View file

@ -18,14 +18,11 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::PieceJustificativeChamp < Champ class Champs::PieceJustificativeChamp < Champ
include FileValidationConcern
FILE_MAX_SIZE = 200.megabytes FILE_MAX_SIZE = 200.megabytes
validates :piece_justificative_file, validates :piece_justificative_file,
size: { size: file_size_validation(FILE_MAX_SIZE),
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))
},
if: -> { !type_de_champ.skip_pj_validation } if: -> { !type_de_champ.skip_pj_validation }
validates :piece_justificative_file, validates :piece_justificative_file,

View file

@ -18,6 +18,7 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::TitreIdentiteChamp < Champ class Champs::TitreIdentiteChamp < Champ
include FileValidationConcern
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
ACCEPTED_FORMATS = [ ACCEPTED_FORMATS = [
@ -30,11 +31,7 @@ class Champs::TitreIdentiteChamp < Champ
# #
validates :piece_justificative_file, validates :piece_justificative_file,
content_type: ACCEPTED_FORMATS, content_type: ACCEPTED_FORMATS,
size: { size: file_size_validation(FILE_MAX_SIZE)
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))
}
def main_value_name def main_value_name
:piece_justificative_file :piece_justificative_file

View file

@ -12,6 +12,8 @@
# instructeur_id :bigint # instructeur_id :bigint
# #
class Commentaire < ApplicationRecord class Commentaire < ApplicationRecord
include FileValidationConcern
self.ignored_columns = [:user_id] self.ignored_columns = [:user_id]
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
@ -27,11 +29,7 @@ class Commentaire < ApplicationRecord
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe, validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: { size: file_size_validation(FILE_MAX_SIZE)
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))
}
default_scope { order(created_at: :asc) } default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }

View file

@ -0,0 +1,8 @@
module FileValidationConcern
extend ActiveSupport::Concern
class_methods do
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

View file

@ -52,6 +52,7 @@
class Procedure < ApplicationRecord class Procedure < ApplicationRecord
include ProcedureStatsConcern include ProcedureStatsConcern
include EncryptableConcern include EncryptableConcern
include FileValidationConcern
include Discard::Model include Discard::Model
self.discard_column = :hidden_at self.discard_column = :hidden_at
@ -242,9 +243,6 @@ class Procedure < ApplicationRecord
validates_with MonAvisEmbedValidator validates_with MonAvisEmbedValidator
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
file_size_validation = Proc.new do
{ 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
validates :notice, content_type: [ validates :notice, content_type: [
"application/msword", "application/msword",
"application/pdf", "application/pdf",
@ -257,7 +255,7 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "text/plain"
], size: file_size_validation.call, if: -> { new_record? || created_at > Date.new(2020, 2, 28) } ], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 2, 28) }
validates :deliberation, content_type: [ validates :deliberation, content_type: [
"application/msword", "application/msword",
@ -268,15 +266,12 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "text/plain"
], size: file_size_validation.call, if: -> { new_record? || created_at > Date.new(2020, 4, 29) } ], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 4, 29) }
LOGO_MAX_SIZE = 5.megabytes LOGO_MAX_SIZE = 5.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'],
size: { size: file_size_validation(LOGO_MAX_SIZE),
less_than: LOGO_MAX_SIZE, if: -> { new_record? || created_at > Date.new(2020, 11, 13) }
message: I18n.t('errors.messages.file_size_out_of_range',
file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(LOGO_MAX_SIZE))
}, if: -> { new_record? || created_at > Date.new(2020, 11, 13) }
validates :api_entreprise_token, jwt_token: true, allow_blank: true validates :api_entreprise_token, jwt_token: true, allow_blank: true
validates :api_particulier_token, format: { with: /\A[A-Za-z0-9\-_=.]{15,}\z/ }, allow_blank: true validates :api_particulier_token, format: { with: /\A[A-Za-z0-9\-_=.]{15,}\z/ }, allow_blank: true