From 068ea45cbe9529a7ea411be0d62eabfb7118e82f Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 7 Aug 2014 14:45:58 +0300 Subject: [PATCH] Add Mobile Phone validation to SpeakerProfile --- Gemfile | 3 +++ Gemfile.lock | 9 +++++++++ app/models/speaker_profile.rb | 4 +++- config/locales/bg.yml | 3 +++ spec/models/speaker_profile_spec.rb | 19 +++++++++++++++++-- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 0efffc4..0310d89 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,9 @@ gem 'devise-i18n' gem 'simple_form' +# Phone validation +gem 'phony_rails' + group :development do gem 'spring' gem 'spring-commands-rspec' diff --git a/Gemfile.lock b/Gemfile.lock index 26d4eec..64602a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,9 @@ GEM coffee-script-source execjs coffee-script-source (1.7.1) + countries (0.9.3) + currencies (~> 0.4.2) + currencies (0.4.2) devise (3.2.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -99,6 +102,11 @@ GEM mini_portile (= 0.6.0) orm_adapter (0.5.0) pg (0.17.1) + phony (2.2.14) + phony_rails (0.6.1) + activesupport (>= 3.0) + countries (>= 0.8.2) + phony (~> 2.1) polyglot (0.3.5) pry (0.10.0) coderay (~> 1.1.0) @@ -221,6 +229,7 @@ DEPENDENCIES guard-rspec jquery-rails pg + phony_rails rails (= 4.1.4) rails-erd rails-i18n diff --git a/app/models/speaker_profile.rb b/app/models/speaker_profile.rb index d7f3414..e518374 100644 --- a/app/models/speaker_profile.rb +++ b/app/models/speaker_profile.rb @@ -4,6 +4,8 @@ class SpeakerProfile < ActiveRecord::Base validates :first_name, presence: true validates :last_name, presence: true validates :photo_url, presence: true - validates :mobile_phone, presence: true + validates :mobile_phone, phony_plausible: true, presence: true validates :biography, presence: true + + phony_normalize :mobile_phone, default_country_code: 'BG' end diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 3864304..c9472f4 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -21,3 +21,6 @@ bg: hello: "Здравей, свят" + errors: + messages: + improbable_phone: 'не е валиден телефонен номер' \ No newline at end of file diff --git a/spec/models/speaker_profile_spec.rb b/spec/models/speaker_profile_spec.rb index 4272afd..ab4fcb5 100644 --- a/spec/models/speaker_profile_spec.rb +++ b/spec/models/speaker_profile_spec.rb @@ -13,8 +13,23 @@ RSpec.describe SpeakerProfile, :type => :model do expect(build(:speaker_profile, photo_url: nil)).to have_error_on :photo_url end - it 'is invalid without a mobile phone' do - expect(build(:speaker_profile, mobile_phone: nil)).to have_error_on :mobile_phone + describe 'mobile_phone' do + it 'must be present' do + expect(build(:speaker_profile, mobile_phone: nil)).to have_error_on :mobile_phone + end + + it 'must be a valid phone number' do + expect(build(:speaker_profile, mobile_phone: 'abc')).to have_error_on :mobile_phone + end + + it 'is stored in a normalized form' do + speaker_profile = create :speaker_profile, mobile_phone: '0883444555' + expect(speaker_profile.mobile_phone).to eq '359883444555' + + speaker_profile.mobile_phone = '+1883444555' + speaker_profile.save + expect(speaker_profile.mobile_phone).to eq '1883444555' + end end it 'is invalid without a bio' do