demarches-normaliennes/app/models/logic/champ_value.rb

113 lines
2.9 KiB
Ruby
Raw Normal View History

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,
:drop_down_list,
:multiple_drop_down_list
2022-06-21 14:39:13 +02:00
)
2022-06-22 16:46:00 +02:00
CHAMP_VALUE_TYPE = {
2022-09-20 15:37:00 +02:00
boolean: :boolean, # from yes_no or checkbox champ
number: :number, # from integer or decimal number champ
enum: :enum, # a choice from a dropdownlist
enums: :enums, # multiple choice from a dropdownlist (multipledropdownlist)
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
def initialize(stable_id)
2022-06-16 17:21:47 +02:00
@stable_id = stable_id
end
2023-01-04 11:10:10 +01:00
def sources
[@stable_id]
end
2022-06-16 17:21:47 +02:00
def compute(champs)
targeted_champ = champ(champs)
return nil if !targeted_champ.visible?
return nil if targeted_champ.blank? & !targeted_champ.drop_down_other?
# on dépense 22ms ici, à cause du map, mais on doit pouvoir passer par un champ type
case targeted_champ.type
when "Champs::YesNoChamp",
"Champs::CheckboxChamp"
targeted_champ.true?
when "Champs::IntegerNumberChamp", "Champs::DecimalNumberChamp"
targeted_champ.for_api
when "Champs::DropDownListChamp"
targeted_champ.selected
when "Champs::MultipleDropDownListChamp"
targeted_champ.selected_options
when "Champs::DepartementChamp"
targeted_champ.code
2022-06-16 17:21:47 +02:00
end
end
2022-09-26 21:21:55 +02:00
def to_s(type_de_champs) = type_de_champ(type_de_champs)&.libelle # TODO: gerer le cas ou un tdc est supprimé
2022-06-16 17:21:47 +02:00
2022-09-26 21:07:43 +02:00
def type(type_de_champs)
case type_de_champ(type_de_champs)&.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)
when MANAGED_TYPE_DE_CHAMP.fetch(:multiple_drop_down_list)
CHAMP_VALUE_TYPE.fetch(:enums)
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
2022-09-26 21:11:43 +02:00
def errors(type_de_champs)
if !type_de_champs.map(&:stable_id).include?(stable_id)
[{ type: :not_available }]
2022-06-16 17:21:47 +02:00
else
[]
end
end
def to_h
{
2022-07-05 14:47:32 +02:00
"term" => self.class.name,
"stable_id" => @stable_id
2022-06-16 17:21:47 +02:00
}
end
def self.from_h(h)
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-09-26 21:07:43 +02:00
def options(type_de_champs)
tdc = type_de_champ(type_de_champs)
opts = tdc.drop_down_list_enabled_non_empty_options.map { |option| [option, option] }
if tdc.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
2022-09-26 21:07:43 +02:00
def type_de_champ(type_de_champs)
type_de_champs.find { |c| c.stable_id == stable_id }
2022-06-16 17:21:47 +02:00
end
def champ(champs)
champs.find { |c| c.stable_id == stable_id }
end
end