Class: ValidationKit::PostalCodeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validation_kit/postal_code_validator/postal_code_validator.rb

Instance Method Summary collapse

Instance Method Details

#disallowed_characters_for_country(country_code) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/validation_kit/postal_code_validator/postal_code_validator.rb', line 17

def disallowed_characters_for_country(country_code)
  if country_code.blank?
    nil
  elsif ["US", "AU", "NZ"].include?(country_code)
    /[^0-9]/
  elsif ["CA", "UK"].include?(country_code)
    /[^0-9A-Z]/
  else
    nil
  end
end

#format_as_postal_code(arg, country_code, disallowed_characters) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/validation_kit/postal_code_validator/postal_code_validator.rb', line 62

def format_as_postal_code(arg, country_code, disallowed_characters)
  return nil if (arg.blank? or country_code.blank? or !postal_code_regex_for_country(country_code))

  postal_code = arg.gsub(disallowed_characters, '')

  if ["US"].include?(country_code)
    digit_count = postal_code.length
    if digit_count == 5
      return postal_code
    elsif digit_count == 9
      return "%s-%s" % [postal_code[0..4], postal_code[5..8]]
    else
      return nil
    end

  elsif ["AU", "NZ"].include?(country_code)
    postal_code

  elsif ["CA"].include?(country_code)
    fsa = postal_code[0..2]
    lda = postal_code[3..5]

    postal_code = "%s %s" % [fsa, lda]
    postal_code.upcase
  end
end

#postal_code_regex_for_country(country_code) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/validation_kit/postal_code_validator/postal_code_validator.rb', line 3

def postal_code_regex_for_country(country_code)
  if country_code.blank?
    nil
  elsif ["AU", "NZ"].include?(country_code)
    /\d{4}/
  elsif ["US"].include?(country_code)
    /\d{5}(-\d{4})?/
  elsif ["CA"].include?(country_code)
    /[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d/
  else
    nil
  end
end

#validate_each(record, attribute, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/validation_kit/postal_code_validator/postal_code_validator.rb', line 29

def validate_each(record, attribute, value)
  if options[:country].is_a?(String)
    country = options[:country]
  elsif options[:country].is_a?(Symbol) and record.respond_to?(options[:country])
    country = record.send(options[:country])
  elsif record.respond_to?(:country)
    country = record.send(:country)
  else
    country = false
  end

  return unless country
  current_regex = postal_code_regex_for_country(country)
  return unless current_regex
  disallowed_characters = disallowed_characters_for_country(country)

  new_value = value.nil? ? "" : value.upcase.gsub(disallowed_characters, '')

  model_name = record.class.to_s

  unless (options[:allow_blank] && new_value.blank?) || new_value =~ current_regex
    message = I18n.t("activerecord.errors.models.#{model_name.underscore}.attributes.#{attribute}.invalid",
                     :default => [:"activerecord.errors.models.#{model_name.underscore}.invalid",
                                  options[:message],
                                  :'activerecord.errors.messages.invalid'])
    record.errors[attribute].add message
  else
    record.send(attribute.to_s + '=',
      format_as_postal_code(new_value, country, disallowed_characters)
    ) if options[:set]
  end
end