From 74c71a3776a1eb894b0fb19f8d39f580963caa57 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 2 Aug 2017 14:56:08 +0200 Subject: [PATCH] Form: use rails form helper --- app/controllers/root_controller.rb | 12 ++++++---- app/models/dossier.rb | 2 ++ .../dossiers/champs/_address.html.haml | 9 +++---- .../dossiers/champs/_champ_label.html.haml | 2 +- .../dossiers/champs/_checkbox.html.haml | 11 ++++----- .../dossiers/champs/_civilite.html.haml | 6 ++--- .../dossiers/champs/_date.html.haml | 10 ++++---- .../dossiers/champs/_datetime.html.haml | 24 ++++--------------- .../dossiers/champs/_departements.html.haml | 7 +++--- .../dossiers/champs/_dossier_link.html.haml | 13 ++++------ .../dossiers/champs/_drop_down_list.html.haml | 12 ++++------ .../dossiers/champs/_email.html.haml | 9 +++---- .../dossiers/champs/_engagement.html.haml | 11 ++++----- .../champs/_multiple_drop_down_list.html.haml | 12 +++++----- .../dossiers/champs/_number.html.haml | 9 +++---- .../dossiers/champs/_pays.html.haml | 7 +++--- .../dossiers/champs/_phone.html.haml | 9 +++---- .../dossiers/champs/_regions.html.haml | 7 +++--- .../dossiers/champs/_text.html.haml | 9 +++---- .../dossiers/champs/_textarea.html.haml | 11 ++++----- .../dossiers/champs/_yes_no.html.haml | 6 ++--- app/views/root/patron.html.haml | 16 ++++++++----- 22 files changed, 92 insertions(+), 122 deletions(-) diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index 0bb093d67..6837a259e 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -32,19 +32,19 @@ class RootController < ApplicationController end def patron - @all_champs = TypeDeChamp.type_champs + all_champs = TypeDeChamp.type_champs .map { |name, _| TypeDeChamp.new(type_champ: name, libelle: name, mandatory: true) } - .map { |type_de_champ| Champ.new(type_de_champ: type_de_champ) } + .map { |type_de_champ| ChampPublic.new(type_de_champ: type_de_champ) } .map.with_index do |champ, i| champ.id = i champ end - @all_champs + all_champs .select { |champ| champ.type_champ == 'header_section' } .each { |champ| champ.type_de_champ.libelle = 'un super titre de section' } - @all_champs + all_champs .select { |champ| %w(drop_down_list multiple_drop_down_list).include?(champ.type_champ) } .each do |champ| champ.type_de_champ.drop_down_list = DropDownList.new(type_de_champ: champ.type_de_champ) @@ -64,9 +64,11 @@ option C" } type_champ_values.each do |(type_champ, value)| - @all_champs + all_champs .select { |champ| champ.type_champ == type_champ.to_s } .each { |champ| champ.value = value } end + + @dossier = Dossier.new(champs: all_champs) end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d96e90b1a..fd0a1279a 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -43,6 +43,8 @@ class Dossier < ActiveRecord::Base belongs_to :procedure belongs_to :user + accepts_nested_attributes_for :champs + default_scope { where(hidden_at: nil) } scope :state_brouillon, -> { where(state: BROUILLON) } scope :state_not_brouillon, -> { where.not(state: BROUILLON) } diff --git a/app/views/new_gestionnaire/dossiers/champs/_address.html.haml b/app/views/new_gestionnaire/dossiers/champs/_address.html.haml index af9fb84c2..c0707410f 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_address.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_address.html.haml @@ -1,9 +1,6 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'text', += form.text_field :value, 'data-address': 'true', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", placeholder: champ.libelle, - value: champ.value, - required: champ.mandatory } + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_champ_label.html.haml b/app/views/new_gestionnaire/dossiers/champs/_champ_label.html.haml index 7807b7c6a..b2e5960e4 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_champ_label.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_champ_label.html.haml @@ -1 +1 @@ -%label{ for: :"champs_#{champ.id}" }> #{champ.libelle} #{champ.mandatory ? '*' : nil} += form.label :value, "#{champ.libelle} #{champ.mandatory ? '*' : nil}" diff --git a/app/views/new_gestionnaire/dossiers/champs/_checkbox.html.haml b/app/views/new_gestionnaire/dossiers/champs/_checkbox.html.haml index 09600b354..2668405ba 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_checkbox.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_checkbox.html.haml @@ -1,9 +1,8 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'checkbox', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", - checked: ('checked' if champ.value == 'on'), - required: champ.mandatory } += form.check_box :value, + { required: champ.mandatory }, + 'on', + 'off' %br diff --git a/app/views/new_gestionnaire/dossiers/champs/_civilite.html.haml b/app/views/new_gestionnaire/dossiers/champs/_civilite.html.haml index 734cf98ac..01952292d 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_civilite.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_civilite.html.haml @@ -1,10 +1,10 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } %div %label - = radio_button_tag "champs['#{champ.id}']", "M.", champ.value == 'Mme' ? false : true + = form.radio_button :value, 'M.' Monsieur %label - = radio_button_tag "champs['#{champ.id}']", "Mme", champ.value == 'Mme' + = form.radio_button :value, 'Mme.' Madame diff --git a/app/views/new_gestionnaire/dossiers/champs/_date.html.haml b/app/views/new_gestionnaire/dossiers/champs/_date.html.haml index 0e8060144..5bdd4ad45 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_date.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_date.html.haml @@ -1,8 +1,6 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ name: "champs['#{champ.id}']", - placeholder: "JJ/MM/AAAA", - id: "champs_#{champ.id}", += form.date_field :value, value: champ.value, - type: "date", - required: champ.mandatory } + placeholder: 'JJ/MM/AAAA', + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_datetime.html.haml b/app/views/new_gestionnaire/dossiers/champs/_datetime.html.haml index 8704e17bc..4dabb6705 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_datetime.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_datetime.html.haml @@ -1,22 +1,6 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } + +- parsed_value = champ.value.present? ? DateTime.parse(champ.value) : DateTime.now .datetime - %input{ name: "champs['#{champ.id}']", - placeholder: champ.libelle, - id: "champs_#{champ.id}", - value: (champ.value.split(/[ ][0-9]*:[0-9]*/).first unless champ.value.nil?), - type: 'date', - required: champ.mandatory } - - %select{ name: "time_hour['#{champ.id}']", id: "time_hour_#{champ.id}", required: champ.mandatory } - - (0..23).each do |num| - - num = "%.2i" %num - %option{ value: num, selected: (:selected if champ.same_hour?(num)) } - = num - h - %select{ name: "time_minute['#{champ.id}']", id: "time_minute_#{champ.id}", required: champ.mandatory } - - (0..55).step(5) do |num| - - num = "%.2i" %num - %option{ value: num, selected: (:selected if champ.same_minute?(num)) } - = num - min + = form.datetime_select(:value, selected: parsed_value, start_year: 1950, end_year: 2100, minute_step: 5) diff --git a/app/views/new_gestionnaire/dossiers/champs/_departements.html.haml b/app/views/new_gestionnaire/dossiers/champs/_departements.html.haml index 8ef409bed..c16358e58 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_departements.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_departements.html.haml @@ -1,4 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -= select_tag("champs['#{champ.id}']", - options_for_select(Champ.departements, selected: champ.value)) += form.select :value, + Champ.departements, + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_dossier_link.html.haml b/app/views/new_gestionnaire/dossiers/champs/_dossier_link.html.haml index 8ecf58dc3..54c9d9b12 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_dossier_link.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_dossier_link.html.haml @@ -1,4 +1,4 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } - dossier = Dossier.find_by(id: champ.value) - show_text_summary = dossier.present? @@ -6,14 +6,11 @@ - text_summary = dossier.try(:text_summary) .dossier-link - %input{ name: "champs['#{ champ.id }']", + = form.number_field :value, placeholder: "Numéro de dossier", - id: "champs_#{ champ.id }", - value: champ.value, - type: 'number', - 'autocomplete' => 'off', - 'data-type' => 'dossier-link', - required: champ.mandatory } + autocomplete: 'off', + 'data-type': 'dossier-link', + required: champ.mandatory .help-block %p.text-info{ style: show_text_summary ? nil : 'display: none;' } diff --git a/app/views/new_gestionnaire/dossiers/champs/_drop_down_list.html.haml b/app/views/new_gestionnaire/dossiers/champs/_drop_down_list.html.haml index c915b1797..c629a8cc8 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_drop_down_list.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_drop_down_list.html.haml @@ -1,8 +1,6 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -- if champ.drop_down_list && champ.drop_down_list.options.any? - = select_tag("champs['#{champ.id}']", - options_for_select(champ.drop_down_list.options, - selected: champ.drop_down_list.selected_options_without_decorator(champ), - disabled: champ.drop_down_list.disabled_options), - multiple: false) += form.select :value, + champ.drop_down_list.options, + disabled: champ.drop_down_list.disabled_options, + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_email.html.haml b/app/views/new_gestionnaire/dossiers/champs/_email.html.haml index 6684b7865..c9819de88 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_email.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_email.html.haml @@ -1,8 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'email', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", += form.email_field :value, placeholder: champ.libelle, - value: champ.value, - required: champ.mandatory } + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_engagement.html.haml b/app/views/new_gestionnaire/dossiers/champs/_engagement.html.haml index 09600b354..2668405ba 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_engagement.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_engagement.html.haml @@ -1,9 +1,8 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'checkbox', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", - checked: ('checked' if champ.value == 'on'), - required: champ.mandatory } += form.check_box :value, + { required: champ.mandatory }, + 'on', + 'off' %br diff --git a/app/views/new_gestionnaire/dossiers/champs/_multiple_drop_down_list.html.haml b/app/views/new_gestionnaire/dossiers/champs/_multiple_drop_down_list.html.haml index 248fb1932..5f0fb9439 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_multiple_drop_down_list.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_multiple_drop_down_list.html.haml @@ -1,9 +1,9 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } - if champ.drop_down_list && champ.drop_down_list.options.any? - = select_tag("champs['#{champ.id}']", - options_for_select(champ.drop_down_list.options, - selected: champ.drop_down_list.selected_options_without_decorator(champ), - disabled: champ.drop_down_list.disabled_options), + = form.select :value, + champ.drop_down_list.options, + { selected: champ.drop_down_list.selected_options_without_decorator(champ), + disabled: champ.drop_down_list.disabled_options }, multiple: true, - class: 'select2') + class: 'select2' diff --git a/app/views/new_gestionnaire/dossiers/champs/_number.html.haml b/app/views/new_gestionnaire/dossiers/champs/_number.html.haml index 5e2b61e8d..c9bcb8eae 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_number.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_number.html.haml @@ -1,8 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'number', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", += form.number_field :value, placeholder: champ.libelle, - value: champ.value, - required: champ.mandatory } + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_pays.html.haml b/app/views/new_gestionnaire/dossiers/champs/_pays.html.haml index 96962d7dc..cad18df07 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_pays.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_pays.html.haml @@ -1,4 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -= select_tag("champs['#{champ.id}']", - options_for_select(Champ.pays, selected: champ.value)) += form.select :value, + Champ.pays, + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_phone.html.haml b/app/views/new_gestionnaire/dossiers/champs/_phone.html.haml index c1bbfd1ce..e42dbfad1 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_phone.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_phone.html.haml @@ -1,8 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'tel', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", += form.phone_field :value, placeholder: champ.libelle, - value: champ.value, - required: champ.mandatory } + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_regions.html.haml b/app/views/new_gestionnaire/dossiers/champs/_regions.html.haml index ad4cf5531..eb9ffaf8d 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_regions.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_regions.html.haml @@ -1,4 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -= select_tag("champs['#{champ.id}']", - options_for_select(Champ.regions, selected: champ.value)) += form.select :value, + Champ.regions, + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_text.html.haml b/app/views/new_gestionnaire/dossiers/champs/_text.html.haml index e42aa8584..2e34ffa6c 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_text.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_text.html.haml @@ -1,8 +1,5 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%input{ type: 'text', - name: "champs['#{champ.id}']", - id: "champs_#{champ.id}", += form.text_field :value, placeholder: champ.libelle, - value: champ.value, - required: champ.mandatory } + required: champ.mandatory diff --git a/app/views/new_gestionnaire/dossiers/champs/_textarea.html.haml b/app/views/new_gestionnaire/dossiers/champs/_textarea.html.haml index 37c5e031d..88c5803aa 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_textarea.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_textarea.html.haml @@ -1,8 +1,7 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } -%textarea{ name: "champs['#{champ.id}']", += form.text_area :value, + row: 6, placeholder: champ.description, - id: "champs_#{champ.id}", - row: '6', - required: champ.mandatory } - = sanitize(champ.value) + required: champ.mandatory, + value: sanitize(champ.value) diff --git a/app/views/new_gestionnaire/dossiers/champs/_yes_no.html.haml b/app/views/new_gestionnaire/dossiers/champs/_yes_no.html.haml index 802595b19..9e9207f86 100644 --- a/app/views/new_gestionnaire/dossiers/champs/_yes_no.html.haml +++ b/app/views/new_gestionnaire/dossiers/champs/_yes_no.html.haml @@ -1,10 +1,10 @@ -= render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { champ: champ } += render partial: 'new_gestionnaire/dossiers/champs/champ_label', locals: { form: form, champ: champ } %div %label - = radio_button_tag "champs['#{champ.id}']", "true", champ.value == 'true' + = form.radio_button :value, true Oui %label - = radio_button_tag "champs['#{champ.id}']", "false", champ.value == 'false' + = form.radio_button :value, false Non diff --git a/app/views/root/patron.html.haml b/app/views/root/patron.html.haml index 383c520c1..a0c61bb4c 100644 --- a/app/views/root/patron.html.haml +++ b/app/views/root/patron.html.haml @@ -11,12 +11,16 @@ %h1 Formulaires %form.form - - @all_champs.each do |champ| - = render partial: "new_gestionnaire/dossiers/champs/#{champ.type_champ}", locals: { champ: champ } - %input{ type: "password", value: "12345678" } - .send-wrapper - %input.button.send{ type: "submit", value: "Enregistrer un brouillon (formnovalidate)", formnovalidate: true } - %input.button.send{ type: "submit", value: "Envoyer" } + = form_for @dossier, url: '', html: { class: 'form' } do |f| + = f.fields_for :champs do |champ_form| + - champ = champ_form.object + = render partial: "new_gestionnaire/dossiers/champs/#{champ.type_champ}", + locals: { champ: champ, form: champ_form } + + %input{ type: "password", value: "12345678" } + .send-wrapper + = f.submit 'Enregistrer un brouillon (formnovalidate)', formnovalidate: true, class: 'button send' + = f.submit 'Envoyer', class: 'button send' %h1 Boutons