Instructeurs: Changed the filters from 'true/false' to 'oui/non' for yes_no type_de_champ

This commit is contained in:
Judith 2020-06-08 14:33:58 +02:00
parent 72d1ca1f42
commit ad53dfa84d
6 changed files with 142 additions and 23 deletions

View file

@ -167,18 +167,7 @@ module Instructeurs
def add_filter
if params[:value].present?
filters = procedure_presentation.filters
table, column = params[:field].split('/')
label = find_field(table, column)['label']
filters[statut] << {
'label' => label,
'table' => table,
'column' => column,
'value' => params[:value]
}
procedure_presentation.update(filters: filters)
procedure_presentation.add_filter(statut, params[:field], params[:value])
end
redirect_back(fallback_location: instructeur_procedure_url(procedure))

View file

@ -173,8 +173,42 @@ class ProcedurePresentation < ApplicationRecord
dossiers.includes(relations_to_include)
end
def human_value_for_filter(filter)
if filter['table'] == 'type_de_champ'
type_de_champ = TypeDeChamp.find_by(id: filter['column'])
return type_de_champ.dynamic_type.filter_to_human(filter['value'])
end
filter['value']
end
def add_filter(statut, field, value)
if value.present?
updated_filters = self.filters
table, column = field.split('/')
label = find_field(table, column)['label']
if table == 'type_de_champ'
type_de_champ = TypeDeChamp.find_by(id: column)
value = type_de_champ.dynamic_type.human_to_filter(value.downcase)
end
updated_filters[statut] << {
'label' => label,
'table' => table,
'column' => column,
'value' => value
}
update(filters: updated_filters)
end
end
private
def find_field(table, column)
fields.find { |c| c['table'] == table && c['column'] == column }
end
def check_allowed_displayed_fields
displayed_fields.each do |field|
check_allowed_field(:displayed_fields, field)

View file

@ -23,4 +23,12 @@ class TypesDeChamp::TypeDeChampBase
def build_champ
@type_de_champ.champ.build
end
def filter_to_human(filter_value)
filter_value
end
def human_to_filter(human_value)
human_value
end
end

View file

@ -1,2 +1,22 @@
class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::CheckboxTypeDeChamp
def filter_to_human(filter_value)
if filter_value == "true"
"oui"
elsif filter_value == "false"
"non"
else
filter_value
end
end
def human_to_filter(human_value)
human_value.downcase!
if human_value == "oui"
"true"
elsif human_value == "non"
"false"
else
human_value
end
end
end

View file

@ -100,7 +100,7 @@
%span.filter
= link_to remove_filter_instructeur_procedure_path(@procedure, { statut: @statut, table: filter['table'], column: filter['column'], value: filter['value'] }) do
%img.close-icon{ src: image_url("close.svg") }
= "#{filter['label'].truncate(50)} : #{filter['value']}"
= "#{filter['label'].truncate(50)} : #{@procedure_presentation.human_value_for_filter(filter)}"
%table.table.dossiers-table.hoverable
%thead
%tr

View file

@ -1,8 +1,9 @@
describe ProcedurePresentation do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
let(:assign_to) { create(:assign_to, procedure: procedure) }
let(:first_type_de_champ_id) { assign_to.procedure.types_de_champ.first.id.to_s }
let (:procedure_presentation_id) {
let(:first_type_de_champ) { assign_to.procedure.types_de_champ.first }
let(:first_type_de_champ_id) { first_type_de_champ.id.to_s }
let(:procedure_presentation) {
ProcedurePresentation.create(
assign_to: assign_to,
displayed_fields: [
@ -10,10 +11,11 @@ describe ProcedurePresentation do
{ "label" => "test2", "table" => "type_de_champ", "column" => first_type_de_champ_id }
],
sort: { "table" => "user", "column" => "email", "order" => "asc" },
filters: { "a-suivre" => [], "suivis" => [{ "label" => "label1", "table" => "self", "column" => "created_at" }] }
).id
filters: filters
)
}
let (:procedure_presentation) { ProcedurePresentation.find(procedure_presentation_id) }
let(:procedure_presentation_id) { procedure_presentation.id }
let(:filters) { { "a-suivre" => [], "suivis" => [{ "label" => "label1", "table" => "self", "column" => "created_at" }] } }
describe "#displayed_fields" do
it { expect(procedure_presentation.displayed_fields).to eq([{ "label" => "test1", "table" => "user", "column" => "email" }, { "label" => "test2", "table" => "type_de_champ", "column" => first_type_de_champ_id }]) }
@ -499,12 +501,14 @@ describe ProcedurePresentation do
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
let(:type_de_champ) { procedure.types_de_champ.first }
before do
kept_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'keep me')
discarded_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'discard me')
end
context 'with single value' do
before do
kept_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'keep me')
discarded_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'discard me')
end
it { is_expected.to contain_exactly(kept_dossier.id) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'with multiple search values' do
let(:filter) do
@ -517,6 +521,8 @@ describe ProcedurePresentation do
let(:other_kept_dossier) { create(:dossier, procedure: procedure) }
before do
kept_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'keep me')
discarded_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'discard me')
other_kept_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'and me too')
end
@ -524,6 +530,18 @@ describe ProcedurePresentation do
is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id)
end
end
context 'with yes_no type_de_champ' do
let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'true' }] }
let(:procedure) { create(:procedure, :with_yes_no) }
before do
kept_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'true')
discarded_dossier.champs.find_by(type_de_champ: type_de_champ).update(value: 'false')
end
it { is_expected.to contain_exactly(kept_dossier.id) }
end
end
context 'for type_de_champ_private table' do
@ -864,4 +882,54 @@ describe ProcedurePresentation do
end
end
end
describe "#human_value_for_filter" do
let(:filters) { { "suivis" => [{ "label" => "label1", "table" => "type_de_champ", "column" => first_type_de_champ_id, "value" => "true" }] } }
subject { procedure_presentation.human_value_for_filter(procedure_presentation.filters["suivis"].first) }
context 'when type_de_champ text' do
it 'should passthrough value' do
expect(subject).to eq("true")
end
end
context 'when type_de_champ yes_no' do
let(:procedure) { create(:procedure, :with_yes_no) }
it 'should transform value' do
expect(subject).to eq("oui")
end
end
end
describe "#add_filter" do
let(:filters) { { "suivis" => [] } }
context 'when type_de_champ yes_no' do
let(:procedure) { create(:procedure, :with_yes_no) }
it 'should downcase and transform value' do
procedure_presentation.add_filter("suivis", "type_de_champ/#{first_type_de_champ_id}", "Oui")
expect(procedure_presentation.filters).to eq({ "suivis" => [
{ "label" => first_type_de_champ.libelle, "table" => "type_de_champ", "column" => first_type_de_champ_id, "value" => "true" }
]
})
end
end
context 'when type_de_champ text' do
let(:filters) { { "suivis" => [] } }
it 'should downcase and passthrough value' do
procedure_presentation.add_filter("suivis", "type_de_champ/#{first_type_de_champ_id}", "Oui")
expect(procedure_presentation.filters).to eq({ "suivis" => [
{ "label" => first_type_de_champ.libelle, "table" => "type_de_champ", "column" => first_type_de_champ_id, "value" => "oui" }
]
})
end
end
end
end