Class: ActiveModel::Validations::CreditCardNumberValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/credit_card_support/validators/credit_card_number_validator.rb

Instance Method Summary collapse

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_allowedObject



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 options[:allow_issuers] && !options[:allow_issuers].include?(@value.issuer)
    @record.errors.add(@attribute, t(:issuer_not_supported, issuer: @value.issuer))
    false
  else
    true
  end
end

#validates_luhnObject



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_testcardObject



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 !options[:allow_testcards] && @value.testcard?
    @record.errors.add(@attribute, t(:testcard_not_supported))
    false
  else
    true
  end
end