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>
This commit is contained in:
Sébastien Carceles 2022-12-19 12:32:09 +01:00 committed by GitHub
parent 936a1bfd91
commit 0a10a08c21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 497 additions and 26 deletions

View file

@ -66,6 +66,7 @@ class Champ < ApplicationRecord
:carte?,
:stable_id,
:mandatory?,
:prefillable?,
to: :type_de_champ
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }

View file

@ -0,0 +1,46 @@
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

View file

@ -31,16 +31,6 @@ class PrefillParams
end
class PrefillValue
AUTHORIZED_TYPES_DE_CHAMPS = [
TypeDeChamp.type_champs.fetch(:text),
TypeDeChamp.type_champs.fetch(:textarea),
TypeDeChamp.type_champs.fetch(:decimal_number),
TypeDeChamp.type_champs.fetch(:integer_number),
TypeDeChamp.type_champs.fetch(:email),
TypeDeChamp.type_champs.fetch(:phone),
TypeDeChamp.type_champs.fetch(:iban)
]
NEED_VALIDATION_TYPES_DE_CHAMPS = [
TypeDeChamp.type_champs.fetch(:decimal_number),
TypeDeChamp.type_champs.fetch(:integer_number)
@ -54,7 +44,7 @@ class PrefillParams
end
def prefillable?
authorized? && valid?
champ.prefillable? && valid?
end
def to_h
@ -66,10 +56,6 @@ class PrefillParams
private
def authorized?
AUTHORIZED_TYPES_DE_CHAMPS.include?(champ.type_champ)
end
def valid?
return true unless NEED_VALIDATION_TYPES_DE_CHAMPS.include?(champ.type_champ)

View file

@ -204,15 +204,16 @@ class Procedure < ApplicationRecord
has_one_attached :notice
has_one_attached :deliberation
scope :brouillons, -> { where(aasm_state: :brouillon) }
scope :publiees, -> { where(aasm_state: :publiee) }
scope :closes, -> { where(aasm_state: [:close, :depubliee]) }
scope :opendata, -> { where(opendata: true) }
scope :publiees_ou_closes, -> { where(aasm_state: [:publiee, :close, :depubliee]) }
scope :by_libelle, -> { order(libelle: :asc) }
scope :created_during, -> (range) { where(created_at: range) }
scope :cloned_from_library, -> { where(cloned_from_library: true) }
scope :declarative, -> { where.not(declarative_with_state: nil) }
scope :brouillons, -> { where(aasm_state: :brouillon) }
scope :publiees, -> { where(aasm_state: :publiee) }
scope :publiees_ou_brouillons, -> { publiees.or(brouillons) }
scope :closes, -> { where(aasm_state: [:close, :depubliee]) }
scope :opendata, -> { where(opendata: true) }
scope :publiees_ou_closes, -> { where(aasm_state: [:publiee, :close, :depubliee]) }
scope :by_libelle, -> { order(libelle: :asc) }
scope :created_during, -> (range) { where(created_at: range) }
scope :cloned_from_library, -> { where(cloned_from_library: true) }
scope :declarative, -> { where.not(declarative_with_state: nil) }
scope :discarded_expired, -> do
with_discarded

View file

@ -251,6 +251,18 @@ class TypeDeChamp < ApplicationRecord
collapsible_explanation_enabled == "1"
end
def prefillable?
type_champ.in?([
TypeDeChamp.type_champs.fetch(:text),
TypeDeChamp.type_champs.fetch(:textarea),
TypeDeChamp.type_champs.fetch(:decimal_number),
TypeDeChamp.type_champs.fetch(:integer_number),
TypeDeChamp.type_champs.fetch(:email),
TypeDeChamp.type_champs.fetch(:phone),
TypeDeChamp.type_champs.fetch(:iban)
])
end
def fillable?
!non_fillable?
end