From 6c962f4f72e190dc1771f286dc8f3123d074dc51 Mon Sep 17 00:00:00 2001 From: SkylerRogers Date: Tue, 8 Oct 2013 17:01:10 -0400 Subject: [PATCH] Check for presence in Spree::CreditCard#expiry method The expiry method needs to check for presence before parsing. An empty card expiration field will raise the error "no implicit conversion of nil into String" on submission. Fixes #3847 Fixes #3896 --- core/app/models/spree/credit_card.rb | 6 ++++-- core/spec/models/spree/credit_card_spec.rb | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rb index 21bc709ec10..4351b3611e4 100644 --- a/core/app/models/spree/credit_card.rb +++ b/core/app/models/spree/credit_card.rb @@ -17,8 +17,10 @@ class CreditCard < ActiveRecord::Base alias_attribute :brand, :cc_type def expiry=(expiry) - self[:month], self[:year] = expiry.split(" / ") - self[:year] = "20" + self[:year] + if expiry.present? + self[:month], self[:year] = expiry.split(" / ") + self[:year] = "20" + self[:year] + end end def number=(num) diff --git a/core/spec/models/spree/credit_card_spec.rb b/core/spec/models/spree/credit_card_spec.rb index 0142064c768..d0aa01f12db 100644 --- a/core/spec/models/spree/credit_card_spec.rb +++ b/core/spec/models/spree/credit_card_spec.rb @@ -150,6 +150,25 @@ def stub_rails_env(environment) end end + # Regression test for #3847 & #3896 + context "#expiry=" do + it "can set with a 2-digit month and year" do + credit_card.expiry = '04 / 14' + credit_card.month = '04' + credit_card.year = '2014' + end + + it "can set with a 2-digit month and 4-digit year" do + credit_card.expiry = '04 / 2014' + credit_card.month = '04' + credit_card.year = '2014' + end + + it "does not blow up when passed an empty string" do + lambda { credit_card.expiry = '' }.should_not raise_error + end + end + context "#cc_type=" do it "converts between the different types" do credit_card.cc_type = 'mastercard'