From 005bd2696595e6b357d692b420c1841bf214a6f3 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 13 Apr 2016 18:52:53 +0200 Subject: [PATCH 1/2] does not require phones to be Spanish any more --- app/models/verification/sms.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/models/verification/sms.rb b/app/models/verification/sms.rb index ba484c4f3..19e0bf041 100644 --- a/app/models/verification/sms.rb +++ b/app/models/verification/sms.rb @@ -5,13 +5,8 @@ class Verification::Sms validates_presence_of :phone validates :phone, length: { is: 9 } - validate :spanish_phone validate :uniqness_phone - def spanish_phone - errors.add(:phone, :invalid) unless phone.start_with?('6', '7') - end - def uniqness_phone errors.add(:phone, :taken) if User.where(confirmed_phone: phone).any? end @@ -40,4 +35,4 @@ class Verification::Sms def generate_confirmation_code rand.to_s[2..5] end -end \ No newline at end of file +end From 92ecd7c983e664f35462b73a457925e60eb6b9c2 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 13 Apr 2016 19:27:04 +0200 Subject: [PATCH 2/2] replaces length 9 validation by format validation: only numbers, spaces and plus sign allowed --- app/models/verification/sms.rb | 2 +- spec/models/sms_spec.rb | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/models/verification/sms.rb b/app/models/verification/sms.rb index 19e0bf041..1a013f1d8 100644 --- a/app/models/verification/sms.rb +++ b/app/models/verification/sms.rb @@ -4,7 +4,7 @@ class Verification::Sms attr_accessor :user, :phone, :confirmation_code validates_presence_of :phone - validates :phone, length: { is: 9 } + validates :phone, format: { with: /\A[\d \+]+\z/ } validate :uniqness_phone def uniqness_phone diff --git a/spec/models/sms_spec.rb b/spec/models/sms_spec.rb index f52ff3c6e..83723cb3c 100644 --- a/spec/models/sms_spec.rb +++ b/spec/models/sms_spec.rb @@ -7,9 +7,16 @@ describe Verification::Sms do end it "should validate uniqness of phone" do - user = create(:user, confirmed_phone: "699999999") + create(:user, confirmed_phone: "699999999") sms = Verification::Sms.new(phone: "699999999") expect(sms).to_not be_valid end -end \ No newline at end of file + it "only allows spaces, numbers and the + sign" do + expect(build(:verification_sms, phone: "0034 666666666")).to be_valid + expect(build(:verification_sms, phone: "+34 666666666")).to be_valid + expect(build(:verification_sms, phone: "hello there")).to_not be_valid + expect(build(:verification_sms, phone: "555; DROP TABLE USERS")).to_not be_valid + end + +end