Browse Source

extracted regex into initializer

pull/721/head
JG 3 years ago
parent
commit
4c7d96099e
  1. 3
      app/models/location.rb
  2. 1
      app/models/validations/property_validations.rb
  3. 1
      config/initializers/postcode_regex.rb

3
app/models/location.rb

@ -1,5 +1,4 @@
class Location < ApplicationRecord
include Validations::PropertyValidations
validate :validate_postcode
belongs_to :scheme
@ -36,7 +35,7 @@ class Location < ApplicationRecord
private
def validate_postcode
if postcode.nil? || !postcode&.match(Validations::PropertyValidations::POSTCODE_REGEXP)
if postcode.nil? || !postcode&.match(POSTCODE_REGEXP)
error_message = I18n.t("validations.postcode")
errors.add :postcode, error_message
end

1
app/models/validations/property_validations.rb

@ -1,7 +1,6 @@
module Validations::PropertyValidations
# Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
POSTCODE_REGEXP = /^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/i
def validate_property_number_of_times_relet(record)
return unless record.offered

1
config/initializers/postcode_regex.rb

@ -0,0 +1 @@
POSTCODE_REGEXP = /^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/i
Loading…
Cancel
Save