add procedure_id to column.id

This commit is contained in:
simon lehericey 2024-10-07 21:46:59 +02:00
parent 3c7521a428
commit 3740a79219
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
14 changed files with 65 additions and 49 deletions

View file

@ -12,7 +12,7 @@ class Instructeurs::ColumnPickerComponent < ApplicationComponent
def displayable_columns_for_select
[
procedure.columns.filter(&:displayable).map { |column| [column.label, column.id] },
procedure_presentation.displayed_fields.map { Column.new(**_1.deep_symbolize_keys).id }
procedure_presentation.displayed_fields.map { Column.new(**_1.deep_symbolize_keys.merge(procedure_id: procedure.id)).id }
]
end
end

View file

@ -5,7 +5,8 @@ class Column
attr_reader :table, :column, :label, :classname, :type, :scope, :value_column, :filterable, :displayable
def initialize(table:, column:, label: nil, type: :text, value_column: :value, filterable: true, displayable: true, classname: '', scope: '')
def initialize(procedure_id:, table:, column:, label: nil, type: :text, value_column: :value, filterable: true, displayable: true, classname: '', scope: '')
@procedure_id = procedure_id
@table = table
@column = column
@label = label || I18n.t(column, scope: [:activerecord, :attributes, :procedure_presentation, :fields, table])

View file

