Merge pull request #1063 from consul/where-is-carmen-sandiego

does not require phones to be Spanish any more
This commit is contained in:
Juanjo Bazán
2016-04-14 09:08:43 +02:00
2 changed files with 11 additions and 9 deletions

View File

@@ -4,14 +4,9 @@ class Verification::Sms
attr_accessor :user, :phone, :confirmation_code
validates_presence_of :phone
validates :phone, length: { is: 9 }
validate :spanish_phone
validates :phone, format: { with: /\A[\d \+]+\z/ }
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
end

View File

@@ -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
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