From 40a5ff026667df2141135991b55db9c0233c4ca9 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Fri, 5 Oct 2018 15:25:24 +0200 Subject: [PATCH] [#2750] do parsing of displayed_fields inside ProcedurePresentation rather than in the view --- app/models/procedure_presentation.rb | 34 +++++++++++-------- .../procedures/show.html.haml | 5 ++- spec/models/procedure_presentation_spec.rb | 4 +-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 90f68e8f6..8297adb9a 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -85,21 +85,8 @@ class ProcedurePresentation < ApplicationRecord end end - def get_value(dossier, table, column) - assert_valid_column(table, column) - - case table - when 'self' - dossier.send(column) - when 'user' - dossier.user.send(column) - when 'etablissement' - dossier.etablissement&.send(column) - when 'type_de_champ' - dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value - when 'type_de_champ_private' - dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value - end + def displayed_field_values(dossier) + displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) } end def sorted_ids(dossiers, gestionnaire) @@ -176,6 +163,23 @@ class ProcedurePresentation < ApplicationRecord private + def get_value(dossier, table, column) + assert_valid_column(table, column) + + case table + when 'self' + dossier.send(column) + when 'user' + dossier.user.send(column) + when 'etablissement' + dossier.etablissement&.send(column) + when 'type_de_champ' + dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value + when 'type_de_champ_private' + dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value + end + end + def field_hash(label, table, column) { 'label' => label, diff --git a/app/views/new_gestionnaire/procedures/show.html.haml b/app/views/new_gestionnaire/procedures/show.html.haml index 3905904e5..4cd66ba4a 100644 --- a/app/views/new_gestionnaire/procedures/show.html.haml +++ b/app/views/new_gestionnaire/procedures/show.html.haml @@ -104,10 +104,9 @@ = link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do = dossier.id - - @procedure_presentation.displayed_fields.each do |field| + - @procedure_presentation.displayed_field_values(dossier).each do |value| %td - = link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do - = @procedure_presentation.get_value(dossier, field['table'], field['column']) + = link_to(value, gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') %td.status-col = link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 45d53f462..4296799d7 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -109,9 +109,9 @@ describe ProcedurePresentation do end describe '#get_value' do - let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to) } + let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, displayed_fields: [{ 'table' => table, 'column' => column }]) } - subject { procedure_presentation.get_value(dossier, table, column) } + subject { procedure_presentation.displayed_field_values(dossier).first } context 'for self table' do let(:table) { 'self' }