From efcac5192a1e64fc7c428be0bc2fbe612ba89872 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 3 Oct 2018 14:46:12 +0200 Subject: [PATCH] [#2750] Move fields_for_select to ProcedurePresentation --- .../new_gestionnaire/procedures_controller.rb | 8 ++++--- app/models/procedure.rb | 6 ----- app/models/procedure_presentation.rb | 6 +++++ .../procedures/show.html.haml | 6 ++--- spec/models/procedure_presentation_spec.rb | 21 ++++++++++++++++++ spec/models/procedure_spec.rb | 22 ------------------- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/app/controllers/new_gestionnaire/procedures_controller.rb b/app/controllers/new_gestionnaire/procedures_controller.rb index 5810348f9..48038b323 100644 --- a/app/controllers/new_gestionnaire/procedures_controller.rb +++ b/app/controllers/new_gestionnaire/procedures_controller.rb @@ -28,7 +28,9 @@ module NewGestionnaire @current_filters = current_filters @available_fields_to_filters = available_fields_to_filters - @displayed_fields = procedure_presentation.displayed_fields + # 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 @a_suivre_dossiers = procedure @@ -225,13 +227,13 @@ module NewGestionnaire "#{field['table']}/#{field['column']}" end - procedure.fields_for_select.reject do |field| + procedure_presentation.fields_for_select.reject do |field| current_filters_fields_ids.include?(field[1]) end end def eager_load_displayed_fields - @displayed_fields + procedure_presentation.displayed_fields .reject { |field| field['table'] == 'self' } .group_by do |field| if ['type_de_champ', 'type_de_champ_private'].include?(field['table']) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index ec4c26a36..7a99f8cc4 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -300,12 +300,6 @@ class Procedure < ApplicationRecord DossierFieldService.new.fields(self) end - def fields_for_select - fields.map do |field| - [field['label'], "#{field['table']}/#{field['column']}"] - end - end - def self.default_sort { 'table' => 'self', diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 7b0c61479..45c64058f 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -42,6 +42,12 @@ class ProcedurePresentation < ApplicationRecord end end + def fields_for_select + procedure.fields.map do |field| + [field['label'], "#{field['table']}/#{field['column']}"] + end + end + private def dossier_field_service diff --git a/app/views/new_gestionnaire/procedures/show.html.haml b/app/views/new_gestionnaire/procedures/show.html.haml index 8b5054168..63d07a986 100644 --- a/app/views/new_gestionnaire/procedures/show.html.haml +++ b/app/views/new_gestionnaire/procedures/show.html.haml @@ -73,7 +73,7 @@ = render partial: "header_field", locals: { field: { "label" => "Nº dossier", "table" => "self", "column" => "id" }, classname: "number-col" } - - @displayed_fields.each do |field| + - @procedure_presentation.displayed_fields.each do |field| = render partial: "header_field", locals: { field: field, classname: "" } = render partial: "header_field", locals: { field: { "label" => "Statut", "table" => "self", "column" => "state" }, classname: "status-col" } @@ -85,7 +85,7 @@ .dropdown-content.fade-in-down = form_tag update_displayed_fields_gestionnaire_procedure_path(@procedure), method: :patch, class: 'dropdown-form columns-form' do = select_tag :values, - options_for_select(@procedure.fields_for_select, + options_for_select(@procedure_presentation.fields_for_select, selected: @displayed_fields_values), multiple: true, class: 'select2-limited' @@ -105,7 +105,7 @@ = link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do = dossier.id - - @displayed_fields.each do |field| + - @procedure_presentation.displayed_fields.each do |field| %td = link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do = dossier_field_service.get_value(dossier, field['table'], field['column']) diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 3cc555401..1e6a685b6 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -46,4 +46,25 @@ describe ProcedurePresentation do it { expect(build(:procedure_presentation, filters: { "suivis" => [{ "table" => "user", "column" => "reset_password_token", "order" => "asc" }] })).to be_invalid } end end + + describe "#fields_for_select" do + subject { create(:procedure_presentation) } + + before do + allow(subject.procedure).to receive(:fields).and_return([ + { + "label" => "label1", + "table" => "table1", + "column" => "column1" + }, + { + "label" => "label2", + "table" => "table2", + "column" => "column2" + } + ]) + end + + it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) } + end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 6e3a93a23..de66654f6 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -664,28 +664,6 @@ describe Procedure do it { expect(subject.fields).to eq(expected) } end - - describe "#fields_for_select" do - subject { create(:procedure) } - - before do - allow(subject).to receive(:fields).and_return([ - { - "label" => "label1", - "table" => "table1", - "column" => "column1" - }, - { - "label" => "label2", - "table" => "table2", - "column" => "column2" - } - ]) - end - - it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) } - end - describe ".default_sort" do it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) } end