Class: ActiveModel::Validations::CreditCardNumberValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::CreditCardNumberValidator
- Defined in:
- lib/credit_card_support/validators/credit_card_number_validator.rb
Instance Method Summary collapse
- #t(code, *args) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
- #validates_issuer_allowed ⇒ Object
- #validates_luhn ⇒ Object
- #validates_testcard ⇒ Object
Instance Method Details
#t(code, *args) ⇒ Object
46 47 48 |
# File 'lib/credit_card_support/validators/credit_card_number_validator.rb', line 46 def t(code, *args) I18n.t("credit_card_support.validators.number.messages.#{code}", *args) end |
#validate_each(record, attribute, value) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/credit_card_support/validators/credit_card_number_validator.rb', line 7 def validate_each(record, attribute, value) @record = record @attribute = attribute @value = "#{value}" @value.extend(CreditCardSupport::CreditCardNumber) validates_luhn && validates_testcard && validates_issuer_allowed end |
#validates_issuer_allowed ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/credit_card_support/validators/credit_card_number_validator.rb', line 37 def validates_issuer_allowed if [:allow_issuers] && ![:allow_issuers].include?(@value.issuer) @record.errors.add(@attribute, t(:issuer_not_supported, issuer: @value.issuer)) false else true end end |
#validates_luhn ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/credit_card_support/validators/credit_card_number_validator.rb', line 19 def validates_luhn if !@value.luhn? @record.errors.add(@attribute, t(:luhn_not_valid)) false else true end end |
#validates_testcard ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/credit_card_support/validators/credit_card_number_validator.rb', line 28 def validates_testcard if ![:allow_testcards] && @value.testcard? @record.errors.add(@attribute, t(:testcard_not_supported)) false else true end end |