diff --git a/app/models/champ.rb b/app/models/champ.rb index 91b8055c0..f396e988c 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -7,82 +7,21 @@ class Champ < ApplicationRecord delegate :libelle, :type_champ, :order_place, :mandatory?, :description, :drop_down_list, to: :type_de_champ - before_save :format_date_to_iso, if: Proc.new { type_champ == 'date' } - before_save :format_datetime, if: Proc.new { type_champ == 'datetime' } - before_save :multiple_select_to_string, if: Proc.new { type_champ == 'multiple_drop_down_list' } - scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) } scope :public_only, -> { where(private: false) } scope :private_only, -> { where(private: true) } - 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" - ] - def public? !private? end - def same_hour?(num) - same_date? num, '%H' - end - - def same_minute?(num) - same_date? num, '%M' - end - def mandatory_and_blank? - if type_champ == 'piece_justificative' - mandatory? && !piece_justificative_file.attached? - else - mandatory? && value.blank? - end - end - - def same_date?(num, compare) - if type_champ == 'datetime' && value.present? - if value.to_datetime.strftime(compare) == num - return true - end - end - false - end - - def self.regions - JSON.parse(Carto::GeoAPI::Driver.regions).sort_by { |e| e['nom'] }.pluck("nom") - end - - def self.departements - JSON.parse(Carto::GeoAPI::Driver.departements).map { |liste| "#{liste['code']} - #{liste['nom']}" }.push('99 - Étranger') - end - - def self.pays - JSON.parse(Carto::GeoAPI::Driver.pays).pluck("nom") + mandatory? && value.blank? end def to_s if value.present? - case type_champ - when 'date' - Date.parse(value).strftime('%d/%m/%Y') - when 'multiple_drop_down_list' - drop_down_list.selected_options_without_decorator(self).join(', ') - else - value.to_s - end + string_value else '' end @@ -90,80 +29,19 @@ class Champ < ApplicationRecord def for_export if value.present? - case type_champ - when 'textarea' - ActionView::Base.full_sanitizer.sanitize(value) - when 'yes_no' - value == 'true' ? 'oui' : 'non' - when 'multiple_drop_down_list' - drop_down_list.selected_options_without_decorator(self).join(', ') - else - value - end + value_for_export else nil end end - 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 - private - def format_date_to_iso - date = begin - Date.parse(value).iso8601 - rescue - nil - end - self.value = date + def string_value + value.to_s end - def format_datetime - if (value =~ /=>/).present? - date = begin - hash_date = YAML.safe_load(value.gsub('=>', ': ')) - year, month, day, hour, minute = hash_date.values_at(1,2,3,4,5) - DateTime.new(year, month, day, hour, minute).strftime("%d/%m/%Y %H:%M") - rescue - nil - end - self.value = date - elsif /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/.match?(value) # old browsers can send with dd/mm/yyyy hh:mm format - self.value = DateTime.parse(value, "%d/%m/%Y %H:%M").strftime("%Y-%m-%d %H:%M") - elsif !(/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/.match?(value)) # a datetime not correctly formatted should not be stored - self.value = nil - end - end - - def multiple_select_to_string - if value.present? - json = JSON.parse(value) - if json == [''] - self.value = nil - else - json = json - [''] - self.value = json.to_s - end - end + def value_for_export + value end end diff --git a/app/models/champs/date_champ.rb b/app/models/champs/date_champ.rb index 7b391c5e9..310f608a9 100644 --- a/app/models/champs/date_champ.rb +++ b/app/models/champs/date_champ.rb @@ -1,2 +1,18 @@ class Champs::DateChamp < Champ + before_save :format_before_save + + private + + def format_before_save + self.value = + begin + Date.parse(value).iso8601 + rescue + nil + end + end + + def string_value + Date.parse(value).strftime('%d/%m/%Y') + end end diff --git a/app/models/champs/datetime_champ.rb b/app/models/champs/datetime_champ.rb index 8b5c9c73f..1388ccc1f 100644 --- a/app/models/champs/datetime_champ.rb +++ b/app/models/champs/datetime_champ.rb @@ -1,2 +1,34 @@ class Champs::DatetimeChamp < Champ + before_save :format_before_save + + def same_hour?(num) + same_date?(num, '%H') + end + + def same_minute?(num) + same_date?(num, '%M') + end + + private + + def same_date?(num, compare) + return value.present? && value.to_datetime.strftime(compare) == num + end + + def format_before_save + if (value =~ /=>/).present? + self.value = + begin + hash_date = YAML.safe_load(value.gsub('=>', ': ')) + year, month, day, hour, minute = hash_date.values_at(1,2,3,4,5) + DateTime.new(year, month, day, hour, minute).strftime("%d/%m/%Y %H:%M") + rescue + nil + end + elsif /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/.match?(value) # old browsers can send with dd/mm/yyyy hh:mm format + self.value = DateTime.parse(value, "%d/%m/%Y %H:%M").strftime("%Y-%m-%d %H:%M") + elsif !(/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/.match?(value)) # a datetime not correctly formatted should not be stored + self.value = nil + end + end end diff --git a/app/models/champs/departement_champ.rb b/app/models/champs/departement_champ.rb index ee9803389..0cb4a8e68 100644 --- a/app/models/champs/departement_champ.rb +++ b/app/models/champs/departement_champ.rb @@ -1,2 +1,5 @@ class Champs::DepartementChamp < Champs::TextChamp + def self.departements + JSON.parse(Carto::GeoAPI::Driver.departements).map { |liste| "#{liste['code']} - #{liste['nom']}" }.push('99 - Étranger') + end end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 5182673b5..b0ca0e121 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -1,2 +1,25 @@ class Champs::MultipleDropDownListChamp < Champ + before_save :format_before_save + + private + + def format_before_save + if value.present? + json = JSON.parse(value) + if json == [''] + self.value = nil + else + json = json - [''] + self.value = json.to_s + end + end + end + + def string_value + drop_down_list.selected_options_without_decorator(self).join(', ') + end + + def value_for_export + drop_down_list.selected_options_without_decorator(self).join(', ') + end end diff --git a/app/models/champs/pays_champ.rb b/app/models/champs/pays_champ.rb index 95b89893f..f8204f4d0 100644 --- a/app/models/champs/pays_champ.rb +++ b/app/models/champs/pays_champ.rb @@ -1,2 +1,5 @@ class Champs::PaysChamp < Champs::TextChamp + def self.pays + JSON.parse(Carto::GeoAPI::Driver.pays).pluck("nom") + end end diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 4c4270609..2213a5579 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -1,6 +1,51 @@ class Champs::PieceJustificativeChamp < Champ after_commit :create_virus_scan + 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" + ] + + def mandatory_and_blank? + mandatory? && !piece_justificative_file.attached? + end + + 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 + + private + def create_virus_scan if self.piece_justificative_file&.attachment&.blob.present? VirusScan.where(champ: self).where.not(blob_key: self.piece_justificative_file.blob.key).delete_all diff --git a/app/models/champs/region_champ.rb b/app/models/champs/region_champ.rb index ba9045087..8c5320c29 100644 --- a/app/models/champs/region_champ.rb +++ b/app/models/champs/region_champ.rb @@ -1,2 +1,5 @@ class Champs::RegionChamp < Champs::TextChamp + def self.regions + JSON.parse(Carto::GeoAPI::Driver.regions).sort_by { |e| e['nom'] }.pluck("nom") + end end diff --git a/app/models/champs/textarea_champ.rb b/app/models/champs/textarea_champ.rb index eedaac8b1..58e8d2db2 100644 --- a/app/models/champs/textarea_champ.rb +++ b/app/models/champs/textarea_champ.rb @@ -1,2 +1,7 @@ class Champs::TextareaChamp < Champs::TextChamp + private + + def value_for_export + ActionView::Base.full_sanitizer.sanitize(value) + end end diff --git a/app/models/champs/yes_no_champ.rb b/app/models/champs/yes_no_champ.rb index 6653a50d4..a73777fa8 100644 --- a/app/models/champs/yes_no_champ.rb +++ b/app/models/champs/yes_no_champ.rb @@ -1,2 +1,7 @@ class Champs::YesNoChamp < Champs::CheckboxChamp + private + + def value_for_export + value == 'true' ? 'oui' : 'non' + end end diff --git a/app/views/shared/dossiers/editable_champs/_departements.html.haml b/app/views/shared/dossiers/editable_champs/_departements.html.haml index 90c2ca408..9f7cc8a8b 100644 --- a/app/views/shared/dossiers/editable_champs/_departements.html.haml +++ b/app/views/shared/dossiers/editable_champs/_departements.html.haml @@ -1,3 +1,3 @@ = form.select :value, - Champ.departements, + Champs::DepartementChamp.departements, required: champ.mandatory? diff --git a/app/views/shared/dossiers/editable_champs/_pays.html.haml b/app/views/shared/dossiers/editable_champs/_pays.html.haml index cdc2e489d..aba08f38a 100644 --- a/app/views/shared/dossiers/editable_champs/_pays.html.haml +++ b/app/views/shared/dossiers/editable_champs/_pays.html.haml @@ -1,3 +1,3 @@ = form.select :value, - Champ.pays, + Champs::PaysChamp.pays, required: champ.mandatory? diff --git a/app/views/shared/dossiers/editable_champs/_regions.html.haml b/app/views/shared/dossiers/editable_champs/_regions.html.haml index b947a8d31..460a4b1af 100644 --- a/app/views/shared/dossiers/editable_champs/_regions.html.haml +++ b/app/views/shared/dossiers/editable_champs/_regions.html.haml @@ -1,3 +1,3 @@ = form.select :value, - Champ.regions, + Champs::RegionChamp.regions, required: champ.mandatory? diff --git a/app/views/users/description/champs/_departements.html.haml b/app/views/users/description/champs/_departements.html.haml index 5fd949ccd..6fb027390 100644 --- a/app/views/users/description/champs/_departements.html.haml +++ b/app/views/users/description/champs/_departements.html.haml @@ -1,2 +1,2 @@ = select_tag("champs['#{champ.id}']", - options_for_select(Champ.departements, selected: champ.object.value)) + options_for_select(Champs::DepartementChamp.departements, selected: champ.object.value)) diff --git a/app/views/users/description/champs/_pays.html.haml b/app/views/users/description/champs/_pays.html.haml index 2f530b683..67981205c 100644 --- a/app/views/users/description/champs/_pays.html.haml +++ b/app/views/users/description/champs/_pays.html.haml @@ -1,2 +1,2 @@ = select_tag("champs['#{champ.id}']", - options_for_select(Champ.pays, selected: champ.object.value)) + options_for_select(Champs::PaysChamp.pays, selected: champ.object.value)) diff --git a/app/views/users/description/champs/_regions.html.haml b/app/views/users/description/champs/_regions.html.haml index 613b6af27..24c6f4bc5 100644 --- a/app/views/users/description/champs/_regions.html.haml +++ b/app/views/users/description/champs/_regions.html.haml @@ -1,2 +1,2 @@ = select_tag("champs['#{champ.id}']", - options_for_select(Champ.regions, selected: champ.object.value)) + options_for_select(Champs::RegionChamp.regions, selected: champ.object.value)) diff --git a/spec/features/new_user/dossier_spec.rb b/spec/features/new_user/dossier_spec.rb index 763b9555e..b9145ab37 100644 --- a/spec/features/new_user/dossier_spec.rb +++ b/spec/features/new_user/dossier_spec.rb @@ -12,8 +12,8 @@ feature 'The user' do # there are no extraneous input # attached file works scenario 'fill a dossier', js: true do - allow(Champ).to receive(:regions).and_return(['region1', 'region2']).at_least(:once) - allow(Champ).to receive(:departements).and_return(['dep1', 'dep2']).at_least(:once) + allow(Champs::RegionChamp).to receive(:regions).and_return(['region1', 'region2']).at_least(:once) + allow(Champs::DepartementChamp).to receive(:departements).and_return(['dep1', 'dep2']).at_least(:once) log_in(user.email, password, procedure) diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index a424fc824..24d150196 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -27,7 +27,7 @@ shared_examples 'champ_spec' do end describe '.departement', vcr: { cassette_name: 'call_geo_api_departements' } do - subject { Champ.departements } + subject { Champs::DepartementChamp.departements } it { expect(subject).to include '99 - Étranger' } end