diff --git a/app/queries/event_procedures/by_patient_query.rb b/app/queries/event_procedures/by_patient_query.rb index c17df3f..00eae98 100644 --- a/app/queries/event_procedures/by_patient_query.rb +++ b/app/queries/event_procedures/by_patient_query.rb @@ -10,7 +10,7 @@ def initialize(patient_name:, relation: EventProcedure) end def call - relation.joins(:patient).where("patients.name ILIKE ?", "%#{patient_name}%") + relation.joins(:patient).where("unaccent(patients.name) ILIKE unaccent(?)", "%#{patient_name}%") end end end diff --git a/db/migrate/20260312000000_enable_unaccent_extension.rb b/db/migrate/20260312000000_enable_unaccent_extension.rb new file mode 100644 index 0000000..80c588a --- /dev/null +++ b/db/migrate/20260312000000_enable_unaccent_extension.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class EnableUnaccentExtension < ActiveRecord::Migration[7.1] + def change + enable_extension "unaccent" + end +end diff --git a/db/schema.rb b/db/schema.rb index bfc5525..d556783 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_12_20_153000) do +ActiveRecord::Schema[7.1].define(version: 2026_03_12_000000) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "plpgsql" + enable_extension "unaccent" create_table "cbhpm_procedures", force: :cascade do |t| t.bigint "cbhpm_id", null: false diff --git a/spec/queries/event_procedures/by_patient_query_spec.rb b/spec/queries/event_procedures/by_patient_query_spec.rb index 029fce6..96bbf74 100644 --- a/spec/queries/event_procedures/by_patient_query_spec.rb +++ b/spec/queries/event_procedures/by_patient_query_spec.rb @@ -26,6 +26,16 @@ expect(query).to contain_exactly(ep) end + + it "performs accent-insensitive matching" do + user = create(:user) + patient = create(:patient, name: "João Silva", user: user) + ep = create(:event_procedure, user: user, patient: patient) + + query = described_class.call(patient_name: "Joao") + + expect(query).to contain_exactly(ep) + end end context "when patient name does not match any patient" do