Class: ValidatesZipcode::Validator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ValidatesZipcode::Validator
- Defined in:
- lib/validates_zipcode/validator.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ Validator
constructor
A new instance of Validator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ Validator
Returns a new instance of Validator.
20 21 22 23 24 25 26 |
# File 'lib/validates_zipcode/validator.rb', line 20 def initialize() @country_code = .fetch(:country_code, nil) @country_code_attribute = .fetch(:country_code_attribute, :country_alpha2) @excluded_country_codes = .fetch(:excluded_country_codes, []) @message = .fetch(:message, nil) super end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/validates_zipcode/validator.rb', line 28 def validate_each(record, attribute, value) alpha2 = @country_code || record.send(@country_code_attribute) = { zipcode: value.to_s, country_alpha2: alpha2, excluded_country_codes: @excluded_country_codes } unless ValidatesZipcode::Zipcode.new().valid? = @message || I18n.t('errors.messages.invalid_zipcode', value: value, default: 'Zipcode is invalid') record.errors.add(attribute, :invalid_zipcode, message: ) end end |