phony_rails 0.13.0 → 0.13.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +63 -0
- data/.travis.yml +6 -5
- data/CHANGELOG.md +2 -2
- data/Gemfile +6 -5
- data/Gemfile.lock +14 -0
- data/Guardfile +1 -1
- data/Rakefile +2 -2
- data/lib/phony_rails.rb +9 -9
- data/lib/phony_rails/string_extensions.rb +38 -43
- data/lib/phony_rails/version.rb +1 -1
- data/lib/validators/phony_validator.rb +5 -9
- data/phony_rails.gemspec +14 -14
- data/spec/lib/phony_rails_spec.rb +26 -27
- data/spec/lib/validators/phony_validator_spec.rb +83 -107
- data/spec/spec_helper.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6000f96b00258917b5399b860088a5d1d11d1fff
|
4
|
+
data.tar.gz: 666250cc63932a0d10563cd122ea2a8835032d90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 935b0168ab070efdef52c294a886c8ac1c8e9a50c9e8a428be7762a449c63fd57ce3275b4b1f91e5772966ceb40c525101afc73e517bf3c82ca1d132d49511d8
|
7
|
+
data.tar.gz: cd99b5a59f9a1478480380b2df006b5f7869809fa2b520d669b5477188f70ff6e47e69df311782760ddb8652c868ab70338a046ac2ea34d77a4b9bb419818524
|
data/.rubocop.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2016-03-12 20:44:43 +0100 using RuboCop version 0.38.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
# Configuration parameters: AllowSafeAssignment.
|
11
|
+
Lint/AssignmentInCondition:
|
12
|
+
Exclude:
|
13
|
+
- 'lib/phony_rails.rb'
|
14
|
+
|
15
|
+
# Offense count: 2
|
16
|
+
Lint/UnderscorePrefixedVariableName:
|
17
|
+
Exclude:
|
18
|
+
- 'lib/phony_rails.rb'
|
19
|
+
|
20
|
+
# Offense count: 4
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Max: 41
|
23
|
+
|
24
|
+
# Offense count: 3
|
25
|
+
Metrics/CyclomaticComplexity:
|
26
|
+
Max: 21
|
27
|
+
|
28
|
+
# Offense count: 162
|
29
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
30
|
+
# URISchemes: http, https
|
31
|
+
Metrics/LineLength:
|
32
|
+
Max: 166
|
33
|
+
|
34
|
+
# Offense count: 4
|
35
|
+
# Configuration parameters: CountComments.
|
36
|
+
Metrics/MethodLength:
|
37
|
+
Max: 25
|
38
|
+
|
39
|
+
# Offense count: 2
|
40
|
+
Metrics/PerceivedComplexity:
|
41
|
+
Max: 23
|
42
|
+
|
43
|
+
# Offense count: 1
|
44
|
+
# Cop supports --auto-correct.
|
45
|
+
Performance/StartWith:
|
46
|
+
Exclude:
|
47
|
+
- 'lib/phony_rails.rb'
|
48
|
+
|
49
|
+
# Offense count: 1
|
50
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
51
|
+
# SupportedStyles: nested, compact
|
52
|
+
Style/ClassAndModuleChildren:
|
53
|
+
Exclude:
|
54
|
+
- 'lib/phony_rails.rb'
|
55
|
+
|
56
|
+
# Offense count: 7
|
57
|
+
Style/Documentation:
|
58
|
+
Exclude:
|
59
|
+
- 'spec/**/*'
|
60
|
+
- 'test/**/*'
|
61
|
+
- 'lib/phony_rails.rb'
|
62
|
+
- 'lib/phony_rails/string_extensions.rb'
|
63
|
+
- 'lib/validators/phony_validator.rb'
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
-
## [
|
3
|
+
## [v0.13.0](https://github.com/joost/phony_rails/tree/v0.13.0) (2016-03-12)
|
4
4
|
|
5
|
-
[Full Changelog](https://github.com/joost/phony_rails/compare/v0.12.11...
|
5
|
+
[Full Changelog](https://github.com/joost/phony_rails/compare/v0.12.11...v0.13.0)
|
6
6
|
|
7
7
|
**Closed issues:**
|
8
8
|
|
data/Gemfile
CHANGED
@@ -6,8 +6,9 @@ gemspec # Specify your gem's dependencies in phony_number.gemspec
|
|
6
6
|
gem 'sqlite3'
|
7
7
|
|
8
8
|
gem 'rake'
|
9
|
-
gem 'rspec'
|
10
|
-
gem 'guard'
|
11
|
-
gem 'guard-bundler'
|
12
|
-
gem 'guard-rspec'
|
13
|
-
gem 'coveralls', require: false
|
9
|
+
gem 'rspec' # , '~> 2.14.0'
|
10
|
+
gem 'guard' # , '~> 2.10.1'
|
11
|
+
gem 'guard-bundler' # , '~> 2.0.0'
|
12
|
+
gem 'guard-rspec' # , '~> 4.2.0'
|
13
|
+
gem 'coveralls', require: false
|
14
|
+
gem 'rubocop'
|
data/Gemfile.lock
CHANGED
@@ -22,6 +22,7 @@ GEM
|
|
22
22
|
thread_safe (~> 0.3, >= 0.3.4)
|
23
23
|
tzinfo (~> 1.1)
|
24
24
|
arel (6.0.3)
|
25
|
+
ast (2.2.0)
|
25
26
|
bson (4.0.4)
|
26
27
|
builder (3.2.2)
|
27
28
|
coderay (1.1.1)
|
@@ -73,11 +74,15 @@ GEM
|
|
73
74
|
nenv (~> 0.1)
|
74
75
|
shellany (~> 0.0)
|
75
76
|
origin (2.2.0)
|
77
|
+
parser (2.3.0.6)
|
78
|
+
ast (~> 2.2)
|
76
79
|
phony (2.15.17)
|
80
|
+
powerpack (0.1.1)
|
77
81
|
pry (0.10.3)
|
78
82
|
coderay (~> 1.1.0)
|
79
83
|
method_source (~> 0.8.1)
|
80
84
|
slop (~> 3.4)
|
85
|
+
rainbow (2.1.0)
|
81
86
|
rake (11.1.0)
|
82
87
|
rb-fsevent (0.9.7)
|
83
88
|
rb-inotify (0.9.7)
|
@@ -95,6 +100,13 @@ GEM
|
|
95
100
|
diff-lcs (>= 1.2.0, < 2.0)
|
96
101
|
rspec-support (~> 3.4.0)
|
97
102
|
rspec-support (3.4.1)
|
103
|
+
rubocop (0.38.0)
|
104
|
+
parser (>= 2.3.0.6, < 3.0)
|
105
|
+
powerpack (~> 0.1)
|
106
|
+
rainbow (>= 1.99.1, < 3.0)
|
107
|
+
ruby-progressbar (~> 1.7)
|
108
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
109
|
+
ruby-progressbar (1.7.5)
|
98
110
|
shellany (0.0.1)
|
99
111
|
simplecov (0.11.2)
|
100
112
|
docile (~> 1.1.0)
|
@@ -110,6 +122,7 @@ GEM
|
|
110
122
|
tins (1.6.0)
|
111
123
|
tzinfo (1.2.2)
|
112
124
|
thread_safe (~> 0.1)
|
125
|
+
unicode-display_width (1.0.2)
|
113
126
|
|
114
127
|
PLATFORMS
|
115
128
|
ruby
|
@@ -124,4 +137,5 @@ DEPENDENCIES
|
|
124
137
|
phony_rails!
|
125
138
|
rake
|
126
139
|
rspec
|
140
|
+
rubocop
|
127
141
|
sqlite3
|
data/Guardfile
CHANGED
data/Rakefile
CHANGED
data/lib/phony_rails.rb
CHANGED
@@ -27,7 +27,7 @@ module PhonyRails
|
|
27
27
|
def self.normalize_number(number, options = {})
|
28
28
|
return if number.nil?
|
29
29
|
original_number = number
|
30
|
-
number = number.
|
30
|
+
number = number.dup # Just to be sure, we don't want to change the original.
|
31
31
|
number.gsub!(/[^\(\)\d\+]/, '') # Strips weird stuff from the number
|
32
32
|
return if number.blank?
|
33
33
|
if _country_number = options[:country_number] || country_number_for(options[:country_code])
|
@@ -86,15 +86,15 @@ module PhonyRails
|
|
86
86
|
# This methods sets the attribute to the normalized version.
|
87
87
|
# It also adds the country_code (number), eg. 31 for NL numbers.
|
88
88
|
def set_phony_normalized_numbers(attributes, options = {})
|
89
|
-
options = options.
|
90
|
-
if
|
89
|
+
options = options.dup
|
90
|
+
if respond_to?(:country_code)
|
91
91
|
set_country_as = options[:enforce_record_country] ? :country_code : :default_country_code
|
92
|
-
options[set_country_as] ||=
|
92
|
+
options[set_country_as] ||= country_code
|
93
93
|
end
|
94
94
|
attributes.each do |attribute|
|
95
95
|
attribute_name = options[:as] || attribute
|
96
|
-
|
97
|
-
new_value= PhonyRails.normalize_number(send(attribute), options)
|
96
|
+
raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
|
97
|
+
new_value = PhonyRails.normalize_number(send(attribute), options)
|
98
98
|
send("#{attribute_name}=", new_value) if new_value
|
99
99
|
end
|
100
100
|
end
|
@@ -128,11 +128,11 @@ module PhonyRails
|
|
128
128
|
main_options = attributes.last.is_a?(Hash) ? attributes.pop : {}
|
129
129
|
main_options.assert_valid_keys :country_code, :default_country_code
|
130
130
|
attributes.each do |attribute|
|
131
|
-
|
131
|
+
raise(StandardError, "Instance method normalized_#{attribute} already exists on #{name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
|
132
132
|
define_method :"normalized_#{attribute}" do |*args|
|
133
133
|
options = args.first || {}
|
134
|
-
|
135
|
-
options[:country_code] ||= country_code if
|
134
|
+
raise(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless respond_to?(attribute)
|
135
|
+
options[:country_code] ||= country_code if respond_to?(:country_code)
|
136
136
|
PhonyRails.normalize_number(send(attribute), main_options.merge(options))
|
137
137
|
end
|
138
138
|
end
|
@@ -1,46 +1,41 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
# Add a method to the String class so we can easily format phone numbers.
|
14
|
-
# This enables:
|
15
|
-
# "31612341234".phony_formatted # => '06 12341234'
|
16
|
-
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
|
17
|
-
# To first normalize a String use:
|
18
|
-
# "010-12341234".phony_formatted(:normalize => :NL)
|
19
|
-
# To return nil when a number is not correct (checked using Phony.plausible?) use
|
20
|
-
# "010-12341234".phony_formatted(strict: true)
|
21
|
-
# When an error occurs during conversion it will return the original String.
|
22
|
-
# To raise an error use:
|
23
|
-
# "somestring".phone_formatted(raise: true)
|
24
|
-
def phony_formatted(options = {})
|
25
|
-
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" if not options.is_a?(Hash)
|
26
|
-
options = options.dup
|
27
|
-
normalize_country_code = options.delete(:normalize)
|
28
|
-
s = (normalize_country_code ? PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s, :add_plus => false) : self.gsub(/\D/, ''))
|
29
|
-
return if s.blank?
|
30
|
-
return if options[:strict] && !Phony.plausible?(s)
|
31
|
-
Phony.format(s, options.reverse_merge(:format => :national))
|
32
|
-
rescue
|
33
|
-
if options[:raise]
|
34
|
-
raise
|
35
|
-
else
|
36
|
-
s
|
37
|
-
end
|
38
|
-
end
|
1
|
+
class String
|
2
|
+
# Usage:
|
3
|
+
# "+31 (0)30 1234 123".phony_normalized # => '+31301234123'
|
4
|
+
# "(0)30 1234 123".phony_normalized # => '301234123'
|
5
|
+
# "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
|
6
|
+
def phony_normalized(options = {})
|
7
|
+
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)
|
8
|
+
options = options.dup
|
9
|
+
PhonyRails.normalize_number(self, options)
|
10
|
+
end
|
39
11
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
12
|
+
# Add a method to the String class so we can easily format phone numbers.
|
13
|
+
# This enables:
|
14
|
+
# "31612341234".phony_formatted # => '06 12341234'
|
15
|
+
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
|
16
|
+
# To first normalize a String use:
|
17
|
+
# "010-12341234".phony_formatted(:normalize => :NL)
|
18
|
+
# To return nil when a number is not correct (checked using Phony.plausible?) use
|
19
|
+
# "010-12341234".phony_formatted(strict: true)
|
20
|
+
# When an error occurs during conversion it will return the original String.
|
21
|
+
# To raise an error use:
|
22
|
+
# "somestring".phone_formatted(raise: true)
|
23
|
+
def phony_formatted(options = {})
|
24
|
+
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)
|
25
|
+
options = options.dup
|
26
|
+
normalize_country_code = options.delete(:normalize)
|
27
|
+
s = (normalize_country_code ? PhonyRails.normalize_number(self, default_country_code: normalize_country_code.to_s, add_plus: false) : gsub(/\D/, ''))
|
28
|
+
return if s.blank?
|
29
|
+
return if options[:strict] && !Phony.plausible?(s)
|
30
|
+
Phony.format(s, options.reverse_merge(format: :national))
|
31
|
+
rescue
|
32
|
+
raise if options[:raise]
|
33
|
+
s
|
34
|
+
end
|
45
35
|
|
36
|
+
# The bang method
|
37
|
+
def phony_formatted!(options = {})
|
38
|
+
raise ArgumentError, 'The :strict options is only supported in the phony_formatted (non bang) method.' if options[:strict]
|
39
|
+
replace(phony_formatted(options))
|
46
40
|
end
|
41
|
+
end
|
data/lib/phony_rails/version.rb
CHANGED
@@ -3,15 +3,14 @@
|
|
3
3
|
# validate :phone_number, :phony_plausible => true
|
4
4
|
require 'active_model'
|
5
5
|
class PhonyPlausibleValidator < ActiveModel::EachValidator
|
6
|
-
|
7
6
|
# Validates a String using Phony.plausible? method.
|
8
7
|
def validate_each(record, attribute, value)
|
9
8
|
return if value.blank?
|
10
9
|
|
11
10
|
@record = record
|
12
11
|
|
13
|
-
value = PhonyRails.normalize_number
|
14
|
-
@record.errors.add(attribute, error_message)
|
12
|
+
value = PhonyRails.normalize_number(value.dup, default_country_code: normalized_country_code) if normalized_country_code
|
13
|
+
@record.errors.add(attribute, error_message) unless Phony.plausible?(value, cc: country_number)
|
15
14
|
end
|
16
15
|
|
17
16
|
private
|
@@ -43,22 +42,19 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
|
|
43
42
|
def normalized_country_code
|
44
43
|
options[:normalized_country_code]
|
45
44
|
end
|
46
|
-
|
47
45
|
end
|
48
46
|
|
49
47
|
module ActiveModel
|
50
48
|
module Validations
|
51
49
|
module HelperMethods
|
52
|
-
|
53
50
|
def validates_plausible_phone(*attr_names)
|
54
51
|
# merged attributes are modified somewhere, so we are cloning them for each validator
|
55
52
|
merged_attributes = _merge_attributes(attr_names)
|
56
53
|
|
57
|
-
validates_with PresenceValidator, merged_attributes.
|
58
|
-
validates_with FormatValidator, merged_attributes.
|
59
|
-
validates_with PhonyPlausibleValidator, merged_attributes.
|
54
|
+
validates_with PresenceValidator, merged_attributes.dup if merged_attributes[:presence]
|
55
|
+
validates_with FormatValidator, merged_attributes.dup if merged_attributes[:with] || merged_attributes[:without]
|
56
|
+
validates_with PhonyPlausibleValidator, merged_attributes.dup
|
60
57
|
end
|
61
|
-
|
62
58
|
end
|
63
59
|
end
|
64
60
|
end
|
data/phony_rails.gemspec
CHANGED
@@ -2,25 +2,25 @@
|
|
2
2
|
require File.expand_path('../lib/phony_rails/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = [
|
6
|
-
gem.email = [
|
7
|
-
gem.description =
|
8
|
-
gem.summary =
|
9
|
-
gem.homepage =
|
5
|
+
gem.authors = ['Joost Hietbrink']
|
6
|
+
gem.email = ['joost@joopp.com']
|
7
|
+
gem.description = 'This Gem adds useful methods to your Rails app to validate, display and save phone numbers.'
|
8
|
+
gem.summary = 'This Gem adds useful methods to your Rails app to validate, display and save phone numbers.'
|
9
|
+
gem.homepage = 'https://github.com/joost/phony_rails'
|
10
10
|
gem.license = 'MIT'
|
11
11
|
|
12
|
-
gem.files = `git ls-files`.split(
|
13
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
14
14
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
-
gem.name =
|
16
|
-
gem.require_paths = [
|
15
|
+
gem.name = 'phony_rails'
|
16
|
+
gem.require_paths = ['lib']
|
17
17
|
gem.version = PhonyRails::VERSION
|
18
18
|
|
19
|
-
gem.post_install_message =
|
19
|
+
gem.post_install_message = 'PhonyRails v0.10.0 changes the way numbers are stored!'
|
20
20
|
gem.post_install_message = "It now adds a '+' to the normalized number when it starts with a country number!"
|
21
21
|
|
22
|
-
gem.add_dependency
|
23
|
-
gem.add_dependency
|
24
|
-
gem.add_development_dependency
|
25
|
-
gem.add_development_dependency
|
22
|
+
gem.add_dependency 'phony', '~> 2.12'
|
23
|
+
gem.add_dependency 'activesupport', '>= 3.0'
|
24
|
+
gem.add_development_dependency 'activerecord', '>= 3.0'
|
25
|
+
gem.add_development_dependency 'mongoid', '>= 3.0'
|
26
26
|
end
|
@@ -8,9 +8,9 @@ describe PhonyRails do
|
|
8
8
|
describe 'phony_format String extension' do
|
9
9
|
describe 'the phony_formatted method' do
|
10
10
|
it 'should not modify the original options Hash' do
|
11
|
-
options = {normalize: :NL, format: :international}
|
11
|
+
options = { normalize: :NL, format: :international }
|
12
12
|
'0101234123'.phony_formatted(options)
|
13
|
-
expect(options).to eql(
|
13
|
+
expect(options).to eql(normalize: :NL, format: :international)
|
14
14
|
end
|
15
15
|
|
16
16
|
describe 'with the bang!' do
|
@@ -69,7 +69,7 @@ describe PhonyRails do
|
|
69
69
|
context 'when raise is true' do
|
70
70
|
it 'should raise the error' do
|
71
71
|
expect(lambda do
|
72
|
-
|
72
|
+
'8887716095'.phony_formatted(format: :international, raise: true)
|
73
73
|
end).to raise_error(NoMethodError)
|
74
74
|
end
|
75
75
|
end
|
@@ -89,12 +89,12 @@ describe PhonyRails do
|
|
89
89
|
|
90
90
|
# https://github.com/joost/phony_rails/issues/42
|
91
91
|
it 'should pass Github issue #42' do
|
92
|
-
expect(PhonyRails.normalize_number(
|
92
|
+
expect(PhonyRails.normalize_number('0606060606', default_country_code: 'FR')).to eq('+33606060606')
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'should pass Github issue #85' do
|
96
|
-
expect(PhonyRails.normalize_number(
|
97
|
-
expect(PhonyRails.normalize_number(
|
96
|
+
expect(PhonyRails.normalize_number('47386160', default_country_code: 'NO')).to eq('+4747386160')
|
97
|
+
expect(PhonyRails.normalize_number('47386160', country_number: '47')).to eq('+4747386160')
|
98
98
|
end
|
99
99
|
|
100
100
|
it 'should pass Github issue #87' do
|
@@ -126,9 +126,9 @@ describe PhonyRails do
|
|
126
126
|
|
127
127
|
it 'should pass Github issue #113' do
|
128
128
|
number = '(951) 703-593'
|
129
|
-
expect(lambda
|
129
|
+
expect(lambda do
|
130
130
|
number.phony_formatted!(normalize: 'US', spaces: '-', strict: true)
|
131
|
-
|
131
|
+
end).to raise_error(ArgumentError)
|
132
132
|
end
|
133
133
|
|
134
134
|
it 'should pass Github issue #95' do
|
@@ -192,7 +192,7 @@ describe PhonyRails do
|
|
192
192
|
normal.phone_number = 'HAHA'
|
193
193
|
expect(normal).to_not be_valid
|
194
194
|
expect(normal.phone_number).to eq('HAHA')
|
195
|
-
expect(normal.errors.messages).to include(phone_number: [
|
195
|
+
expect(normal.errors.messages).to include(phone_number: ['is an invalid number'])
|
196
196
|
end
|
197
197
|
end
|
198
198
|
|
@@ -215,7 +215,7 @@ describe PhonyRails do
|
|
215
215
|
end
|
216
216
|
|
217
217
|
it 'returns nil on blank string' do
|
218
|
-
expect(
|
218
|
+
expect(''.phony_formatted).to be_nil
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
@@ -225,9 +225,9 @@ describe PhonyRails do
|
|
225
225
|
end
|
226
226
|
|
227
227
|
it 'should not modify the original options Hash' do
|
228
|
-
options = {normalize: :NL, format: :international}
|
228
|
+
options = { normalize: :NL, format: :international }
|
229
229
|
'0101234123'.phony_normalized(options)
|
230
|
-
expect(options).to eql(
|
230
|
+
expect(options).to eql(normalize: :NL, format: :international)
|
231
231
|
end
|
232
232
|
|
233
233
|
context 'when String misses a country_code' do
|
@@ -295,7 +295,6 @@ describe PhonyRails do
|
|
295
295
|
it 'should recognize lowercase country codes' do
|
296
296
|
expect(PhonyRails.normalize_number('010-1234123', country_code: 'nl')).to eql('+31101234123')
|
297
297
|
end
|
298
|
-
|
299
298
|
end
|
300
299
|
|
301
300
|
it 'should handle some edge cases (with country_code)' do
|
@@ -336,7 +335,7 @@ describe PhonyRails do
|
|
336
335
|
is_expected.to be_plausible_number valid_number, country_code: 'US'
|
337
336
|
end
|
338
337
|
|
339
|
-
it
|
338
|
+
it 'should return false for an invalid number' do
|
340
339
|
is_expected.not_to be_plausible_number invalid_number, country_code: 'US'
|
341
340
|
end
|
342
341
|
|
@@ -381,9 +380,9 @@ describe PhonyRails do
|
|
381
380
|
end
|
382
381
|
|
383
382
|
it 'should raise error on existing methods' do
|
384
|
-
expect(lambda
|
383
|
+
expect(lambda do
|
385
384
|
model_klass.phony_normalized_method(:phone_method)
|
386
|
-
|
385
|
+
end).to raise_error(StandardError)
|
387
386
|
end
|
388
387
|
|
389
388
|
it 'should raise error on not existing attribute' do
|
@@ -414,15 +413,15 @@ describe PhonyRails do
|
|
414
413
|
end
|
415
414
|
|
416
415
|
it 'should accept :as option with single existing attribute name' do
|
417
|
-
expect(lambda
|
416
|
+
expect(lambda do
|
418
417
|
model_klass.phony_normalize(:phone_number, as: 'phone_number_as_normalized')
|
419
|
-
|
418
|
+
end).to_not raise_error
|
420
419
|
end
|
421
420
|
|
422
421
|
it 'should accept a non existing attribute name' do
|
423
|
-
expect(lambda
|
422
|
+
expect(lambda do
|
424
423
|
dummy_klass.phony_normalize(:non_existing_attribute)
|
425
|
-
|
424
|
+
end).to_not raise_error
|
426
425
|
end
|
427
426
|
|
428
427
|
it 'should accept supported options' do
|
@@ -448,7 +447,7 @@ describe PhonyRails do
|
|
448
447
|
expect(model.normalized_phone_attribute).to eql('+31101234123')
|
449
448
|
end
|
450
449
|
|
451
|
-
it
|
450
|
+
it 'should return a normalized version of a method' do
|
452
451
|
model = model_klass.new(phone_method: '+31-(0)10-1234123')
|
453
452
|
expect(model.normalized_phone_method).to eql('+31101234123')
|
454
453
|
end
|
@@ -526,17 +525,17 @@ describe PhonyRails do
|
|
526
525
|
it 'should raise a RuntimeError at validation if the attribute doesn\'t exist' do
|
527
526
|
dummy_klass.phony_normalize :non_existing_attribute
|
528
527
|
dummy = dummy_klass.new
|
529
|
-
expect(lambda
|
528
|
+
expect(lambda do
|
530
529
|
dummy.valid?
|
531
|
-
|
530
|
+
end).to raise_error(RuntimeError)
|
532
531
|
end
|
533
532
|
|
534
533
|
it 'should raise a RuntimeError at validation if the :as option attribute doesn\'t exist' do
|
535
534
|
dummy_klass.phony_normalize :phone_number, as: :non_existing_attribute
|
536
535
|
dummy = dummy_klass.new
|
537
|
-
expect(lambda
|
536
|
+
expect(lambda do
|
538
537
|
dummy.valid?
|
539
|
-
|
538
|
+
end).to raise_error(RuntimeError)
|
540
539
|
end
|
541
540
|
end
|
542
541
|
end
|
@@ -564,11 +563,11 @@ describe PhonyRails do
|
|
564
563
|
let(:model_klass) { RelaxedActiveRecordModel }
|
565
564
|
let(:record) { model_klass.new }
|
566
565
|
|
567
|
-
before
|
566
|
+
before do
|
568
567
|
record.phone_number = phone_number
|
569
568
|
record.country_code = 'DE'
|
570
569
|
record.valid? # run the empty validation chain to execute the before hook (normalized the number)
|
571
|
-
|
570
|
+
end
|
572
571
|
|
573
572
|
context 'when the country_code attribute does not match the country number' do
|
574
573
|
context 'when the number is prefixed with a country number and a plus' do
|
@@ -101,25 +101,25 @@ end
|
|
101
101
|
#--------------------
|
102
102
|
class AustralianHelpfulHome < ActiveRecord::Base
|
103
103
|
attr_accessor :phone_number
|
104
|
-
validates_plausible_phone :phone_number, country_number:
|
104
|
+
validates_plausible_phone :phone_number, country_number: '61'
|
105
105
|
end
|
106
106
|
|
107
107
|
#--------------------
|
108
108
|
class PolishHelpfulHome < ActiveRecord::Base
|
109
109
|
attr_accessor :phone_number
|
110
|
-
validates_plausible_phone :phone_number, country_code:
|
110
|
+
validates_plausible_phone :phone_number, country_code: 'PL'
|
111
111
|
end
|
112
112
|
|
113
113
|
#--------------------
|
114
114
|
class BigHelpfulHome < ActiveRecord::Base
|
115
115
|
attr_accessor :phone_number
|
116
|
-
validates_plausible_phone :phone_number, presence: true, with: /\A\+\d+/, country_number:
|
116
|
+
validates_plausible_phone :phone_number, presence: true, with: /\A\+\d+/, country_number: '33'
|
117
117
|
end
|
118
118
|
|
119
119
|
#--------------------
|
120
120
|
class MismatchedHelpfulHome < ActiveRecord::Base
|
121
121
|
attr_accessor :phone_number, :country_code
|
122
|
-
validates :phone_number, phony_plausible: {ignore_record_country_code: true}
|
122
|
+
validates :phone_number, phony_plausible: { ignore_record_country_code: true }
|
123
123
|
end
|
124
124
|
|
125
125
|
#--------------------
|
@@ -129,7 +129,7 @@ class InvalidCountryCodeHelpfulHome < ActiveRecord::Base
|
|
129
129
|
validates_plausible_phone :phone_number
|
130
130
|
|
131
131
|
def country_code
|
132
|
-
|
132
|
+
'--'
|
133
133
|
end
|
134
134
|
end
|
135
135
|
#-----------------------------------------------------------------------------------------------------------------------
|
@@ -137,65 +137,63 @@ end
|
|
137
137
|
#-----------------------------------------------------------------------------------------------------------------------
|
138
138
|
|
139
139
|
I18n.locale = :en
|
140
|
-
VALID_NUMBER = '1 555 555 5555'
|
141
|
-
NORMALIZABLE_NUMBER = '555 555 5555'
|
142
|
-
AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '61390133997'
|
143
|
-
POLISH_NUMBER_WITH_COUNTRY_CODE = '48600600600'
|
144
|
-
FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '+61 390133997'
|
145
|
-
FRENCH_NUMBER_WITH_COUNTRY_CODE = '33627899541'
|
146
|
-
FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE = '+33 627899541'
|
147
|
-
INVALID_NUMBER = '123456789 123456789 123456789 123456789'
|
148
|
-
NOT_A_NUMBER = 'HAHA'
|
149
|
-
JAPAN_COUNTRY = 'jp'
|
140
|
+
VALID_NUMBER = '1 555 555 5555'.freeze
|
141
|
+
NORMALIZABLE_NUMBER = '555 555 5555'.freeze
|
142
|
+
AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '61390133997'.freeze
|
143
|
+
POLISH_NUMBER_WITH_COUNTRY_CODE = '48600600600'.freeze
|
144
|
+
FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '+61 390133997'.freeze
|
145
|
+
FRENCH_NUMBER_WITH_COUNTRY_CODE = '33627899541'.freeze
|
146
|
+
FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE = '+33 627899541'.freeze
|
147
|
+
INVALID_NUMBER = '123456789 123456789 123456789 123456789'.freeze
|
148
|
+
NOT_A_NUMBER = 'HAHA'.freeze
|
149
|
+
JAPAN_COUNTRY = 'jp'.freeze
|
150
150
|
|
151
151
|
#-----------------------------------------------------------------------------------------------------------------------
|
152
152
|
describe PhonyPlausibleValidator do
|
153
|
-
|
154
153
|
#--------------------
|
155
154
|
describe '#validates' do
|
156
|
-
|
157
155
|
before(:each) do
|
158
156
|
@home = SimpleHome.new
|
159
157
|
end
|
160
158
|
|
161
|
-
it
|
159
|
+
it 'should validate an empty number' do
|
162
160
|
expect(@home).to be_valid
|
163
161
|
end
|
164
162
|
|
165
|
-
it
|
163
|
+
it 'should validate a valid number' do
|
166
164
|
@home.phone_number = VALID_NUMBER
|
167
165
|
expect(@home).to be_valid
|
168
166
|
end
|
169
167
|
|
170
|
-
it
|
168
|
+
it 'should invalidate an invalid number' do
|
171
169
|
@home.phone_number = INVALID_NUMBER
|
172
170
|
expect(@home).to_not be_valid
|
173
|
-
expect(@home.errors.messages).to include(phone_number: [
|
171
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
174
172
|
end
|
175
173
|
|
176
|
-
it
|
174
|
+
it 'should invalidate not a number' do
|
177
175
|
@home.phone_number = NOT_A_NUMBER
|
178
176
|
expect(@home).to_not be_valid
|
179
|
-
expect(@home.errors.messages).to include(phone_number: [
|
177
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
180
178
|
end
|
181
179
|
|
182
|
-
it
|
180
|
+
it 'should translate the error message in Dutch' do
|
183
181
|
I18n.with_locale(:nl) do
|
184
182
|
@home.phone_number = INVALID_NUMBER
|
185
183
|
@home.valid?
|
186
|
-
expect(@home.errors.messages).to include(phone_number: [
|
184
|
+
expect(@home.errors.messages).to include(phone_number: ['is geen geldig nummer'])
|
187
185
|
end
|
188
186
|
end
|
189
187
|
|
190
|
-
it
|
188
|
+
it 'should translate the error message in English' do
|
191
189
|
I18n.with_locale(:en) do
|
192
190
|
@home.phone_number = INVALID_NUMBER
|
193
191
|
@home.valid?
|
194
|
-
expect(@home.errors.messages).to include(phone_number: [
|
192
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
195
193
|
end
|
196
194
|
end
|
197
195
|
|
198
|
-
it
|
196
|
+
it 'should translate the error message in French' do
|
199
197
|
I18n.with_locale(:fr) do
|
200
198
|
@home.phone_number = INVALID_NUMBER
|
201
199
|
@home.valid?
|
@@ -203,7 +201,7 @@ describe PhonyPlausibleValidator do
|
|
203
201
|
end
|
204
202
|
end
|
205
203
|
|
206
|
-
it
|
204
|
+
it 'should translate the error message in Japanese' do
|
207
205
|
I18n.with_locale(:ja) do
|
208
206
|
@home.phone_number = INVALID_NUMBER
|
209
207
|
@home.valid?
|
@@ -211,7 +209,7 @@ describe PhonyPlausibleValidator do
|
|
211
209
|
end
|
212
210
|
end
|
213
211
|
|
214
|
-
it
|
212
|
+
it 'should translate the error message in Khmer' do
|
215
213
|
I18n.with_locale(:km) do
|
216
214
|
@home.phone_number = INVALID_NUMBER
|
217
215
|
@home.valid?
|
@@ -219,7 +217,7 @@ describe PhonyPlausibleValidator do
|
|
219
217
|
end
|
220
218
|
end
|
221
219
|
|
222
|
-
it
|
220
|
+
it 'should translate the error message in Ukrainian' do
|
223
221
|
I18n.with_locale(:uk) do
|
224
222
|
@home.phone_number = INVALID_NUMBER
|
225
223
|
@home.valid?
|
@@ -227,7 +225,7 @@ describe PhonyPlausibleValidator do
|
|
227
225
|
end
|
228
226
|
end
|
229
227
|
|
230
|
-
it
|
228
|
+
it 'should translate the error message in Russian' do
|
231
229
|
I18n.with_locale(:ru) do
|
232
230
|
@home.phone_number = INVALID_NUMBER
|
233
231
|
@home.valid?
|
@@ -239,254 +237,234 @@ end
|
|
239
237
|
|
240
238
|
#-----------------------------------------------------------------------------------------------------------------------
|
241
239
|
describe ActiveModel::Validations::HelperMethods do
|
242
|
-
|
243
240
|
#--------------------
|
244
241
|
describe '#validates_plausible_phone' do
|
245
|
-
|
246
242
|
#--------------------
|
247
243
|
context 'when a number is optional' do
|
248
|
-
|
249
244
|
before(:each) do
|
250
245
|
@home = HelpfulHome.new
|
251
246
|
end
|
252
247
|
|
253
|
-
it
|
248
|
+
it 'should validate an empty number' do
|
254
249
|
expect(@home).to be_valid
|
255
250
|
end
|
256
251
|
|
257
|
-
it
|
252
|
+
it 'should validate a valid number' do
|
258
253
|
@home.phone_number = VALID_NUMBER
|
259
254
|
expect(@home).to be_valid
|
260
255
|
end
|
261
256
|
|
262
|
-
it
|
257
|
+
it 'should invalidate an invalid number' do
|
263
258
|
@home.phone_number = INVALID_NUMBER
|
264
259
|
expect(@home).to_not be_valid
|
265
|
-
expect(@home.errors.messages).to include(phone_number: [
|
260
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
266
261
|
end
|
267
|
-
|
268
262
|
end
|
269
263
|
|
270
264
|
#--------------------
|
271
265
|
context 'when a number is required (:presence = true)' do
|
272
|
-
|
273
266
|
before(:each) do
|
274
267
|
@home = RequiredHelpfulHome.new
|
275
268
|
end
|
276
269
|
|
277
|
-
it
|
270
|
+
it 'should invalidate an empty number' do
|
278
271
|
expect(@home).to_not be_valid
|
279
272
|
expect(@home.errors.messages).to include(phone_number: ["can't be blank"])
|
280
273
|
end
|
281
274
|
|
282
|
-
it
|
275
|
+
it 'should validate a valid number' do
|
283
276
|
@home.phone_number = VALID_NUMBER
|
284
277
|
expect(@home).to be_valid
|
285
278
|
end
|
286
279
|
|
287
|
-
it
|
280
|
+
it 'should invalidate an invalid number' do
|
288
281
|
@home.phone_number = INVALID_NUMBER
|
289
282
|
expect(@home).to_not be_valid
|
290
|
-
expect(@home.errors.messages).to include(phone_number: [
|
283
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
291
284
|
end
|
292
|
-
|
293
285
|
end
|
294
286
|
|
295
287
|
#--------------------
|
296
288
|
context 'when a number is not required (!presence = false)' do
|
297
|
-
|
298
289
|
before(:each) do
|
299
290
|
@home = OptionalHelpfulHome.new
|
300
291
|
end
|
301
292
|
|
302
|
-
it
|
293
|
+
it 'should validate an empty number' do
|
303
294
|
expect(@home).to be_valid
|
304
295
|
end
|
305
296
|
|
306
|
-
it
|
297
|
+
it 'should validate a valid number' do
|
307
298
|
@home.phone_number = VALID_NUMBER
|
308
299
|
expect(@home).to be_valid
|
309
300
|
end
|
310
301
|
|
311
|
-
it
|
302
|
+
it 'should invalidate an invalid number' do
|
312
303
|
@home.phone_number = INVALID_NUMBER
|
313
304
|
expect(@home).to_not be_valid
|
314
|
-
expect(@home.errors.messages).to include(phone_number: [
|
305
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
315
306
|
end
|
316
|
-
|
317
307
|
end
|
318
308
|
|
319
309
|
#--------------------
|
320
310
|
context 'when a number must be formatted (:with)' do
|
321
|
-
|
322
311
|
before(:each) do
|
323
312
|
@home = FormattedHelpfulHome.new
|
324
313
|
end
|
325
314
|
|
326
|
-
it
|
315
|
+
it 'should invalidate an empty number' do
|
327
316
|
expect(@home).to_not be_valid
|
328
|
-
expect(@home.errors.messages).to include(phone_number: [
|
317
|
+
expect(@home.errors.messages).to include(phone_number: ['is invalid'])
|
329
318
|
end
|
330
319
|
|
331
|
-
it
|
320
|
+
it 'should validate a well formatted valid number' do
|
332
321
|
@home.phone_number = "+#{VALID_NUMBER}"
|
333
322
|
expect(@home).to be_valid
|
334
323
|
end
|
335
324
|
|
336
|
-
it
|
325
|
+
it 'should invalidate a bad formatted valid number' do
|
337
326
|
@home.phone_number = VALID_NUMBER
|
338
327
|
expect(@home).to_not be_valid
|
339
|
-
expect(@home.errors.messages).to include(phone_number: [
|
328
|
+
expect(@home.errors.messages).to include(phone_number: ['is invalid'])
|
340
329
|
end
|
341
|
-
|
342
330
|
end
|
343
331
|
|
344
332
|
#--------------------
|
345
333
|
context 'when a number must not be formatted (:without)' do
|
346
|
-
|
347
334
|
before(:each) do
|
348
335
|
@home = NotFormattedHelpfulHome.new
|
349
336
|
end
|
350
337
|
|
351
|
-
it
|
338
|
+
it 'should validate an empty number' do
|
352
339
|
expect(@home).to be_valid
|
353
340
|
end
|
354
341
|
|
355
|
-
it
|
342
|
+
it 'should validate a well formatted valid number' do
|
356
343
|
@home.phone_number = VALID_NUMBER
|
357
344
|
expect(@home).to be_valid
|
358
345
|
end
|
359
346
|
|
360
|
-
it
|
347
|
+
it 'should invalidate a bad formatted valid number' do
|
361
348
|
@home.phone_number = "+#{VALID_NUMBER}"
|
362
349
|
expect(@home).to_not be_valid
|
363
|
-
expect(@home.errors.messages).to include(phone_number: [
|
350
|
+
expect(@home.errors.messages).to include(phone_number: ['is invalid'])
|
364
351
|
end
|
365
|
-
|
366
352
|
end
|
367
353
|
|
368
354
|
#--------------------
|
369
355
|
context 'when a number must include a specific country number' do
|
370
|
-
|
371
356
|
before(:each) do
|
372
357
|
@home = AustralianHelpfulHome.new
|
373
358
|
end
|
374
359
|
|
375
|
-
it
|
360
|
+
it 'should validate an empty number' do
|
376
361
|
expect(@home).to be_valid
|
377
362
|
end
|
378
363
|
|
379
|
-
it
|
364
|
+
it 'should validate a valid number with the right country code' do
|
380
365
|
@home.phone_number = AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE
|
381
366
|
expect(@home).to be_valid
|
382
367
|
end
|
383
368
|
|
384
|
-
it
|
369
|
+
it 'should invalidate a valid number with the wrong country code' do
|
385
370
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
386
371
|
expect(@home).to_not be_valid
|
387
|
-
expect(@home.errors.messages).to include(phone_number: [
|
372
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
388
373
|
end
|
389
374
|
|
390
|
-
it
|
375
|
+
it 'should invalidate a valid number without a country code' do
|
391
376
|
@home.phone_number = VALID_NUMBER
|
392
377
|
expect(@home).to_not be_valid
|
393
|
-
expect(@home.errors.messages).to include(phone_number: [
|
378
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
394
379
|
end
|
395
|
-
|
396
380
|
end
|
397
381
|
|
398
382
|
#--------------------
|
399
383
|
context 'when a number must be validated after normalization' do
|
400
|
-
|
401
384
|
before(:each) do
|
402
385
|
@home = NormalizableHelpfulHome.new
|
403
386
|
end
|
404
387
|
|
405
|
-
it
|
388
|
+
it 'should validate an empty number' do
|
406
389
|
expect(@home).to be_valid
|
407
390
|
end
|
408
391
|
|
409
|
-
it
|
392
|
+
it 'should validate a valid number' do
|
410
393
|
@home.phone_number = VALID_NUMBER
|
411
394
|
expect(@home).to be_valid
|
412
395
|
end
|
413
396
|
|
414
|
-
it
|
397
|
+
it 'should validate a normalizable number' do
|
415
398
|
@home.phone_number = NORMALIZABLE_NUMBER
|
416
399
|
expect(@home).to be_valid
|
417
400
|
end
|
418
401
|
|
419
|
-
it
|
402
|
+
it 'should invalidate an invalid number' do
|
420
403
|
@home.phone_number = INVALID_NUMBER
|
421
404
|
expect(@home).to_not be_valid
|
422
|
-
expect(@home.errors.messages).to include(phone_number: [
|
405
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
423
406
|
end
|
424
|
-
|
425
407
|
end
|
426
408
|
|
427
409
|
#--------------------
|
428
410
|
context 'when a number must include a specific country code' do
|
429
|
-
|
430
411
|
before(:each) do
|
431
412
|
@home = PolishHelpfulHome.new
|
432
413
|
end
|
433
414
|
|
434
|
-
it
|
415
|
+
it 'should validate an empty number' do
|
435
416
|
expect(@home).to be_valid
|
436
417
|
end
|
437
418
|
|
438
|
-
it
|
419
|
+
it 'should validate a valid number with the right country code' do
|
439
420
|
@home.phone_number = POLISH_NUMBER_WITH_COUNTRY_CODE
|
440
421
|
expect(@home).to be_valid
|
441
422
|
end
|
442
423
|
|
443
|
-
it
|
424
|
+
it 'should invalidate a valid number with the wrong country code' do
|
444
425
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
445
426
|
expect(@home).to_not be_valid
|
446
|
-
expect(@home.errors.messages).to include(phone_number: [
|
427
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
447
428
|
end
|
448
429
|
|
449
|
-
it
|
430
|
+
it 'should invalidate a valid number without a country code' do
|
450
431
|
@home.phone_number = VALID_NUMBER
|
451
432
|
expect(@home).to_not be_valid
|
452
|
-
expect(@home.errors.messages).to include(phone_number: [
|
433
|
+
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
|
453
434
|
end
|
454
|
-
|
455
435
|
end
|
456
436
|
|
457
437
|
context 'when lots of things are being validated simultaneously' do
|
458
|
-
|
459
438
|
before(:each) do
|
460
439
|
@home = BigHelpfulHome.new
|
461
440
|
end
|
462
441
|
|
463
|
-
it
|
442
|
+
it 'should invalidate an empty number' do
|
464
443
|
expect(@home).to_not be_valid
|
465
444
|
end
|
466
445
|
|
467
|
-
it
|
446
|
+
it 'should invalidate an invalid number' do
|
468
447
|
@home.phone_number = INVALID_NUMBER
|
469
448
|
expect(@home).to_not be_valid
|
470
|
-
expect(@home.errors.messages[:phone_number]).to include
|
449
|
+
expect(@home.errors.messages[:phone_number]).to include 'is an invalid number'
|
471
450
|
end
|
472
451
|
|
473
|
-
it
|
452
|
+
it 'should invalidate a badly formatted number with the right country code' do
|
474
453
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
475
454
|
expect(@home).to_not be_valid
|
476
|
-
expect(@home.errors.messages[:phone_number]).to include
|
455
|
+
expect(@home.errors.messages[:phone_number]).to include 'is invalid'
|
477
456
|
end
|
478
457
|
|
479
|
-
it
|
458
|
+
it 'should invalidate a properly formatted number with the wrong country code' do
|
480
459
|
@home.phone_number = FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE
|
481
460
|
expect(@home).to_not be_valid
|
482
|
-
expect(@home.errors.messages[:phone_number]).to include
|
461
|
+
expect(@home.errors.messages[:phone_number]).to include 'is an invalid number'
|
483
462
|
end
|
484
463
|
|
485
|
-
it
|
464
|
+
it 'should validate a properly formatted number with the right country code' do
|
486
465
|
@home.phone_number = FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE
|
487
466
|
expect(@home).to be_valid
|
488
467
|
end
|
489
|
-
|
490
468
|
end
|
491
469
|
|
492
470
|
#--------------------
|
@@ -497,34 +475,32 @@ describe ActiveModel::Validations::HelperMethods do
|
|
497
475
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
498
476
|
end
|
499
477
|
|
500
|
-
it
|
478
|
+
it 'should allow this number' do
|
501
479
|
expect(@home).to be_valid
|
502
480
|
end
|
503
481
|
end
|
504
482
|
|
505
|
-
|
483
|
+
#--------------------
|
506
484
|
context 'when a country code is set to something invalid' do
|
507
485
|
before(:each) do
|
508
486
|
@home = InvalidCountryCodeHelpfulHome.new
|
509
487
|
end
|
510
488
|
|
511
|
-
it
|
489
|
+
it 'should allow any valid number' do
|
512
490
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
513
491
|
expect(@home).to be_valid
|
514
492
|
end
|
515
493
|
|
516
|
-
it
|
494
|
+
it 'should not allow any invalid number' do
|
517
495
|
@home.phone_number = INVALID_NUMBER
|
518
496
|
expect(@home).to_not be_valid
|
519
497
|
end
|
520
498
|
|
521
|
-
it
|
522
|
-
expect
|
499
|
+
it 'should not raise a NoMethodError when looking up a country fails (Regression)' do
|
500
|
+
expect do
|
523
501
|
@home.valid?
|
524
|
-
|
502
|
+
end.to_not raise_error
|
525
503
|
end
|
526
504
|
end
|
527
|
-
|
528
505
|
end
|
529
|
-
|
530
506
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phony_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.13.
|
4
|
+
version: 0.13.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joost Hietbrink
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phony
|
@@ -76,6 +76,8 @@ extra_rdoc_files: []
|
|
76
76
|
files:
|
77
77
|
- ".gitignore"
|
78
78
|
- ".rspec"
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- ".rubocop_todo.yml"
|
79
81
|
- ".travis.yml"
|
80
82
|
- CHANGELOG.md
|
81
83
|
- Gemfile
|