diff --git a/app/models/champ.rb b/app/models/champ.rb index 7fa02202b..7c98eebcf 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -109,24 +109,29 @@ class Champ < ApplicationRecord [to_s] end - def to_s - value.present? ? value.to_s : '' - end - - def for_export(path = :value) - path == :value ? value.presence : nil - end - - def for_api + def valid_value + return unless valid_champ_value? value end + def to_s + TypeDeChamp.champ_value(type_champ, self) + end + + def for_api + TypeDeChamp.champ_value_for_api(type_champ, self, 1) + end + def for_api_v2 - to_s + TypeDeChamp.champ_value_for_api(type_champ, self, 2) + end + + def for_export(path = :value) + TypeDeChamp.champ_value_for_export(type_champ, self, path) end def for_tag(path = :value) - path == :value && value.present? ? value.to_s : '' + TypeDeChamp.champ_value_for_tag(type_champ, self, path) end def main_value_name diff --git a/app/models/concerns/dossier_champs_concern.rb b/app/models/concerns/dossier_champs_concern.rb index ab5a7545e..1e5f2bc3e 100644 --- a/app/models/concerns/dossier_champs_concern.rb +++ b/app/models/concerns/dossier_champs_concern.rb @@ -25,7 +25,7 @@ module DossierChampsConcern types_de_champ.flat_map do |type_de_champ| champ = champ_for_export(type_de_champ, row_id) type_de_champ.libelles_for_export.map do |(libelle, path)| - [libelle, champ&.for_export(path)] + [libelle, TypeDeChamp.champ_value_for_export(type_de_champ.type_champ, champ, path)] end end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index b4ff3ac51..78f5f6bac 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -250,7 +250,7 @@ class TypeDeChamp < ApplicationRecord def params_for_champ { private: private?, - type: "Champs::#{type_champ.classify}Champ", + type: self.class.type_champ_to_champ_class_name(type_champ), stable_id:, stream: 'main' } @@ -463,10 +463,6 @@ class TypeDeChamp < ApplicationRecord !private? end - def self.type_champ_to_class_name(type_champ) - "TypesDeChamp::#{type_champ.classify}TypeDeChamp" - end - def filename_for_attachement(attachment_sym) attachment = send(attachment_sym) if attachment.attached? @@ -687,6 +683,56 @@ class TypeDeChamp < ApplicationRecord end end + class << self + def champ_value(type_champ, champ) + dynamic_type_class = type_champ_to_class_name(type_champ).constantize + # special case for linked drop down champ – it's blank implementation is not what you think + if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) + dynamic_type_class.champ_default_value + else + dynamic_type_class.champ_value(champ) + end + end + + def champ_value_for_api(type_champ, champ, version = 2) + dynamic_type_class = type_champ_to_class_name(type_champ).constantize + # special case for linked drop down champ – it's blank implementation is not what you think + if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) + dynamic_type_class.champ_default_api_value(version) + else + dynamic_type_class.champ_value_for_api(champ, version) + end + end + + def champ_value_for_export(type_champ, champ, path = :value) + dynamic_type_class = type_champ_to_class_name(type_champ).constantize + # special case for linked drop down champ – it's blank implementation is not what you think + if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) + dynamic_type_class.champ_default_export_value(path) + else + dynamic_type_class.champ_value_for_export(champ, path) + end + end + + def champ_value_for_tag(type_champ, champ, path = :value) + dynamic_type_class = type_champ_to_class_name(type_champ).constantize + # special case for linked drop down champ – it's blank implementation is not what you think + if champ.blank? && (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) + '' + else + dynamic_type_class.champ_value_for_tag(champ, path) + end + end + + def type_champ_to_champ_class_name(type_champ) + "Champs::#{type_champ.classify}Champ" + end + + def type_champ_to_class_name(type_champ) + "TypesDeChamp::#{type_champ.classify}TypeDeChamp" + end + end + private DEFAULT_EMPTY = [''] diff --git a/app/models/types_de_champ/type_de_champ_base.rb b/app/models/types_de_champ/type_de_champ_base.rb index db0d38d9b..81217caa2 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -56,6 +56,46 @@ class TypesDeChamp::TypeDeChampBase human_value end + class << self + def champ_value(champ) + champ.value.present? ? champ.value.to_s : champ_default_value + end + + def champ_value_for_api(champ, version = 2) + case version + when 2 + champ_value(champ) + else + champ.valid_value.presence || champ_default_api_value(version) + end + end + + def champ_value_for_export(champ, path = :value) + path == :value ? champ.valid_value.presence : champ_default_export_value(path) + end + + def champ_value_for_tag(champ, path = :value) + path == :value ? champ_value(champ) : nil + end + + def champ_default_value + '' + end + + def champ_default_export_value(path = :value) + nil + end + + def champ_default_api_value(version = 2) + case version + when 2 + '' + else + nil + end + end + end + private def paths