extract column_filter_value

This commit is contained in:
simon lehericey 2024-10-25 17:30:20 +02:00
parent c8332b5e22
commit b4ed393a55
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
6 changed files with 117 additions and 85 deletions

View file

@ -10,33 +10,6 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent
@column = column
end
def column_type = column.present? ? column.type : :text
def html_column_type
case column_type
when :datetime, :date
'date'
when :integer, :decimal
'number'
else
'text'
end
end
def options_for_select_of_column
if column.scope.present?
I18n.t(column.scope).map(&:to_a).map(&:reverse)
elsif column.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(column.column).options_for_select(column)
end
end
def filter_react_props
{
selected_key: column.present? ? column.id : '',
@ -63,15 +36,4 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent
end
def prefix = "#{procedure_presentation.filters_name_for(@statut)}[]"
private
def find_type_de_champ(column)
stable_id = column.to_s.split('->').first
TypeDeChamp
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision_id: procedure.revisions })
.order(created_at: :desc)
.find_by(stable_id:)
end
end

View file

@ -18,22 +18,8 @@
}
= label_tag :value, t('.value'), for: 'value', class: 'fr-label'
- if column_type.in?([:enum, :enums, :boolean])
= select_tag :filter,
options_for_select(options_for_select_of_column),
id: 'value',
name: "#{prefix}[filter]",
class: 'fr-select',
data: { no_autosubmit: true }
- else
%input#value.fr-input{
type: html_column_type,
name: "#{prefix}[filter]",
maxlength: FilteredColumn::FILTERS_VALUE_MAX_LENGTH,
disabled: column.nil? ? true : false,
data: { no_autosubmit: true },
required: true
}
= render Instructeurs::ColumnFilterValueComponent.new(column:, prefix:)
= hidden_field_tag :statut, statut
= hidden_field_tag :prefix, prefix
= submit_tag t('.add_filter'), class: 'fr-btn fr-btn--secondary fr-mt-2w'

View file

@ -0,0 +1,72 @@
# frozen_string_literal: true
class Instructeurs::ColumnFilterValueComponent < ApplicationComponent
attr_reader :column
def initialize(column:, prefix:)
@column = column
@prefix = prefix
end
def column_type = column.present? ? column.type : :text
def call
if column_type.in?([:enum, :enums, :boolean])
select_tag :filter,
options_for_select(options_for_select_of_column),
id: 'value',
name: "#{@prefix}[filter]",
class: 'fr-select',
data: { no_autosubmit: true }
else
tag.input(
class: 'fr-input',
id: 'value',
type: html_column_type,
name: "#{@prefix}[filter]",
maxlength: FilteredColumn::FILTERS_VALUE_MAX_LENGTH,
disabled: column.nil? ? true : false,
data: { no_autosubmit: true },
required: true
)
end
end
private
def type
case column_type
when :datetime, :date
'date'
when :integer, :decimal
'number'
else
'text'
end
end
def options_for_select_of_column
if column.scope.present?
I18n.t(column.scope).map(&:to_a).map(&:reverse)
elsif column.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(column.column).options_for_select(column)
end
end
def find_type_de_champ(column)
stable_id = column.to_s.split('->').first
TypeDeChamp
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision_id: ProcedureRevision.where(procedure_id:) })
.order(created_at: :desc)
.find_by(stable_id:)
end
def procedure_id = @column.h_id[:procedure_id]
end

View file

@ -15,16 +15,13 @@ module Instructeurs
end
def refresh_column_filter
procedure_presentation = @procedure_presentation
statut = params[:statut]
current_filter = procedure_presentation.filters_name_for(statut)
# According to the html, the selected column is the last one
h_id = JSON.parse(params[current_filter].last[:id], symbolize_names: true)
column = procedure.find_column(h_id:)
prefix = params[:prefix]
key = prefix.gsub('[]', '')
column = ColumnType.new.cast(params[key].last['id'])
filter_component = Instructeurs::ColumnFilterComponent.new(procedure:, procedure_presentation:, statut:, column:)
component = Instructeurs::ColumnFilterValueComponent.new(column:, prefix:)
render turbo_stream: turbo_stream.replace('filter-component', filter_component)
render turbo_stream: turbo_stream.replace('value', component)
end
private
@ -32,6 +29,9 @@ module Instructeurs
def procedure = @procedure_presentation.procedure
def procedure_presentation_params
# TODO: peut etre simplifier en transformer un parametre filter -> tous_filter, suivant le params statut
filters = [
:tous_filters, :a_suivre_filters, :suivis_filters, :traites_filters,
:expirant_filters, :archives_filters, :supprimes_filters

View file

@ -30,27 +30,4 @@ describe Instructeurs::ColumnFilterComponent, type: :component do
it { is_expected.to eq([[filterable_column.label, filterable_column.id]]) }
end
describe '.options_for_select_of_column' do
subject { component.options_for_select_of_column }
context "column is groupe_instructeur" do
let(:column) { double("Column", 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 column 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(:column) { Column.new(procedure_id:, 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

@ -0,0 +1,35 @@
# frozen_string_literal: true
describe Instructeurs::ColumnFilterValueComponent, type: :component do
let(:component) { described_class.new(column:) }
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, instructeurs: [instructeur]) }
let(:procedure_id) { procedure.id }
before do
allow(component).to receive(:current_instructeur).and_return(instructeur)
end
describe '.options_for_select_of_column' do
subject { component.send(:options_for_select_of_column) }
context "column is groupe_instructeur" do
let(:column) { double("Column", scope: nil, table: 'groupe_instructeur', h_id: { procedure_id: }) }
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 column 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(:column) { Column.new(procedure_id:, 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