@ -4,7 +4,7 @@ module AddressableColumnConcern
extend ActiveSupport::Concern
included do
def columns(displayable: true, prefix: nil)
def columns(procedure_id:, displayable: true, prefix: nil)
super.concat([
["code postal (5 chiffres)", ['postal_code'], :text],
["commune", ['city_name'], :text],
@ -12,6 +12,7 @@ module AddressableColumnConcern
["region", ['region_name'], :enum]
].map do |(label, value_column, type)|
Columns::JSONPathColumn.new(
procedure_id:,
table: Column::TYPE_DE_CHAMP_TABLE,
column: stable_id,
label: "#{libelle_with_prefix(prefix)} #{label}",

View file

@ -15,23 +15,23 @@ module ColumnsConcern
end
def dossier_id_column
Column.new(table: 'self', column: 'id', classname: 'number-col', type: :number)
Column.new(procedure_id: id, table: 'self', column: 'id', classname: 'number-col', type: :number)
end
def notifications_column
Column.new(table: 'notifications', column: 'notifications', label: "notifications", filterable: false)
Column.new(procedure_id: id, table: 'notifications', column: 'notifications', label: "notifications", filterable: false)
end
def dossier_columns
common = [dossier_id_column, notifications_column]
dates = ['created_at', 'updated_at', 'depose_at', 'en_construction_at', 'en_instruction_at', 'processed_at']
.map { |column| Column.new(table: 'self', column:, type: :date) }
.map { |column| Column.new(procedure_id: id, table: 'self', column:, type: :date) }
non_displayable_dates = ['updated_since', 'depose_since', 'en_construction_since', 'en_instruction_since', 'processed_since']
.map { |column| Column.new(table: 'self', column:, type: :date, displayable: false) }
.map { |column| Column.new(procedure_id: id, table: 'self', column:, type: :date, displayable: false) }
states = [Column.new(table: 'self', column: 'state', type: :enum, scope: 'instructeurs.dossiers.filterable_state', displayable: false)]
states = [Column.new(procedure_id: id, table: 'self', column: 'state', type: :enum, scope: 'instructeurs.dossiers.filterable_state', displayable: false)]
[common, dates, sva_svr_columns(for_filters: true), non_displayable_dates, states].flatten.compact
end
@ -42,12 +42,12 @@ module ColumnsConcern
scope = [:activerecord, :attributes, :procedure_presentation, :fields, :self]
columns = [
Column.new(table: 'self', column: 'sva_svr_decision_on', type: :date,
Column.new(procedure_id: id, table: 'self', column: 'sva_svr_decision_on', type: :date,
label: I18n.t("#{sva_svr_decision}_decision_on", scope:), classname: for_filters ? '' : 'sva-col')
]
if for_filters
columns << Column.new(table: 'self', column: 'sva_svr_decision_before', type: :date, displayable: false,
columns << Column.new(procedure_id: id, table: 'self', column: 'sva_svr_decision_before', type: :date, displayable: false,
label: I18n.t("#{sva_svr_decision}_decision_before", scope:))
end
@ -58,30 +58,30 @@ module ColumnsConcern
def standard_columns
[
Column.new(table: 'user', column: 'email'),
Column.new(table: 'followers_instructeurs', column: 'email'),
Column.new(table: 'groupe_instructeur', column: 'id', type: :enum),
Column.new(table: 'avis', column: 'question_answer', filterable: false) # not filterable ?
Column.new(procedure_id: id, table: 'user', column: 'email'),
Column.new(procedure_id: id, table: 'followers_instructeurs', column: 'email'),
Column.new(procedure_id: id, table: 'groupe_instructeur', column: 'id', type: :enum),
Column.new(procedure_id: id, table: 'avis', column: 'question_answer', filterable: false) # not filterable ?
]
end
def individual_columns
['nom', 'prenom', 'gender'].map { |column| Column.new(table: 'individual', column:) }
['nom', 'prenom', 'gender'].map { |column| Column.new(procedure_id: id, table: 'individual', column:) }
end
def moral_columns
etablissements = ['entreprise_siren', 'entreprise_forme_juridique', 'entreprise_nom_commercial', 'entreprise_raison_sociale', 'entreprise_siret_siege_social']
.map { |column| Column.new(table: 'etablissement', column:) }
.map { |column| Column.new(procedure_id: id, table: 'etablissement', column:) }
etablissement_dates = ['entreprise_date_creation'].map { |column| Column.new(table: 'etablissement', column:, type: :date) }
etablissement_dates = ['entreprise_date_creation'].map { |column| Column.new(procedure_id: id, table: 'etablissement', column:, type: :date) }
other = ['siret', 'libelle_naf', 'code_postal'].map { |column| Column.new(table: 'etablissement', column:) }
other = ['siret', 'libelle_naf', 'code_postal'].map { |column| Column.new(procedure_id: id, table: 'etablissement', column:) }
[etablissements, etablissement_dates, other].flatten
end
def types_de_champ_columns
all_revisions_types_de_champ.flat_map(&:columns)
all_revisions_types_de_champ.flat_map { _1.columns(procedure_id: id) }
end
end
end

View file

@ -31,9 +31,9 @@ class ProcedurePresentation < ApplicationRecord
def displayed_fields_for_headers
[
Column.new(table: 'self', column: 'id', classname: 'number-col'),
*displayed_fields.map { Column.new(**_1.deep_symbolize_keys) },
Column.new(table: 'self', column: 'state', classname: 'state-col'),
Column.new(procedure_id: procedure.id, table: 'self', column: 'id', classname: 'number-col'),
*displayed_fields.map { Column.new(**_1.deep_symbolize_keys.merge(procedure_id: procedure.id)) },
Column.new(procedure_id: procedure.id, table: 'self', column: 'state', classname: 'state-col'),
*procedure.sva_svr_columns
]
end
@ -201,7 +201,7 @@ class ProcedurePresentation < ApplicationRecord
.map do |(table, column), filters|
values = filters.pluck('value')
value_column = filters.pluck('value_column').compact.first || :value
dossier_column = procedure.find_column(id: Column.make_id(table, column)) # hack to find json path columns
dossier_column = procedure.find_column(id: Column.make_id(procedure.id, table, column)) # hack to find json path columns
if dossier_column.is_a?(Columns::JSONPathColumn)
dossier_column.filtered_ids(dossiers, values)
else

View file

@ -27,9 +27,9 @@ class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase
ActiveStorage::Filename.new(str.delete('[]*?')).sanitized
end
def columns(displayable: true, prefix: nil)
def columns(procedure_id:, displayable: true, prefix: nil)
@type_de_champ.procedure
.all_revisions_types_de_champ(parent: @type_de_champ)
.flat_map { _1.columns(displayable: false, prefix: libelle) }
.flat_map { _1.columns(procedure_id:, displayable: false, prefix: libelle) }
end
end

View file

@ -98,9 +98,10 @@ class TypesDeChamp::TypeDeChampBase
end
end
def columns(displayable: true, prefix: nil)
def columns(procedure_id:, displayable: true, prefix: nil)
[
Column.new(
procedure_id:,
table: Column::TYPE_DE_CHAMP_TABLE,
column: stable_id.to_s,
label: libelle_with_prefix(prefix),

View file

@ -6,6 +6,6 @@
- filters.each_with_index do |filter, i|
- if i > 0
= " ou "
= link_to remove_filter_instructeur_procedure_path(procedure, { statut: statut, column: "#{filter['table']}/#{filter['column']}", value: filter['value'] }),
= link_to remove_filter_instructeur_procedure_path(procedure, { statut: statut, column: Column.make_id(procedure.id, filter['table'], filter['column']), value: filter['value'] }),
class: "fr-tag fr-tag--dismiss fr-my-1w", aria: { label: "Retirer le filtre #{filter['column']}" } do
= "#{filter['label'].truncate(50)} : #{procedure_presentation.human_value_for_filter(filter)}"

View file

@ -5,6 +5,7 @@ describe Instructeurs::ColumnFilterComponent, type: :component do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, instructeurs: [instructeur]) }
let(:procedure_id) { procedure.id }
let(:procedure_presentation) { nil }
let(:statut) { nil }
@ -17,8 +18,8 @@ describe Instructeurs::ColumnFilterComponent, type: :component do
let(:column) { nil }
let(:included_displayable_field) do
[
Column.new(label: 'email', table: 'user', column: 'email'),
Column.new(label: "depose_since", table: "self", column: "depose_since", displayable: false)
Column.new(procedure_id:, label: 'email', table: 'user', column: 'email'),
Column.new(procedure_id:, label: "depose_since", table: "self", column: "depose_since", displayable: false)
]
end
@ -26,7 +27,7 @@ describe Instructeurs::ColumnFilterComponent, type: :component do
subject { component.filterable_columns_options }
it { is_expected.to eq([["email", "user/email"], ["depose_since", "self/depose_since"]]) }
it { is_expected.to eq([["email", Column.make_id(procedure_id, "user", "email")], ["depose_since", Column.make_id(procedure_id, "self", "depose_since")]]) }
end
end
@ -45,7 +46,7 @@ describe Instructeurs::ColumnFilterComponent, type: :component 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(table: 'type_de_champ', scope: nil, column: drop_down_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'])

View file

@ -4,13 +4,15 @@ describe Instructeurs::ColumnPickerComponent, type: :component do
let(:component) { described_class.new(procedure:, procedure_presentation:) }
let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:instructeur) { create(:instructeur) }
let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: instructeur) }
let(:procedure_presentation) { create(:procedure_presentation, assign_to: assign_to) }
describe "#displayable_columns_for_select" do
let(:default_user_email) { Column.new(label: 'email', table: 'user', column: 'email') }
let(:excluded_displayable_field) { Column.new(label: "label1", table: "table1", column: "column1", displayable: false) }
let(:default_user_email) { Column.new(procedure_id:, label: 'email', table: 'user', column: 'email') }
let(:excluded_displayable_field) { Column.new(procedure_id:, label: "label1", table: "table1", column: "column1", displayable: false) }
let(:email_column_id) { Column.make_id(procedure_id, 'user', 'email') }
subject { component.displayable_columns_for_select }
@ -21,6 +23,6 @@ describe Instructeurs::ColumnPickerComponent, type: :component do
])
end
it { is_expected.to eq([[["email", "user/email"]], ["user/email"]]) }
it { is_expected.to eq([[["email", email_column_id]], [email_column_id]]) }
end
end

