Skip to content

Commit

Permalink
specs for :only, :except examples in activemodel validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Jan 14, 2015
1 parent bfdf624 commit a11f6e6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# UNRELEASED

* :only and :except options for CreditCardNumberValidator
* credit card generator
* test unit -> specs migration
* card rules structure changed to allow custom options
Expand Down
12 changes: 11 additions & 1 deletion lib/active_model/credit_card_number_validator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# == 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: {brands: [:amex, :maestro]}
# 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
#

Expand Down
27 changes: 19 additions & 8 deletions spec/active_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
end


describe "Except Amex and Maestro brand" do
it "should reject all other valid numbers" do
VALID_NUMBERS.except(:amex, :maestro) do |_, numbers|
card = model
card.number = numbers.first
card.valid?.must_equal false
end
end

it "should accept using except options" do
VALID_NUMBERS.except(:amex, :maestro) do |_, numbers|
card = model
card.number3 = numbers.first
card.valid?.must_equal true
end
end

end

describe "Only Amex and Mestro brands" do

Expand All @@ -28,18 +46,11 @@
VALID_NUMBERS.slice(:amex, :maestro) do |_, numbers|
card = model
card.number = numbers.first
card.number2 = numbers.first
card.valid?.must_equal true
end
end

it "should reject all other valid numbers" do
VALID_NUMBERS.except(:amex, :maestro) do |_, numbers|
card = model
card.number = numbers.first
card.valid?.must_equal false
end

end

end
end
5 changes: 3 additions & 2 deletions spec/models/credit_card.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class CreditCard
attr_accessor :number, :number4, :number5
attr_accessor :number, :number2, :number3, :number4, :number5
include ActiveModel::Validations
validates :number, credit_card_number: {brands: [:amex, :maestro]} , allow_blank: true

validates :number2, credit_card_number: {only: [:amex, :maestro]}, allow_blank: true
validates :number3, credit_card_number: {except: [:amex, :maestro]} , allow_blank: true
validates :number4, credit_card_number: {brands: :any} , allow_blank: true
validates :number5, credit_card_number: true , allow_blank: true
end

0 comments on commit a11f6e6

Please sign in to comment.