dossier: fix rendering of piece_justificative in repetition

In repetitions we can't render the piece_justificative champ in the way
we do for normal champs (which was already a bit hackish).

Instead we render the whole form partial – and then extract the champ
from the form. In this way, we're sure to render the champ as it
appears in the form, without any hacks to reproduce the form structure.
This commit is contained in:
Pierre de La Morinerie 2020-04-02 17:33:54 +02:00
parent c506402dcb
commit ded65cfdb9
3 changed files with 11 additions and 13 deletions

View file

@ -49,6 +49,15 @@ module ApplicationHelper
end
end
def render_champ(champ)
champ_selector = ".editable-champ[data-champ-id=\"#{champ.id}\"]"
form_html = render 'shared/dossiers/edit', dossier: champ.dossier, apercu: false
champ_html = Nokogiri::HTML.fragment(form_html).at_css(champ_selector).to_s
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{champ_selector}').outerHTML = \"#{escape_javascript(champ_html)}\";")
# rubocop:enable Rails/OutputSafety
end
def remove_element(selector, timeout: 0, inner: false)
script = "(function() {";
script << "var el = document.querySelector('#{selector}');"

View file

@ -33,7 +33,7 @@ module ChampHelper
end
def auto_attach_url(form, object)
if feature_enabled?(:autoupload_dossier_attachments) && object.is_a?(Champ) && object.public?
if feature_enabled?(:autoupload_dossier_attachments) && object.is_a?(Champ) && object.persisted? && object.public?
champs_piece_justificative_url(object.id)
end
end

View file

@ -1,15 +1,4 @@
<% dossier = @champ.dossier %>
<%= fields_for dossier do |form| %>
<%= form.fields_for :champs, dossier.champs.where(id: @champ.id), include_id: false do |champ_form| %>
<% render_to_element(".editable-champ[data-champ-id=\"#{@champ.id}\"]",
partial: 'shared/dossiers/editable_champs/editable_champ',
locals: {
champ: @champ,
form: champ_form
}) %>
<% end %>
<% end %>
<%= render_champ(@champ) %>
<% attachment = @champ.piece_justificative_file.attachment %>
<% if attachment.virus_scanner.pending? %>