View file

@ -886,12 +886,14 @@ describe Instructeurs::ProceduresController, type: :controller do
end
it 'can change order' do
expect { get :update_sort, params: { procedure_id: procedure.id, column_id: "individual/nom", order: 'asc' } }
column_id = Column.make_id(procedure.id, "individual", "nom")
expect { get :update_sort, params: { procedure_id: procedure.id, column_id:, order: 'asc' } }
.to change { procedure_presentation.sort }
.from({ "column" => "notifications", "order" => "desc", "table" => "notifications" })
.to({ "column" => "nom", "order" => "asc", "table" => "individual" })
end
end
describe '#add_filter' do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :for_individual) }
@ -903,7 +905,8 @@ describe Instructeurs::ProceduresController, type: :controller do
end
subject do
post :add_filter, params: { procedure_id: procedure.id, column: "individual/nom", value: "n" * 110, statut: "a-suivre" }
column = Column.make_id(procedure.id, "individual", "nom")
post :add_filter, params: { procedure_id: procedure.id, column:, value: "n" * 110, statut: "a-suivre" }
end
it 'should render the error' do

View file

@ -6,6 +6,7 @@ describe ColumnsConcern do
context 'when the procedure can have a SIRET number' do
let(:procedure) { create(:procedure, types_de_champ_public:, types_de_champ_private:) }
let(:procedure_id) { procedure.id }
let(:tdc_1) { procedure.active_revision.types_de_champ_public[0] }
let(:tdc_2) { procedure.active_revision.types_de_champ_public[1] }
let(:tdc_private_1) { procedure.active_revision.types_de_champ_private[0] }
@ -43,7 +44,7 @@ describe ColumnsConcern do
{ label: tdc_2.libelle, table: 'type_de_champ', column: tdc_2.stable_id.to_s, classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true },
{ label: tdc_private_1.libelle, table: 'type_de_champ', column: tdc_private_1.stable_id.to_s, classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true },
{ label: tdc_private_2.libelle, table: 'type_de_champ', column: tdc_private_2.stable_id.to_s, classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true }
].map { Column.new(**_1) }
].map { Column.new(**_1.merge(procedure_id:)) }
}
context 'with explication/header_sections' do
@ -67,10 +68,11 @@ describe ColumnsConcern do
end
context 'when the procedure is for individuals' do
let(:name_field) { Column.new(label: "Prénom", table: "individual", column: "prenom", classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true) }
let(:surname_field) { Column.new(label: "Nom", table: "individual", column: "nom", classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true) }
let(:gender_field) { Column.new(label: "Civilité", table: "individual", column: "gender", classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true) }
let(:name_field) { Column.new(procedure_id:, label: "Prénom", table: "individual", column: "prenom", classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true) }
let(:surname_field) { Column.new(procedure_id:, label: "Nom", table: "individual", column: "nom", classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true) }
let(:gender_field) { Column.new(procedure_id:, label: "Civilité", table: "individual", column: "gender", classname: '', displayable: true, type: :text, scope: '', value_column: :value, filterable: true) }
let(:procedure) { create(:procedure, :for_individual) }
let(:procedure_id) { procedure.id }
let(:procedure_presentation) { create(:procedure_presentation, assign_to: assign_to) }
it { is_expected.to include(name_field, surname_field, gender_field) }
@ -78,20 +80,22 @@ describe ColumnsConcern do
context 'when the procedure is sva' do
let(:procedure) { create(:procedure, :for_individual, :sva) }
let(:procedure_id) { procedure.id }
let(:procedure_presentation) { create(:procedure_presentation, assign_to: assign_to) }
let(:decision_on) { Column.new(label: "Date décision SVA", table: "self", column: "sva_svr_decision_on", classname: '', displayable: true, type: :date, scope: '', value_column: :value, filterable: true) }
let(:decision_before_field) { Column.new(label: "Date décision SVA avant", table: "self", column: "sva_svr_decision_before", classname: '', displayable: false, type: :date, scope: '', value_column: :value, filterable: true) }
let(:decision_on) { Column.new(procedure_id:, label: "Date décision SVA", table: "self", column: "sva_svr_decision_on", classname: '', displayable: true, type: :date, scope: '', value_column: :value, filterable: true) }
let(:decision_before_field) { Column.new(procedure_id:, label: "Date décision SVA avant", table: "self", column: "sva_svr_decision_before", classname: '', displayable: false, type: :date, scope: '', value_column: :value, filterable: true) }
it { is_expected.to include(decision_on, decision_before_field) }
end
context 'when the procedure is svr' do
let(:procedure) { create(:procedure, :for_individual, :svr) }
let(:procedure_id) { procedure.id }
let(:procedure_presentation) { create(:procedure_presentation, assign_to: assign_to) }
let(:decision_on) { Column.new(label: "Date décision SVR", table: "self", column: "sva_svr_decision_on", classname: '', displayable: true, type: :date, scope: '', value_column: :value, filterable: true) }
let(:decision_before_field) { Column.new(label: "Date décision SVR avant", table: "self", column: "sva_svr_decision_before", classname: '', displayable: false, type: :date, scope: '', value_column: :value, filterable: true) }
let(:decision_on) { Column.new(procedure_id:, label: "Date décision SVR", table: "self", column: "sva_svr_decision_on", classname: '', displayable: true, type: :date, scope: '', value_column: :value, filterable: true) }
let(:decision_before_field) { Column.new(procedure_id:, label: "Date décision SVR avant", table: "self", column: "sva_svr_decision_before", classname: '', displayable: false, type: :date, scope: '', value_column: :value, filterable: true) }
it { is_expected.to include(decision_on, decision_before_field) }
end

