[#2750] Move get_value to ProcedurePresentation
This commit is contained in:
parent
fc157c3e4c
commit
9fa7a6fd0e
5 changed files with 75 additions and 79 deletions
|
@ -52,6 +52,23 @@ class ProcedurePresentation < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def get_value(dossier, table, column)
|
||||
dossier_field_service.assert_valid_column(dossier.procedure, 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 sorted_ids(dossiers, gestionnaire)
|
||||
table = sort['table']
|
||||
column = sanitized_column(sort)
|
||||
|
|
|
@ -40,23 +40,6 @@ class DossierFieldService
|
|||
fields
|
||||
end
|
||||
|
||||
def get_value(dossier, table, column)
|
||||
assert_valid_column(dossier.procedure, 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 assert_valid_column(procedure, table, column)
|
||||
if !valid_column?(procedure, table, column)
|
||||
raise "Invalid column #{table}.#{column}"
|
||||
|
|
|
@ -92,7 +92,6 @@
|
|||
= submit_tag "Enregistrer", class: 'button'
|
||||
|
||||
%tbody
|
||||
- dossier_field_service = DossierFieldService.new
|
||||
- @dossiers.each do |dossier|
|
||||
%tr
|
||||
%td.folder-col
|
||||
|
@ -108,7 +107,7 @@
|
|||
- @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'])
|
||||
= @procedure_presentation.get_value(dossier, field['table'], field['column'])
|
||||
|
||||
%td.status-col
|
||||
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
|
||||
|
|
|
@ -108,6 +108,63 @@ describe ProcedurePresentation do
|
|||
it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) }
|
||||
end
|
||||
|
||||
describe '#get_value' do
|
||||
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to) }
|
||||
|
||||
subject { procedure_presentation.get_value(dossier, table, column) }
|
||||
|
||||
context 'for self table' do
|
||||
let(:table) { 'self' }
|
||||
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
before { dossier.touch(time: DateTime.new(2018, 9, 25)) }
|
||||
|
||||
it { is_expected.to eq(DateTime.new(2018, 9, 25)) }
|
||||
end
|
||||
|
||||
context 'for user table' do
|
||||
let(:table) { 'user' }
|
||||
let(:column) { 'email' }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bla@yopmail.com')) }
|
||||
|
||||
it { is_expected.to eq('bla@yopmail.com') }
|
||||
end
|
||||
|
||||
context 'for etablissement table' do
|
||||
let(:table) { 'etablissement' }
|
||||
let(:column) { 'code_postal' } # All other columns work the same, no extra test required
|
||||
|
||||
let!(:dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
|
||||
|
||||
it { is_expected.to eq('75008') }
|
||||
end
|
||||
|
||||
context 'for type_de_champ table' do
|
||||
let(:table) { 'type_de_champ' }
|
||||
let(:column) { procedure.types_de_champ.first.id.to_s }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
before { dossier.champs.first.update(value: 'kale') }
|
||||
|
||||
it { is_expected.to eq('kale') }
|
||||
end
|
||||
|
||||
context 'for type_de_champ_private table' do
|
||||
let(:table) { 'type_de_champ_private' }
|
||||
let(:column) { procedure.types_de_champ_private.first.id.to_s }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
before { dossier.champs_private.first.update(value: 'quinoa') }
|
||||
|
||||
it { is_expected.to eq('quinoa') }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#sorted_ids' do
|
||||
let(:gestionnaire) { create(:gestionnaire) }
|
||||
let(:assign_to) { create(:assign_to, procedure: procedure, gestionnaire: gestionnaire) }
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe DossierFieldService do
|
||||
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
|
||||
|
||||
describe '#get_value' do
|
||||
subject { described_class.new.get_value(dossier, table, column) }
|
||||
|
||||
context 'for self table' do
|
||||
let(:table) { 'self' }
|
||||
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
before { dossier.touch(time: DateTime.new(2018, 9, 25)) }
|
||||
|
||||
it { is_expected.to eq(DateTime.new(2018, 9, 25)) }
|
||||
end
|
||||
|
||||
context 'for user table' do
|
||||
let(:table) { 'user' }
|
||||
let(:column) { 'email' }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bla@yopmail.com')) }
|
||||
|
||||
it { is_expected.to eq('bla@yopmail.com') }
|
||||
end
|
||||
|
||||
context 'for etablissement table' do
|
||||
let(:table) { 'etablissement' }
|
||||
let(:column) { 'code_postal' } # All other columns work the same, no extra test required
|
||||
|
||||
let!(:dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
|
||||
|
||||
it { is_expected.to eq('75008') }
|
||||
end
|
||||
|
||||
context 'for type_de_champ table' do
|
||||
let(:table) { 'type_de_champ' }
|
||||
let(:column) { procedure.types_de_champ.first.id.to_s }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
before { dossier.champs.first.update(value: 'kale') }
|
||||
|
||||
it { is_expected.to eq('kale') }
|
||||
end
|
||||
|
||||
context 'for type_de_champ_private table' do
|
||||
let(:table) { 'type_de_champ_private' }
|
||||
let(:column) { procedure.types_de_champ_private.first.id.to_s }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
before { dossier.champs_private.first.update(value: 'quinoa') }
|
||||
|
||||
it { is_expected.to eq('quinoa') }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue