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

77 lines
1.5 KiB
Ruby
Raw Normal View History

2022-06-16 17:21:47 +02:00
class Logic::ChampValue < Logic::Term
attr_reader :stable_id
def initialize(stable_id)
@stable_id = stable_id
end
def compute(champs)
case type_de_champ.type_champ
when all_types.fetch(:yes_no),
all_types.fetch(:checkbox)
champ(champs).true?
when all_types.fetch(:integer_number), all_types.fetch(:decimal_number)
champ(champs).for_api
2022-06-09 14:20:06 +02:00
when all_types.fetch(:drop_down_list), all_types.fetch(:text)
2022-06-16 17:21:47 +02:00
champ(champs).value
end
end
def to_s = "#{type_de_champ.libelle}#{stable_id}"
def type
case type_de_champ.type_champ
when all_types.fetch(:yes_no),
all_types.fetch(:checkbox)
:boolean
when all_types.fetch(:integer_number), all_types.fetch(:decimal_number)
:number
when all_types.fetch(:text)
:string
2022-06-09 14:20:06 +02:00
when all_types.fetch(:drop_down_list)
:enum
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
{
"op" => self.class.name,
"stable_id" => @stable_id
}
end
def self.from_h(h)
self.new(h['stable_id'])
end
def ==(other)
self.class == other.class && @stable_id == other.stable_id
end
2022-06-09 14:20:06 +02:00
def options
type_de_champ.drop_down_list_enabled_non_empty_options
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
def all_types
TypeDeChamp.type_champs
end
end