refactor(export): expose new interface on type de champ and use it in champs
This commit is contained in:
parent
371b8b0b46
commit
39364961ab
4 changed files with 108 additions and 17 deletions
|
@ -109,24 +109,29 @@ class Champ < ApplicationRecord
|
||||||
[to_s]
|
[to_s]
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def valid_value
|
||||||
value.present? ? value.to_s : ''
|
return unless valid_champ_value?
|
||||||
end
|
|
||||||
|
|
||||||
def for_export(path = :value)
|
|
||||||
path == :value ? value.presence : nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def for_api
|
|
||||||
value
|
value
|
||||||
end
|
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
|
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
|
end
|
||||||
|
|
||||||
def for_tag(path = :value)
|
def for_tag(path = :value)
|
||||||
path == :value && value.present? ? value.to_s : ''
|
TypeDeChamp.champ_value_for_tag(type_champ, self, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def main_value_name
|
def main_value_name
|
||||||
|
|
|
@ -25,7 +25,7 @@ module DossierChampsConcern
|
||||||
types_de_champ.flat_map do |type_de_champ|
|
types_de_champ.flat_map do |type_de_champ|
|
||||||
champ = champ_for_export(type_de_champ, row_id)
|
champ = champ_for_export(type_de_champ, row_id)
|
||||||
type_de_champ.libelles_for_export.map do |(libelle, path)|
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -250,7 +250,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
def params_for_champ
|
def params_for_champ
|
||||||
{
|
{
|
||||||
private: private?,
|
private: private?,
|
||||||
type: "Champs::#{type_champ.classify}Champ",
|
type: self.class.type_champ_to_champ_class_name(type_champ),
|
||||||
stable_id:,
|
stable_id:,
|
||||||
stream: 'main'
|
stream: 'main'
|
||||||
}
|
}
|
||||||
|
@ -463,10 +463,6 @@ class TypeDeChamp < ApplicationRecord
|
||||||
!private?
|
!private?
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.type_champ_to_class_name(type_champ)
|
|
||||||
"TypesDeChamp::#{type_champ.classify}TypeDeChamp"
|
|
||||||
end
|
|
||||||
|
|
||||||
def filename_for_attachement(attachment_sym)
|
def filename_for_attachement(attachment_sym)
|
||||||
attachment = send(attachment_sym)
|
attachment = send(attachment_sym)
|
||||||
if attachment.attached?
|
if attachment.attached?
|
||||||
|
@ -687,6 +683,56 @@ class TypeDeChamp < ApplicationRecord
|
||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
DEFAULT_EMPTY = ['']
|
DEFAULT_EMPTY = ['']
|
||||||
|
|
|
@ -56,6 +56,46 @@ class TypesDeChamp::TypeDeChampBase
|
||||||
human_value
|
human_value
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def paths
|
def paths
|
||||||
|
|
Loading…
Reference in a new issue