move displayed_fields for select to procedure_controller

This commit is contained in:
simon lehericey 2020-10-30 11:38:58 +01:00 committed by Paul Chavard
parent 55998b1284
commit cbccdea8e0
4 changed files with 18 additions and 27 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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