2018-03-06 13:44:29 +01:00
class Champ < ApplicationRecord
2017-10-24 18:12:25 +02:00
belongs_to :dossier , touch : true
2018-02-09 17:38:30 +01:00
belongs_to :type_de_champ , inverse_of : :champ
2016-11-14 18:00:26 +01:00
has_many :commentaires
2018-01-30 19:17:16 +01:00
has_one_attached :piece_justificative_file
2018-05-11 14:59:20 +02:00
has_one :virus_scan
2016-03-15 17:17:56 +01:00
2018-02-09 17:42:53 +01:00
delegate :libelle , :type_champ , :order_place , :mandatory? , :description , :drop_down_list , to : :type_de_champ
2016-03-15 17:17:56 +01:00
2017-10-05 16:10:00 +02:00
scope :updated_since? , - > ( date ) { where ( 'champs.updated_at > ?' , date ) }
2018-02-14 11:46:38 +01:00
scope :public_only , - > { where ( private : false ) }
scope :private_only , - > { where ( private : true ) }
2017-10-05 16:10:00 +02:00
2018-01-30 19:17:16 +01:00
PIECE_JUSTIFICATIVE_FILE_MAX_SIZE = 200 . megabytes
PIECE_JUSTIFICATIVE_FILE_ACCEPTED_FORMATS = [
" application/pdf " ,
" application/msword " ,
" application/vnd.openxmlformats-officedocument.wordprocessingml.document " ,
" application/vnd.ms-excel " ,
" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " ,
" application/vnd.ms-powerpoint " ,
" application/vnd.openxmlformats-officedocument.presentationml.presentation " ,
" application/vnd.oasis.opendocument.text " ,
" application/vnd.oasis.opendocument.presentation " ,
" application/vnd.oasis.opendocument.spreadsheet " ,
" image/png " ,
" image/jpeg "
]
2018-02-09 17:38:30 +01:00
def public?
! private ?
end
2018-03-20 17:47:37 +01:00
def same_hour? ( num )
2016-06-22 11:25:41 +02:00
same_date? num , '%H'
end
2018-03-20 17:47:37 +01:00
def same_minute? ( num )
2016-06-22 11:25:41 +02:00
same_date? num , '%M'
end
2017-03-29 13:37:07 +02:00
def mandatory_and_blank?
2018-06-14 17:44:03 +02:00
mandatory? && value . blank?
2017-03-29 13:37:07 +02:00
end
2018-03-20 17:47:37 +01:00
def same_date? ( num , compare )
2018-01-11 19:08:04 +01:00
if type_champ == 'datetime' && value . present?
2016-06-22 11:25:41 +02:00
if value . to_datetime . strftime ( compare ) == num
return true
end
end
false
end
2016-10-12 17:22:56 +02:00
def self . regions
2018-01-16 13:23:29 +01:00
JSON . parse ( Carto :: GeoAPI :: Driver . regions ) . sort_by { | e | e [ 'nom' ] } . pluck ( " nom " )
2016-10-12 17:22:56 +02:00
end
def self . departements
2018-01-15 21:41:16 +01:00
JSON . parse ( Carto :: GeoAPI :: Driver . departements ) . map { | liste | " #{ liste [ 'code' ] } - #{ liste [ 'nom' ] } " } . push ( '99 - Étranger' )
2016-10-12 17:22:56 +02:00
end
def self . pays
2018-01-16 13:23:29 +01:00
JSON . parse ( Carto :: GeoAPI :: Driver . pays ) . pluck ( " nom " )
2016-12-26 11:47:51 +01:00
end
2017-10-16 17:39:34 +02:00
def to_s
if value . present?
2018-06-15 13:47:32 +02:00
string_value
2017-10-16 17:39:34 +02:00
else
''
end
end
2017-10-26 16:54:10 +02:00
def for_export
if value . present?
case type_champ
when 'textarea'
ActionView :: Base . full_sanitizer . sanitize ( value )
2017-10-26 17:45:35 +02:00
when 'yes_no'
2017-11-21 10:28:21 +01:00
value == 'true' ? 'oui' : 'non'
2017-10-27 10:01:53 +02:00
when 'multiple_drop_down_list'
drop_down_list . selected_options_without_decorator ( self ) . join ( ', ' )
2017-10-26 16:54:10 +02:00
else
value
end
else
nil
end
end
2018-01-30 19:17:16 +01:00
def piece_justificative_file_errors
errors = [ ]
if piece_justificative_file . attached? && piece_justificative_file . previous_changes . present?
if piece_justificative_file . blob . byte_size > PIECE_JUSTIFICATIVE_FILE_MAX_SIZE
errors << " Le fichier #{ piece_justificative_file . filename . to_s } est trop lourd, il doit faire au plus #{ PIECE_JUSTIFICATIVE_FILE_MAX_SIZE . to_s ( :human_size , precision : 2 ) } "
end
if ! piece_justificative_file . blob . content_type . in? ( PIECE_JUSTIFICATIVE_FILE_ACCEPTED_FORMATS )
errors << " Le fichier #{ piece_justificative_file . filename . to_s } est dans un format que nous n'acceptons pas "
end
# FIXME: add Clamav check
end
if errors . present?
piece_justificative_file . purge
end
errors
end
2018-06-15 13:47:32 +02:00
private
def string_value
value . to_s
end
2015-11-03 10:48:40 +01:00
end