credit_card_support 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
| @@ -1,48 +1,52 @@ | |
| 1 1 | 
             
            I18n.load_path << File.dirname(__FILE__) + '/../locale/en.yml'
         | 
| 2 2 |  | 
| 3 | 
            -
             | 
| 3 | 
            +
            module ActiveModel
         | 
| 4 | 
            +
              module Validations
         | 
| 5 | 
            +
                class CreditCardNumberValidator < ActiveModel::EachValidator
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  def validate_each(record, attribute, value)
         | 
| 8 | 
            +
                    @record     = record
         | 
| 9 | 
            +
                    @attribute  = attribute
         | 
| 10 | 
            +
                    @value      = value
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                    @value.extend(CreditCardSupport::CreditCardNumber)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    validates_luhn &&
         | 
| 15 | 
            +
                    validates_testcard &&
         | 
| 16 | 
            +
                    validates_issuer_allowed
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  def validates_luhn
         | 
| 20 | 
            +
                    if !@value.luhn?
         | 
| 21 | 
            +
                      @record.errors.add(@attribute, t(:luhn_not_valid))
         | 
| 22 | 
            +
                      false
         | 
| 23 | 
            +
                    else
         | 
| 24 | 
            +
                      true
         | 
| 25 | 
            +
                    end
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def validates_testcard
         | 
| 29 | 
            +
                    if !options[:allow_testcards] && @value.testcard?
         | 
| 30 | 
            +
                      @record.errors.add(@attribute, t(:testcard_not_supported))
         | 
| 31 | 
            +
                      false
         | 
| 32 | 
            +
                    else
         | 
| 33 | 
            +
                      true
         | 
| 34 | 
            +
                    end
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  def validates_issuer_allowed
         | 
| 38 | 
            +
                    if options[:allow_issuers] && !options[:allow_issuers].include?(@value.issuer)
         | 
| 39 | 
            +
                      @record.errors.add(@attribute, t(:issuer_not_supported, issuer: @value.issuer))
         | 
| 40 | 
            +
                      false
         | 
| 41 | 
            +
                    else
         | 
| 42 | 
            +
                      true
         | 
| 43 | 
            +
                    end
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  def t(code, *args)
         | 
| 47 | 
            +
                     I18n.t("credit_card_support.validators.number.messages.#{code}", *args)
         | 
| 48 | 
            +
                  end
         | 
| 4 49 |  | 
| 5 | 
            -
              def validate_each(record, attribute, value)
         | 
| 6 | 
            -
                @record     = record
         | 
| 7 | 
            -
                @attribute  = attribute
         | 
| 8 | 
            -
                @value      = value
         | 
| 9 | 
            -
             | 
| 10 | 
            -
                @value.extend(CreditCardSupport::CreditCardNumber)
         | 
| 11 | 
            -
             | 
| 12 | 
            -
                validates_luhn &&
         | 
| 13 | 
            -
                validates_testcard &&
         | 
| 14 | 
            -
                validates_issuer_allowed
         | 
| 15 | 
            -
              end
         | 
| 16 | 
            -
             | 
| 17 | 
            -
              def validates_luhn
         | 
| 18 | 
            -
                if !@value.luhn?
         | 
| 19 | 
            -
                  @record.errors.add(@attribute, t(:luhn_not_valid))
         | 
| 20 | 
            -
                  false
         | 
| 21 | 
            -
                else
         | 
| 22 | 
            -
                  true
         | 
| 23 | 
            -
                end
         | 
| 24 | 
            -
              end
         | 
| 25 | 
            -
             | 
| 26 | 
            -
              def validates_testcard
         | 
| 27 | 
            -
                if !options[:allow_testcards] && @value.testcard?
         | 
| 28 | 
            -
                  @record.errors.add(@attribute, t(:testcard_not_supported))
         | 
| 29 | 
            -
                  false
         | 
| 30 | 
            -
                else
         | 
| 31 | 
            -
                  true
         | 
| 32 50 | 
             
                end
         | 
| 33 51 | 
             
              end
         | 
| 34 | 
            -
             | 
| 35 | 
            -
              def validates_issuer_allowed
         | 
| 36 | 
            -
                if options[:allow_issuers] && !options[:allow_issuers].include?(@value.issuer)
         | 
| 37 | 
            -
                  @record.errors.add(@attribute, t(:issuer_not_supported, issuer: @value.issuer))
         | 
| 38 | 
            -
                  false
         | 
| 39 | 
            -
                else
         | 
| 40 | 
            -
                  true
         | 
| 41 | 
            -
                end
         | 
| 42 | 
            -
              end
         | 
| 43 | 
            -
             | 
| 44 | 
            -
              def t(code, *args)
         | 
| 45 | 
            -
                 I18n.t("credit_card_support.validators.number.messages.#{code}", *args)
         | 
| 46 | 
            -
              end
         | 
| 47 | 
            -
             | 
| 48 52 | 
             
            end
         |