From cbccdea8e0ffd63c1829edddb094570e84b0c12e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 11:38:58 +0100 Subject: [PATCH] move displayed_fields for select to procedure_controller --- .../instructeurs/procedures_controller.rb | 15 +-------------- app/models/procedure_presentation.rb | 15 ++++++++++----- app/views/instructeurs/procedures/show.html.haml | 7 +++---- spec/models/procedure_presentation_spec.rb | 8 ++++---- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index ae0232381..108be8994 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -44,13 +44,12 @@ module Instructeurs def show @procedure = procedure - @available_fields_to_filters = available_fields_to_filters # Technically, procedure_presentation already sets the attribute. # Setting it here to make clear that it is used by the view @procedure_presentation = procedure_presentation - @displayed_fields_values = displayed_fields_values @current_filters = current_filters + @displayed_fields_options, @displayed_fields_selected = procedure_presentation.displayed_fields_for_select @a_suivre_dossiers = current_instructeur .dossiers @@ -275,10 +274,6 @@ module Instructeurs procedure_presentation.fields.find { |c| c['table'] == table && c['column'] == column } end - def field_id(field) - field.values_at('table', 'column').join('/') - end - def assign_to current_instructeur.assign_to.joins(:groupe_instructeur).find_by(groupe_instructeurs: { procedure: procedure }) end @@ -316,18 +311,10 @@ module Instructeurs procedure_presentation end - def displayed_fields_values - procedure_presentation.displayed_fields.map { |field| field_id(field) } - end - def current_filters @current_filters ||= procedure_presentation.filters[statut] end - def available_fields_to_filters - procedure_presentation.fields_for_select - end - def kaminarize(current_page, total) @dossiers.instance_eval <<-EVAL def current_page diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index c5e6e493c..e03014552 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -75,13 +75,14 @@ class ProcedurePresentation < ApplicationRecord fields end - def fields_for_select - fields.map do |field| - [field['label'], "#{field['table']}/#{field['column']}"] - end + def displayed_fields_for_select + [ + fields.map { |field| [field['label'], field_id(field)] }, + displayed_fields.map { |field| field_id(field) } + ] end - def displayed_field_values(dossier) + def displayed_fields_values(dossier) displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) } end @@ -219,6 +220,10 @@ class ProcedurePresentation < ApplicationRecord private + def field_id(field) + field.values_at('table', 'column').join('/') + end + def find_field(table, column) fields.find { |c| c['table'] == table && c['column'] == column } end diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index f8f4b395f..412fadc0c 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -83,7 +83,7 @@ #filter-menu.dropdown-content.left-aligned.fade-in-down = form_tag add_filter_instructeur_procedure_path(@procedure), method: :post, class: 'dropdown-form large' do = label_tag :field, "Colonne" - = select_tag :field, options_for_select(@available_fields_to_filters) + = select_tag :field, options_for_select(@displayed_fields_options) %br = label_tag :value, "Valeur" = text_field_tag :value @@ -123,8 +123,7 @@ #custom-menu.dropdown-content.fade-in-down = form_tag update_displayed_fields_instructeur_procedure_path(@procedure), method: :patch, class: 'dropdown-form columns-form' do = select_tag :values, - options_for_select(@procedure_presentation.fields_for_select, - selected: @displayed_fields_values), + options_for_select(@displayed_fields_options, selected: @displayed_fields_selected), multiple: true, class: 'select2-limited' = submit_tag "Enregistrer", class: 'button' @@ -142,7 +141,7 @@ = link_to(instructeur_dossier_path(@procedure, dossier), class: 'cell-link') do = dossier.id - - @procedure_presentation.displayed_field_values(dossier).each do |value| + - @procedure_presentation.displayed_fields_values(dossier).each do |value| %td / FIXME: value should automatically fallback to `""` instead of nil / #get_value should call to_s on the champ diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 1c118c7ad..0f237d925 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -104,7 +104,7 @@ describe ProcedurePresentation do end end - describe "#fields_for_select" do + describe "#displayed_fields_for_select" do subject { create(:procedure_presentation, assign_to: assign_to) } before do @@ -122,13 +122,13 @@ describe ProcedurePresentation do ]) end - it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) } + it { expect(subject.displayed_fields_for_select).to eq([[["label1", "table1/column1"], ["label2", "table2/column2"]], ["user/email"]]) } end describe '#get_value' do let(:procedure_presentation) { create(:procedure_presentation, procedure: procedure, assign_to: assign_to, displayed_fields: [{ 'table' => table, 'column' => column }]) } - subject { procedure_presentation.displayed_field_values(dossier).first } + subject { procedure_presentation.displayed_fields_values(dossier).first } context 'for self table' do let(:table) { 'self' } @@ -217,7 +217,7 @@ describe ProcedurePresentation do let!(:follow2) { create(:follow, dossier: dossier, instructeur: create(:instructeur, email: 'user2@host')) } it "return emails of followers instructeurs" do - emails_to_display = procedure_presentation.displayed_field_values(dossier).first.split(', ').sort + emails_to_display = procedure_presentation.displayed_fields_values(dossier).first.split(', ').sort expect(emails_to_display).to eq ["user1@host", "user2@host"] end end