From ded65cfdb91c5ab2f419964ca59c104905b99b42 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 2 Apr 2020 17:33:54 +0200 Subject: [PATCH] dossier: fix rendering of piece_justificative in repetition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/helpers/application_helper.rb | 9 +++++++++ app/helpers/champ_helper.rb | 2 +- app/views/champs/piece_justificative/show.js.erb | 13 +------------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e45d95781..acdabec8a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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}');" diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index 3eb709454..1e51c662d 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -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 diff --git a/app/views/champs/piece_justificative/show.js.erb b/app/views/champs/piece_justificative/show.js.erb index e921f9556..80b454204 100644 --- a/app/views/champs/piece_justificative/show.js.erb +++ b/app/views/champs/piece_justificative/show.js.erb @@ -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? %>