demarches-normaliennes/app/services/champs_service.rb
Simon Lehericey 2b969ef575 MandatoryAttachment: group missing errors logic together
and show errors only if not draft
2017-04-19 15:45:45 +02:00

35 lines
905 B
Ruby

class ChampsService
class << self
def save_champs(champs, params)
fill_champs(champs, params)
champs.select(&:changed?).each(&:save)
end
def build_error_messages(champs)
champs.select(&:mandatory_and_blank?)
.map { |c| "Le champ #{c.libelle} doit être rempli." }
end
private
def fill_champs(champs, h)
datetimes, not_datetimes = champs.partition { |c| c.type_champ == 'datetime' }
not_datetimes.each { |c| c.value = h[:champs]["'#{c.id}'"] }
datetimes.each { |c| c.value = parse_datetime(c.id, h) }
end
def parse_datetime(champ_id, h)
"#{h[:champs]["'#{champ_id}'"]} #{extract_hour(champ_id, h)}:#{extract_minute(champ_id, h)}"
end
def extract_hour(champ_id, h)
h[:time_hour]["'#{champ_id}'"]
end
def extract_minute(champ_id, h)
h[:time_minute]["'#{champ_id}'"]
end
end
end