Skip to content

Commit

Permalink
wip try to fix test with a before_save hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lisa-durand authored and E-L-T committed Dec 20, 2024
1 parent 384006d commit baed393
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions app/models/champs/drop_down_list_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Champs::DropDownListChamp < Champ
OTHER = '__other__'
delegate :options_without_empty_value_when_mandatory, to: :type_de_champ
validate :value_is_in_options, if: -> { validate_champ_value? && !(value.blank? || drop_down_other?) }
before_save :store_referentiel

def render_as_radios?
options = referentiel? ? referentiel_drop_down_options : drop_down_options
Expand Down Expand Up @@ -37,18 +38,27 @@ def other?
def value=(value)
if value == OTHER
self.other = true
self.referentiel = nil if self.referentiel?
write_attribute(:value, nil)
else
self.other = false
self.referentiel = set_referentiel_from(value) if self.referentiel? && value
write_attribute(:value, value)
end
end

def store_referentiel
return if !self.referentiel?
if self.other?
self.referentiel = nil
else
self.referentiel = set_referentiel_from(value) if value
end
end

def set_referentiel_from(value)
referentiel_item = ReferentielItem.find(value)
{ data: referentiel_item.data }
if value.present?
referentiel_item = ReferentielItem.find(value)
{ data: referentiel_item.data }
end
end

def value_other=(value)
Expand Down

0 comments on commit baed393

Please sign in to comment.