Module: ActiveModel

Defined in:
lib/active_model/credit_card_number_validator.rb

Overview

ActiveModel Validations CreditCardNumberValidator

Can  be used in combination with the +validates+ method

 Only Amex and Maestro

 class CreditCard
   attr_accessor :number
   include ActiveModel::Validations
   validates :number, credit_card_number: {only: [:amex, :maestro]}
 end

 All numbers are valid except Maestro

class CreditCard
   attr_accessor :number
   include ActiveModel::Validations
   validates :number, credit_card_number: {except: [:maestro]}
end

Proc can be used as well

class CreditCard
   attr_accessor :number, :card_type
   include ActiveModel::Validations
   validates :number, credit_card_number: {brands: ->{|record|  Array(record.accepted_brands) }  }

   def accepted_brands
     if card_type == 'Maestro'
       :maestro
     elsif card_type == 'American Express'
       :amex
     else
       :visa
     end
   end

end

Defined Under Namespace

Modules: Validations