-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActiveModel’s Luhn Algorithm validator
- Loading branch information
Kimmie
committed
Jul 7, 2019
1 parent
9f3bea5
commit 8bba7db
Showing
4 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'luhn_algorithm/version' | ||
require 'luhn_algorithm/validator' | ||
|
||
module LuhnAlgorithm | ||
NUMBER_ONLY = /^\d+$/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'active_model' | ||
|
||
module ActiveModel | ||
module Validations | ||
# active model validator for Luhn Algorithm | ||
class LuhnAlgorithmValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
return if LuhnAlgorithm.valid?(value) | ||
record.errors.add(attribute, options[:message] || :invalid) | ||
end | ||
end | ||
|
||
module HelperMethods | ||
def validates_luhn_algorithm_of(*attr_names) | ||
validates_with LuhnAlgorithmValidator, _merge_attributes(attr_names) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class SampleModel | ||
include ::ActiveModel::Validations | ||
|
||
attr_accessor :number1, :number2 | ||
|
||
validates_luhn_algorithm_of :number1 | ||
validates :number2, luhn_algorithm: true | ||
end | ||
|
||
RSpec.describe ActiveModel::Validations::LuhnAlgorithmValidator do | ||
let(:model) { SampleModel.new } | ||
|
||
context 'validates attributes based on luhn_algorithm' do | ||
it 'calls LuhnAlgorithm.valid?' do | ||
model.number1 = 6_011_111_111_111_118 | ||
model.number2 = '4111111111111111' | ||
expect(LuhnAlgorithm).to receive(:valid?).and_call_original.twice | ||
end | ||
end | ||
end |