From d6b85a038c36e3b4139364b8cc3a51de6fc5a436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 27 Nov 2021 18:48:42 +0100 Subject: [PATCH] Allow regular expressions in postal codes Programmers can take advantage of this feature when defining custom default settings. And, since many CONSUL installations had custom changes in the `custom/verification/residence.rb` model and those changes might use regular expressions, we're making it easier to migrate that code to the new system to define valid postal codes. We aren't documenting this feature in the description in the admin section because most administrators don't know what regular expressions are. Note that, in order to simplify the setting, we already define the `/\A` and `\Z/` characters. So, if the custom code had something like `postal_code =~ /^280/`, the setting would have to be "280*" (without the quotes) or, in order to comply with a length validation, "280[0-9]{2}" (without the quotes). --- app/models/verification/residence.rb | 2 +- spec/models/verification/residence_spec.rb | 52 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index d782fbb6f..539c3ef12 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -110,7 +110,7 @@ class Verification::Residence if code_or_range.include?(":") Range.new(*code_or_range.split(":").map(&:strip)).include?(postal_code&.strip) else - postal_code&.strip == code_or_range.strip + /\A#{code_or_range.strip}\Z/.match?(postal_code&.strip) end end end diff --git a/spec/models/verification/residence_spec.rb b/spec/models/verification/residence_spec.rb index a33f8aaba..e9733c7d7 100644 --- a/spec/models/verification/residence_spec.rb +++ b/spec/models/verification/residence_spec.rb @@ -126,6 +126,58 @@ describe Verification::Residence do expect(residence).to be_valid end + it "allows regular expressions" do + Setting["postal_codes"] = "007,[A-Za-z]{2}-[0-9]{3},86" + + residence.postal_code = "007" + expect(residence).to be_valid + + residence.postal_code = "86" + expect(residence).to be_valid + + residence.postal_code = "AB-123" + expect(residence).to be_valid + + residence.postal_code = "zz-789" + expect(residence).to be_valid + + residence.postal_code = "006" + expect(residence).not_to be_valid + + residence.postal_code = "87" + expect(residence).not_to be_valid + + residence.postal_code = "AB-12" + expect(residence).not_to be_valid + + residence.postal_code = "AB-1234" + expect(residence).not_to be_valid + + residence.postal_code = "A-123" + expect(residence).not_to be_valid + + residence.postal_code = "ABC-123" + expect(residence).not_to be_valid + + residence.postal_code = "ABC-12" + expect(residence).not_to be_valid + + residence.postal_code = "AB-A12" + expect(residence).not_to be_valid + + residence.postal_code = "12A-12" + expect(residence).not_to be_valid + + residence.postal_code = "123-12" + expect(residence).not_to be_valid + + residence.postal_code = "ABC-A1" + expect(residence).not_to be_valid + + residence.postal_code = "AB-123\n123" + expect(residence).not_to be_valid + end + it "is not valid with postal codes not included in settings" do residence.postal_code = "12345" expect(residence).not_to be_valid