Sha256: 7b5e29c2245c553955bf9d078ea7e47a299f5c6450deab65ae9d881f5a21e359

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# Use this with
# validates :your_province_field, province: { country: :your_country_field }

class ProvinceValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    provinces = PROV_STATE[object.send(options[:country]).to_sym]
    if !provinces.try(:include?, value)
      object.errors[attribute] << (options[:message] || 'Province/State should be in selected country.')
    end
  end

  COUNTRIES = { canada: 'Canada', usa: 'United States of America', mexico: 'Mexico' }

  PROV_STATE = {
      canada: ['Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland', 'Nova Scotia',
               'Northwest Territories', 'Nunavut', 'Ontario', 'Prince Edward Island', 'Saskatchewan',
               'Quebec', 'Yukon'],
      usa: ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware',
            'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky',
            'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
            'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico',
            'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
            'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
            'Virginia', 'Washington', 'Washington, D.C', 'West Virginia', 'Wisconsin', 'Wyoming']
  }
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ama_validators-0.0.7 lib/ama_validators/province_validator.rb