View file

@ -94,7 +94,7 @@ RSpec.describe Export, type: :model do
let(:instructeur) { create(:instructeur) }
let!(:gi_1) { create(:groupe_instructeur, procedure: procedure, instructeurs: [instructeur]) }
let!(:pp) { gi_1.instructeurs.first.procedure_presentation_and_errors_for_procedure_id(procedure.id).first }
before { pp.add_filter('tous', 'self/created_at', '10/12/2021') }
before { pp.add_filter('tous', Column.make_id(procedure.id, 'self', 'created_at'), '10/12/2021') }
context 'with procedure_presentation having different filters' do
it 'works once' do
@ -105,7 +105,7 @@ RSpec.describe Export, type: :model do
it 'works once, changes procedure_presentation, recreate a new' do
expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
.to change { Export.count }.by(1)
pp.add_filter('tous', 'self/updated_at', '10/12/2021')
pp.add_filter('tous', Column.make_id(procedure.id, 'self', 'updated_at'), '10/12/2021')
expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
.to change { Export.count }.by(1)
end

View file

@ -582,7 +582,7 @@ describe ProcedurePresentation do
context 'for type_de_champ using AddressableColumnConcern' do
let(:types_de_champ_public) { [{ type: :rna, stable_id: 1 }] }
let(:type_de_champ) { procedure.active_revision.types_de_champ.first }
let(:available_columns) { type_de_champ.columns }
let(:available_columns) { type_de_champ.columns(procedure_id: procedure.id) }
let(:column) { available_columns.find { _1.value_column == value_column_searched } }
let(:filter) { [column.to_json.merge({ "value" => value })] }
let(:kept_dossier) { create(:dossier, procedure: procedure) }
@ -611,6 +611,7 @@ describe ProcedurePresentation do
create(:dossier, procedure: procedure).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "departement_code" => "unknown" })
end
it { is_expected.to contain_exactly(kept_dossier.id) }
it 'describes column' do
expect(column.type).to eq(:enum)
expect(column.options_for_select.first).to eq(["99 Etranger", "99"])
@ -865,9 +866,10 @@ describe ProcedurePresentation do
context 'when type_de_champ text' do
let(:filters) { { "suivis" => [] } }
let(:column_id) { Column.make_id(procedure.id, 'type_de_champ', first_type_de_champ_id) }
it 'should passthrough value' do
procedure_presentation.add_filter("suivis", "type_de_champ/#{first_type_de_champ_id}", "Oui")
procedure_presentation.add_filter("suivis", column_id, "Oui")
expect(procedure_presentation.filters).to eq({
"suivis" => [
@ -879,10 +881,11 @@ describe ProcedurePresentation do
context 'when type_de_champ departements' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :departements }]) }
let(:column_id) { Column.make_id(procedure.id, 'type_de_champ', first_type_de_champ_id) }
let(:filters) { { "suivis" => [] } }
it 'should set value_column' do
procedure_presentation.add_filter("suivis", "type_de_champ/#{first_type_de_champ_id}", "13")
procedure_presentation.add_filter("suivis", column_id, "13")
expect(procedure_presentation.filters).to eq({
"suivis" => [