2023-01-05 12:18:27 +01:00
|
|
|
class Champs::BooleanChamp < Champ
|
|
|
|
TRUE_VALUE = 'true'
|
|
|
|
FALSE_VALUE = 'false'
|
|
|
|
|
|
|
|
before_validation :set_value_to_nil, if: -> { value.blank? }
|
|
|
|
before_validation :set_value_to_false, unless: -> { ([nil, TRUE_VALUE, FALSE_VALUE]).include?(value) }
|
|
|
|
|
2024-04-01 07:36:14 +02:00
|
|
|
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
|
2024-04-01 07:16:56 +02:00
|
|
|
validates :value, inclusion: [TRUE_VALUE, FALSE_VALUE],
|
|
|
|
allow_nil: true,
|
|
|
|
allow_blank: false,
|
|
|
|
if: :validate_champ_value?
|
2023-01-06 14:01:59 +01:00
|
|
|
|
2023-01-05 12:18:27 +01:00
|
|
|
def true?
|
|
|
|
value == TRUE_VALUE
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_terms
|
|
|
|
if true?
|
|
|
|
[libelle]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
processed_value
|
|
|
|
end
|
|
|
|
|
2024-04-02 17:05:44 +02:00
|
|
|
def for_tag(path = :value)
|
2023-01-05 12:18:27 +01:00
|
|
|
processed_value
|
|
|
|
end
|
|
|
|
|
2024-04-02 17:05:44 +02:00
|
|
|
def for_export(path = :value)
|
2023-01-05 12:18:27 +01:00
|
|
|
processed_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def for_api_v2
|
|
|
|
true? ? 'true' : 'false'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def processed_value
|
|
|
|
true? ? 'Oui' : 'Non'
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_value_to_nil
|
|
|
|
self.value = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_value_to_false
|
|
|
|
self.value = FALSE_VALUE
|
|
|
|
end
|
|
|
|
end
|