demarches-normaliennes/app/models/champs/piece_justificative_champ.rb

62 lines
1.8 KiB
Ruby
Raw Normal View History

2020-08-06 16:35:45 +02:00
# == Schema Information
#
# Table name: champs
#
# id :integer not null, primary key
# data :jsonb
# fetch_external_data_exceptions :string is an Array
2022-12-01 12:00:21 +01:00
# prefilled :boolean default(FALSE)
# private :boolean default(FALSE), not null
# rebased_at :datetime
# row :integer
# type :string
# value :string
2021-10-05 15:37:13 +02:00
# value_json :jsonb
# created_at :datetime
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
2020-08-06 16:35:45 +02:00
#
2018-02-13 18:18:20 +01:00
class Champs::PieceJustificativeChamp < Champ
FILE_MAX_SIZE = 200.megabytes
validates :piece_justificative_file,
size: { less_than: FILE_MAX_SIZE },
if: -> { !type_de_champ.skip_pj_validation }
2021-01-18 12:34:52 +01:00
validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES,
if: -> { !type_de_champ.skip_content_type_pj_validation }
def main_value_name
:piece_justificative_file
end
def search_terms
# We dont know how to search inside documents yet
end
def mandatory_blank?
mandatory? && !piece_justificative_file.attached?
end
def for_export
piece_justificative_file.map { _1.filename.to_s }.join(', ')
end
def for_api
return nil unless piece_justificative_file.attached?
# API v1 don't support multiple PJ
attachment = piece_justificative_file.first
return nil if attachment.nil?
if attachment.virus_scanner.safe? || attachment.virus_scanner.pending?
attachment.service_url
end
end
2018-02-13 18:18:20 +01:00
end