Merge pull request #8652 from demarches-simplifiees/US/fix-departements-filter

correctif(procedure#filtre): passe a l'usage de nos filtres par enum pour la recherche par departement
This commit is contained in:
mfo 2023-02-20 11:54:16 +01:00 committed by GitHub
commit fec7c1d6f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 6 deletions

View file

@ -53,6 +53,8 @@ class Champ < ApplicationRecord
:repetition?,
:block?,
:dossier_link?,
:departement?,
:region?,
:titre_identite?,
:header_section?,
:simple_drop_down_list?,

View file

@ -89,8 +89,8 @@ class ProcedurePresentation < ApplicationRecord
end
fields.concat procedure.types_de_champ_for_procedure_presentation
.pluck(:libelle, :private, :stable_id)
.map { |(libelle, is_private, stable_id)| field_hash(is_private ? TYPE_DE_CHAMP_PRIVATE : TYPE_DE_CHAMP, stable_id.to_s, label: libelle, type: :text) }
.pluck(:type_champ, :libelle, :private, :stable_id)
.map { |(type_champ, libelle, is_private, stable_id)| field_hash(is_private ? TYPE_DE_CHAMP_PRIVATE : TYPE_DE_CHAMP, stable_id.to_s, label: libelle, type: (TypeDeChamp.options_for_select?(type_champ) ? :enum : :text)) }
fields
end
@ -335,6 +335,8 @@ class ProcedurePresentation < ApplicationRecord
[_1.label, _1.id]
end
end
else
TypeDeChamp.find(field['column']).options_for_select
end
end

View file

@ -369,6 +369,14 @@ class TypeDeChamp < ApplicationRecord
type_champ == TypeDeChamp.type_champs.fetch(:pole_emploi)
end
def departement?
type_champ == TypeDeChamp.type_champs.fetch(:departements)
end
def region?
type_champ == TypeDeChamp.type_champs.fetch(:regions)
end
def mesri?
type_champ == TypeDeChamp.type_champs.fetch(:mesri)
end
@ -405,6 +413,21 @@ class TypeDeChamp < ApplicationRecord
self.drop_down_options = parse_drop_down_list_value(value)
end
def self.options_for_select?(type_champs)
[
TypeDeChamp.type_champs.fetch(:departements),
TypeDeChamp.type_champs.fetch(:regions)
].include?(type_champs)
end
def options_for_select
if departement?
APIGeoService.departements.map { ["#{_1[:code]} #{_1[:name]}", _1[:name]] }
elsif region?
APIGeoService.regions.map { [_1[:name], _1[:name]] }
end
end
# historicaly we added a blank ("") option by default to avoid wrong selection
# see self.parse_drop_down_list_value
# then rails decided to add this blank ("") option when the select is required

View file

@ -0,0 +1,13 @@
class RemoveChampsExternalIdIndex < ActiveRecord::Migration[6.1]
include Database::MigrationHelpers
disable_ddl_transaction!
def up
remove_index :champs, column: :external_id
end
def down
add_concurrent_index :champs, :external_id
end
end

View file

@ -227,13 +227,12 @@ ActiveRecord::Schema.define(version: 2023_02_16_141558) do
t.datetime "rebased_at"
t.string "row_id"
t.string "type"
t.integer "type_de_champ_id", null: false
t.integer "type_de_champ_id"
t.datetime "updated_at"
t.string "value"
t.jsonb "value_json"
t.index ["dossier_id"], name: "index_champs_on_dossier_id"
t.index ["etablissement_id"], name: "index_champs_on_etablissement_id"
t.index ["external_id"], name: "index_champs_on_external_id"
t.index ["parent_id"], name: "index_champs_on_parent_id"
t.index ["private"], name: "index_champs_on_private"
t.index ["row_id"], name: "index_champs_on_row_id"

View file

@ -122,7 +122,9 @@ FactoryBot.define do
factory :champ_communes, class: 'Champs::CommuneChamp' do
type_de_champ { association :type_de_champ_communes, procedure: dossier.procedure }
value { 'Paris' }
value { 'Coye-la-Forêt (60580)' }
value_json { { "departement" => "Oise", "code_departement" => "60" } }
external_id { "60172" }
end
factory :champ_epci, class: 'Champs::EpciChamp' do

View file

@ -206,6 +206,12 @@ FactoryBot.define do
end
end
trait :with_region do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_regions, procedure: procedure)
end
end
trait :with_piece_justificative do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_piece_justificative, procedure: procedure)

View file

@ -1,6 +1,6 @@
describe "procedure filters" do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, :with_type_de_champ, instructeurs: [instructeur]) }
let(:procedure) { create(:procedure, :published, :with_type_de_champ, :with_departement, :with_region, instructeurs: [instructeur]) }
let!(:type_de_champ) { procedure.active_revision.types_de_champ_public.first }
let!(:new_unfollow_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_instruction)) }
let!(:champ) { Champ.find_by(type_de_champ_id: type_de_champ.id, dossier_id: new_unfollow_dossier.id) }
@ -94,6 +94,44 @@ describe "procedure filters" do
click_button "Ajouter le filtre"
end
describe 'with a vcr cached cassette' do
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
end
scenario "should be able to find by departements with custom enum lookup", js: true, vcr: { cassette_name: 'api_geo_departements' } do
departement_champ = new_unfollow_dossier.champs.find(&:departement?)
departement_champ.update!(value: 'Oise', external_id: '60')
departement_champ.reload
champ_select_value = "#{departement_champ.external_id} #{departement_champ.value}"
click_on 'Sélectionner un filtre'
select departement_champ.libelle, from: "Colonne"
find("select#value", visible: true)
select champ_select_value, from: "Valeur"
click_button "Ajouter le filtre"
find("select#value", visible: false) # w8 for filter to be applied
expect(page).to have_link(new_unfollow_dossier.id.to_s)
end
scenario "should be able to find by region with custom enum lookup", js: true, vcr: { cassette_name: 'api_geo_regions' } do
region_champ = new_unfollow_dossier.champs.find(&:region?)
region_champ.update!(value: 'Bretagne', external_id: '53')
region_champ.reload
click_on 'Sélectionner un filtre'
select region_champ.libelle, from: "Colonne"
find("select#value", visible: true)
select region_champ.value, from: "Valeur"
click_button "Ajouter le filtre"
find("select#value", visible: false) # w8 for filter to be applied
expect(page).to have_link(new_unfollow_dossier.id.to_s)
end
end
scenario "should be able to add and remove two filters for the same field", js: true do
add_filter(type_de_champ.libelle, champ.value)
add_filter(type_de_champ.libelle, champ_2.value)