demarches-normaliennes/app/models/prefill_description.rb
Sébastien Carceles 0a10a08c21
feat(demarche): describe procedure prefilling (#8187)
* feat(demarche): description

Show the description of an opendata procedure (published or draft),
with help about how to prefill a dossier for this procedure.

Co-authored-by: Damien Le Thiec <damien.lethiec@gmail.com>
2022-12-19 11:32:09 +00:00

46 lines
1.2 KiB
Ruby

class PrefillDescription < SimpleDelegator
include Rails.application.routes.url_helpers
MAX_PREFILL_LINK_LENGTH = 2000
attr_reader :selected_type_de_champ_ids
def initialize(procedure)
super(procedure)
@selected_type_de_champ_ids = []
end
def update(attributes)
@selected_type_de_champ_ids = attributes[:selected_type_de_champ_ids].presence || []
end
def types_de_champ
active_revision.types_de_champ_public
end
def include?(type_de_champ_id)
selected_type_de_champ_ids.include?(type_de_champ_id.to_s)
end
def link_too_long?
prefill_link.length > MAX_PREFILL_LINK_LENGTH
end
def prefill_link
@prefill_link ||= commencer_url({ path: path }.merge(prefilled_champs_for_link))
end
def prefilled_champs
@prefilled_champs ||= types_de_champ.where(id: selected_type_de_champ_ids)
end
private
def prefilled_champs_for_link
prefilled_champs.map { |type_de_champ| ["champ_#{type_de_champ.to_typed_id}", type_de_champ.libelle] }.to_h
end
def prefilled_champs_for_query
prefilled_champs.map { |type_de_champ| "\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.libelle}\"" } .join(', ')
end
end