move field_enum to instructeur filter component

This commit is contained in:
simon lehericey 2024-07-19 17:53:04 +02:00 committed by mfo
parent 73293b0d06
commit 772424ff5e
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
5 changed files with 62 additions and 45 deletions

View file

@ -8,6 +8,10 @@ class ApplicationComponent < ViewComponent::Base
controller.current_user
end
def current_instructeur
controller.current_instructeur
end
def current_administrateur
controller.current_administrateur
end

View file

@ -1,4 +1,6 @@
class Dossiers::InstructeurFilterComponent < ApplicationComponent
attr_reader :procedure, :procedure_presentation, :statut, :facet
def initialize(procedure:, procedure_presentation:, statut:, facet: nil)
@procedure = procedure
@procedure_presentation = procedure_presentation
@ -6,12 +8,20 @@ class Dossiers::InstructeurFilterComponent < ApplicationComponent
@facet = facet
end
attr_reader :procedure, :procedure_presentation, :statut, :facet
def facet_type = facet.present? ? facet.type : :text
def options_for_select_of_field
procedure_presentation.field_enum(field_id)
if facet.scope.present?
I18n.t(facet.scope).map(&:to_a).map(&:reverse)
elsif facet.table == 'groupe_instructeur'
current_instructeur.groupe_instructeurs.filter_map do
if _1.procedure_id == procedure.id
[_1.label, _1.id]
end
end
else
find_type_de_champ(facet.column).options_for_select
end
end
def filter_react_props
@ -25,4 +35,14 @@ class Dossiers::InstructeurFilterComponent < ApplicationComponent
data: { no_autosubmit: 'input blur', no_autosubmit_on_empty: 'true', autosubmit_target: 'input' }
}
end
private
def find_type_de_champ(column)
TypeDeChamp
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision_id: procedure.revisions })
.order(created_at: :desc)
.find_by(stable_id: column)
end
end

View file

@ -271,21 +271,6 @@ class ProcedurePresentation < ApplicationRecord
slice(:filters, :sort, :displayed_fields)
end
def field_enum(facet_id)
facet = Facet.find(procedure:, id: facet_id)
if facet.scope.present?
I18n.t(facet.scope).map(&:to_a).map(&:reverse)
elsif facet.table == 'groupe_instructeur'
instructeur.groupe_instructeurs.filter_map do
if _1.procedure_id == procedure.id
[_1.label, _1.id]
end
end
else
find_type_de_champ(facet.column).options_for_select
end
end
def sortable?(field)
sort['table'] == field.table &&
sort['column'] == field.column

View file

@ -0,0 +1,35 @@
describe Dossiers::InstructeurFilterComponent, type: :component do
let(:component) { described_class.new(procedure:, procedure_presentation:, statut:, facet:) }
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, instructeurs: [instructeur]) }
let(:procedure_presentation) { nil }
let(:statut) { nil }
before do
allow(component).to receive(:current_instructeur).and_return(instructeur)
end
describe '.options_for_select_of_field' do
subject { component.options_for_select_of_field }
context "facet is groupe_instructeur" do
let(:facet) { double("Facet", scope: nil, table: 'groupe_instructeur') }
let!(:gi_2) { instructeur.groupe_instructeurs.create(label: 'gi2', procedure:) }
let!(:gi_3) { instructeur.groupe_instructeurs.create(label: 'gi3', procedure: create(:procedure)) }
it { is_expected.to eq([['défaut', procedure.defaut_groupe_instructeur.id], ['gi2', gi_2.id]]) }
end
context 'when facet is dropdown' do
let(:types_de_champ_public) { [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }] }
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
let(:drop_down_stable_id) { procedure.active_revision.types_de_champ.first.stable_id }
let(:facet) { Facet.new(table: 'type_de_champ', scope: nil, column: drop_down_stable_id) }
it 'find most recent tdc' do
is_expected.to eq(['Paris', 'Lyon', 'Marseille'])
end
end
end
end

View file

@ -904,31 +904,4 @@ describe ProcedurePresentation do
end
end
end
describe '#field_enum' do
context "field is groupe_instructeur" do
let!(:gi_2) { instructeur.groupe_instructeurs.create(label: 'gi2', procedure:) }
let!(:gi_3) { instructeur.groupe_instructeurs.create(label: 'gi3', procedure: create(:procedure)) }
subject { procedure_presentation.field_enum('groupe_instructeur/id') }
it { is_expected.to eq([['défaut', procedure.defaut_groupe_instructeur.id], ['gi2', gi_2.id]]) }
end
context 'when field is dropdown' do
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :text }], types_de_champ_private: [{}]) }
let(:tdc) { procedure.published_revision.types_de_champ_public.first }
before do
procedure.draft_revision
.find_and_ensure_exclusive_use(tdc.stable_id)
.update(type_champ: :drop_down_list,
drop_down_list_value: "Paris\nLyon\nMarseille")
procedure.publish_revision!
end
subject { procedure_presentation.field_enum("type_de_champ/#{tdc.id}") }
it 'find most recent tdc' do
expect(subject).to eq(["Paris", "Lyon", "Marseille"])
end
end
end
end