2022-06-16 17:21:47 +02:00
|
|
|
class Logic::ChampValue < Logic::Term
|
2022-06-21 14:39:13 +02:00
|
|
|
MANAGED_TYPE_DE_CHAMP = TypeDeChamp.type_champs.slice(
|
|
|
|
:yes_no,
|
|
|
|
:checkbox,
|
|
|
|
:integer_number,
|
|
|
|
:decimal_number,
|
2022-07-05 11:33:56 +02:00
|
|
|
:drop_down_list
|
2022-06-21 14:39:13 +02:00
|
|
|
)
|
|
|
|
|
2022-06-22 16:46:00 +02:00
|
|
|
CHAMP_VALUE_TYPE = {
|
|
|
|
boolean: :boolean,
|
|
|
|
number: :number,
|
|
|
|
enum: :enum,
|
2022-06-28 12:49:25 +02:00
|
|
|
empty: :empty,
|
|
|
|
unmanaged: :unmanaged
|
2022-06-22 16:46:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
attr_reader :stable_id
|
|
|
|
|
2022-07-06 09:44:54 +02:00
|
|
|
def initialize(stable_id)
|
2022-06-16 17:21:47 +02:00
|
|
|
@stable_id = stable_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def compute(champs)
|
2022-07-19 17:40:50 +02:00
|
|
|
targeted_champ = champ(champs)
|
|
|
|
|
|
|
|
return nil if !targeted_champ.visible?
|
2022-07-21 15:48:37 +02:00
|
|
|
return nil if targeted_champ.blank?
|
2022-07-19 17:40:50 +02:00
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
case type_de_champ.type_champ
|
2022-06-21 14:39:13 +02:00
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:yes_no),
|
|
|
|
MANAGED_TYPE_DE_CHAMP.fetch(:checkbox)
|
2022-07-19 17:40:50 +02:00
|
|
|
targeted_champ.true?
|
2022-06-21 14:39:13 +02:00
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:integer_number), MANAGED_TYPE_DE_CHAMP.fetch(:decimal_number)
|
2022-07-19 17:40:50 +02:00
|
|
|
targeted_champ.for_api
|
2022-07-05 11:33:56 +02:00
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list)
|
2022-08-02 12:57:41 +02:00
|
|
|
targeted_champ.selected
|
2022-06-16 17:21:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-13 12:35:11 +02:00
|
|
|
def to_s = type_de_champ&.libelle # TODO: gerer le cas ou un tdc est supprimé
|
2022-06-16 17:21:47 +02:00
|
|
|
|
|
|
|
def type
|
2022-07-11 21:31:06 +02:00
|
|
|
case type_de_champ&.type_champ # TODO: gerer le cas ou un tdc est supprimé
|
2022-06-21 14:39:13 +02:00
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:yes_no),
|
|
|
|
MANAGED_TYPE_DE_CHAMP.fetch(:checkbox)
|
2022-06-22 16:46:00 +02:00
|
|
|
CHAMP_VALUE_TYPE.fetch(:boolean)
|
2022-06-21 14:39:13 +02:00
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:integer_number), MANAGED_TYPE_DE_CHAMP.fetch(:decimal_number)
|
2022-06-22 16:46:00 +02:00
|
|
|
CHAMP_VALUE_TYPE.fetch(:number)
|
2022-06-21 14:39:13 +02:00
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list)
|
2022-06-22 16:46:00 +02:00
|
|
|
CHAMP_VALUE_TYPE.fetch(:enum)
|
2022-06-21 14:39:13 +02:00
|
|
|
else
|
2022-06-28 12:49:25 +02:00
|
|
|
CHAMP_VALUE_TYPE.fetch(:unmanaged)
|
2022-06-16 17:21:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors(stable_ids)
|
|
|
|
if !stable_ids.include?(stable_id)
|
|
|
|
["le type de champ stable_id=#{stable_id} n'est pas disponible"]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
{
|
2022-07-05 14:47:32 +02:00
|
|
|
"term" => self.class.name,
|
2022-07-06 09:44:54 +02:00
|
|
|
"stable_id" => @stable_id
|
2022-06-16 17:21:47 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_h(h)
|
2022-07-06 09:44:54 +02:00
|
|
|
self.new(h['stable_id'])
|
2022-06-16 17:21:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
self.class == other.class && @stable_id == other.stable_id
|
|
|
|
end
|
|
|
|
|
2022-06-09 14:20:06 +02:00
|
|
|
def options
|
2022-08-02 12:57:41 +02:00
|
|
|
opts = type_de_champ.drop_down_list_enabled_non_empty_options.map { |option| [option, option] }
|
|
|
|
if type_de_champ.drop_down_other?
|
|
|
|
opts + [["Autre", Champs::DropDownListChamp::OTHER]]
|
|
|
|
else
|
|
|
|
opts
|
|
|
|
end
|
2022-06-09 14:20:06 +02:00
|
|
|
end
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def type_de_champ
|
|
|
|
TypeDeChamp.find_by(stable_id: stable_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def champ(champs)
|
|
|
|
champs.find { |c| c.stable_id == stable_id }
|
|
|
|
end
|
|
|
|
end
|