Merge pull request #3392 from tchak/champ-repetition-fixes

Champ repetition fixes
This commit is contained in:
Paul Chavard 2019-02-07 12:42:15 +01:00 committed by GitHub
commit 026b6790f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 24 deletions

View file

@ -38,13 +38,28 @@ function source(url) {
}
addEventListener('turbolinks:load', function() {
autocompleteSetup();
});
addEventListener('ajax:success', function() {
autocompleteSetup();
});
function autocompleteSetup() {
for (let { type, url } of sources) {
for (let target of document.querySelectorAll(selector(type))) {
let select = autocomplete(target, options, [source(url)]);
select.on('autocomplete:selected', ({ target }, suggestion) => {
fire(target, 'autocomplete:select', suggestion);
select.autocomplete.setVal(suggestion.label);
});
for (let element of document.querySelectorAll(selector(type))) {
if (!element.dataset.autocompleteInitialized) {
autocompleteInitializeElement(element, url);
}
}
}
});
}
function autocompleteInitializeElement(element, url) {
const select = autocomplete(element, options, [source(url)]);
select.on('autocomplete:selected', ({ target }, suggestion) => {
fire(target, 'autocomplete:select', suggestion);
select.autocomplete.setVal(suggestion.label);
});
element.dataset.autocompleteInitialized = true;
}

View file

@ -120,17 +120,11 @@ class Dossier < ApplicationRecord
end
def build_default_champs
procedure.types_de_champ.each do |type_de_champ|
champ = type_de_champ.champ.build
if type_de_champ.repetition?
champ.add_row
end
procedure.build_champs.each do |champ|
champs << champ
end
procedure.types_de_champ_private.each do |type_de_champ|
champs_private << type_de_champ.champ.build
procedure.build_champs_private.each do |champ|
champs_private << champ
end
end

View file

@ -138,10 +138,15 @@ class Procedure < ApplicationRecord
# Warning: dossier after_save build_default_champs must be removed
# to save a dossier created from this method
def new_dossier
champs = types_de_champ.map { |tdc| tdc.champ.build }
champs_private = types_de_champ_private.map { |tdc| tdc.champ.build }
Dossier.new(procedure: self, champs: build_champs, champs_private: build_champs_private)
end
Dossier.new(procedure: self, champs: champs, champs_private: champs_private)
def build_champs
types_de_champ.map(&:build_champ)
end
def build_champs_private
types_de_champ_private.map(&:build_champ)
end
def default_path

View file

@ -120,6 +120,10 @@ class TypeDeChamp < ApplicationRecord
}
end
def build_champ
dynamic_type.build_champ
end
def self.type_de_champs_list_fr
type_champs.map { |champ| [I18n.t("activerecord.attributes.type_de_champ.type_champs.#{champ.last}"), champ.first] }
end

View file

@ -1,2 +1,7 @@
class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase
def build_champ
champ = super
champ.add_row
champ
end
end

View file

@ -19,4 +19,8 @@ class TypesDeChamp::TypeDeChampBase
}
]
end
def build_champ
@type_de_champ.champ.build
end
end

View file

@ -5,11 +5,9 @@
= form.fields_for :champs, champ do |form|
= render partial: 'shared/dossiers/editable_champs/editable_champ', locals: { champ: form.object, form: form }
= form.hidden_field :_destroy, disabled: true
%button.button.danger.remove-row
Supprimer
- if champ.persisted?
%button.button.danger.remove-row
Supprimer
- if champ.persisted?
= link_to "Ajouter une ligne pour « #{champ.libelle} »", champs_repetition_path(form.index), class: 'button add-row', data: { remote: true, method: 'POST', params: { champ_id: champ&.id }.to_query }
- else
%button.button.add-row{ disabled: true }
= "Ajouter une ligne pour « #{champ.libelle} »"