phony_rails 0.12.11 → 0.12.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/CHANGELOG.md +20 -2
- data/Gemfile.lock +31 -31
- data/README.md +1 -1
- data/lib/data/country_codes.yaml +2 -0
- data/lib/phony_rails.rb +10 -12
- data/lib/phony_rails/locales/nl.yml +4 -0
- data/lib/phony_rails/version.rb +1 -1
- data/spec/lib/phony_rails_spec.rb +171 -163
- data/spec/lib/validators/phony_validator_spec.rb +8 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 97008ef55c4d1d7748babd4150f0cd8ff7cabaeb
|
|
4
|
+
data.tar.gz: 37045f7383c7811809ba99f151dd133448c4967c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 39dc96431aca90966b4d79f6441213cbe6e1fbd920a86e50b1fe10f49606fbe020bb4ae557b0edec411b4a8e15814d3f15e8e102c595b63c6eabf2790596a130
|
|
7
|
+
data.tar.gz: 73b3c1c05704b4dd21e442888eedd2550688d5d6bb2dbe68475f29671bdff61228f2d0522c919ce8744781ff46060e79eb47c6d5b06bfb523f9d01de9757368c
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
## [
|
|
3
|
+
## [v0.12.11](https://github.com/joost/phony_rails/tree/v0.12.11) (2015-11-12)
|
|
4
4
|
|
|
5
|
-
[Full Changelog](https://github.com/joost/phony_rails/compare/v0.12.9...
|
|
5
|
+
[Full Changelog](https://github.com/joost/phony_rails/compare/v0.12.9...v0.12.11)
|
|
6
6
|
|
|
7
7
|
**Closed issues:**
|
|
8
8
|
|
|
9
|
+
- French formatting [\#121](https://github.com/joost/phony_rails/issues/121)
|
|
10
|
+
|
|
11
|
+
- French phony\_normalize [\#120](https://github.com/joost/phony_rails/issues/120)
|
|
12
|
+
|
|
13
|
+
- Correct phone number failed the validatiton [\#115](https://github.com/joost/phony_rails/issues/115)
|
|
14
|
+
|
|
15
|
+
- 'no implicit conversion of nil into String' from phony\_formatted!\(spaces: '-', strict: true\) with invalid numbers [\#113](https://github.com/joost/phony_rails/issues/113)
|
|
16
|
+
|
|
9
17
|
- Can i skip a validation with another number? [\#110](https://github.com/joost/phony_rails/issues/110)
|
|
10
18
|
|
|
11
19
|
- Consider dropping the dependency on the countries gem and using a YAML file instead [\#108](https://github.com/joost/phony_rails/issues/108)
|
|
12
20
|
|
|
21
|
+
- Some Finnish mobile numbers are formatted wrong [\#107](https://github.com/joost/phony_rails/issues/107)
|
|
22
|
+
|
|
13
23
|
- undefined method `\[\]' for Data:Class [\#106](https://github.com/joost/phony_rails/issues/106)
|
|
14
24
|
|
|
25
|
+
- Phony is out of date [\#102](https://github.com/joost/phony_rails/issues/102)
|
|
26
|
+
|
|
15
27
|
**Merged pull requests:**
|
|
16
28
|
|
|
29
|
+
- Update readme [\#117](https://github.com/joost/phony_rails/pull/117) ([toydestroyer](https://github.com/toydestroyer))
|
|
30
|
+
|
|
31
|
+
- Add uk, ru error message translations [\#114](https://github.com/joost/phony_rails/pull/114) ([shhavel](https://github.com/shhavel))
|
|
32
|
+
|
|
33
|
+
- Update phony\_rails.gemspec [\#112](https://github.com/joost/phony_rails/pull/112) ([Agsiegert](https://github.com/Agsiegert))
|
|
34
|
+
|
|
17
35
|
- Don't re-parse country codes YAML file every time it's needed. [\#111](https://github.com/joost/phony_rails/pull/111) ([jcoleman](https://github.com/jcoleman))
|
|
18
36
|
|
|
19
37
|
- Replace countries dependency with YAML file [\#109](https://github.com/joost/phony_rails/pull/109) ([monfresh](https://github.com/monfresh))
|
data/Gemfile.lock
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
phony_rails (0.12.
|
|
4
|
+
phony_rails (0.12.12)
|
|
5
5
|
activesupport (>= 3.0)
|
|
6
6
|
phony (~> 2.12)
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
activemodel (4.2.5)
|
|
12
|
-
activesupport (= 4.2.5)
|
|
11
|
+
activemodel (4.2.5.1)
|
|
12
|
+
activesupport (= 4.2.5.1)
|
|
13
13
|
builder (~> 3.1)
|
|
14
|
-
activerecord (4.2.5)
|
|
15
|
-
activemodel (= 4.2.5)
|
|
16
|
-
activesupport (= 4.2.5)
|
|
14
|
+
activerecord (4.2.5.1)
|
|
15
|
+
activemodel (= 4.2.5.1)
|
|
16
|
+
activesupport (= 4.2.5.1)
|
|
17
17
|
arel (~> 6.0)
|
|
18
|
-
activesupport (4.2.5)
|
|
18
|
+
activesupport (4.2.5.1)
|
|
19
19
|
i18n (~> 0.7)
|
|
20
20
|
json (~> 1.7, >= 1.7.7)
|
|
21
21
|
minitest (~> 5.1)
|
|
22
22
|
thread_safe (~> 0.3, >= 0.3.4)
|
|
23
23
|
tzinfo (~> 1.1)
|
|
24
24
|
arel (6.0.3)
|
|
25
|
-
bson (
|
|
25
|
+
bson (4.0.1)
|
|
26
26
|
builder (3.2.2)
|
|
27
27
|
coderay (1.1.0)
|
|
28
|
-
coveralls (0.8.
|
|
28
|
+
coveralls (0.8.10)
|
|
29
29
|
json (~> 1.8)
|
|
30
30
|
rest-client (>= 1.6.8, < 2)
|
|
31
|
-
simplecov (~> 0.
|
|
31
|
+
simplecov (~> 0.11.0)
|
|
32
32
|
term-ansicolor (~> 1.3)
|
|
33
33
|
thor (~> 0.19.1)
|
|
34
34
|
tins (~> 1.6.0)
|
|
35
35
|
diff-lcs (1.2.5)
|
|
36
36
|
docile (1.1.5)
|
|
37
|
-
domain_name (0.5.
|
|
37
|
+
domain_name (0.5.20160128)
|
|
38
38
|
unf (>= 0.0.5, < 1.0.0)
|
|
39
39
|
ffi (1.9.10)
|
|
40
40
|
formatador (0.2.5)
|
|
@@ -60,34 +60,34 @@ GEM
|
|
|
60
60
|
domain_name (~> 0.5)
|
|
61
61
|
i18n (0.7.0)
|
|
62
62
|
json (1.8.3)
|
|
63
|
-
listen (3.0.
|
|
63
|
+
listen (3.0.6)
|
|
64
64
|
rb-fsevent (>= 0.9.3)
|
|
65
|
-
rb-inotify (>= 0.9)
|
|
66
|
-
lumberjack (1.0.
|
|
65
|
+
rb-inotify (>= 0.9.7)
|
|
66
|
+
lumberjack (1.0.10)
|
|
67
67
|
method_source (0.8.2)
|
|
68
|
-
mime-types (2.
|
|
69
|
-
minitest (5.8.
|
|
70
|
-
mongo (2.
|
|
71
|
-
bson (~>
|
|
72
|
-
mongoid (5.0
|
|
68
|
+
mime-types (2.99)
|
|
69
|
+
minitest (5.8.4)
|
|
70
|
+
mongo (2.2.2)
|
|
71
|
+
bson (~> 4.0)
|
|
72
|
+
mongoid (5.1.0)
|
|
73
73
|
activemodel (~> 4.0)
|
|
74
74
|
mongo (~> 2.1)
|
|
75
|
-
origin (~> 2.
|
|
75
|
+
origin (~> 2.2)
|
|
76
76
|
tzinfo (>= 0.3.37)
|
|
77
|
-
nenv (0.
|
|
77
|
+
nenv (0.3.0)
|
|
78
78
|
netrc (0.11.0)
|
|
79
79
|
notiffany (0.0.8)
|
|
80
80
|
nenv (~> 0.1)
|
|
81
81
|
shellany (~> 0.0)
|
|
82
|
-
origin (2.
|
|
83
|
-
phony (2.15.
|
|
82
|
+
origin (2.2.0)
|
|
83
|
+
phony (2.15.15)
|
|
84
84
|
pry (0.10.3)
|
|
85
85
|
coderay (~> 1.1.0)
|
|
86
86
|
method_source (~> 0.8.1)
|
|
87
87
|
slop (~> 3.4)
|
|
88
|
-
rake (10.
|
|
89
|
-
rb-fsevent (0.9.
|
|
90
|
-
rb-inotify (0.9.
|
|
88
|
+
rake (10.5.0)
|
|
89
|
+
rb-fsevent (0.9.7)
|
|
90
|
+
rb-inotify (0.9.7)
|
|
91
91
|
ffi (>= 0.5.0)
|
|
92
92
|
rest-client (1.8.0)
|
|
93
93
|
http-cookie (>= 1.0.2, < 2.0)
|
|
@@ -97,17 +97,17 @@ GEM
|
|
|
97
97
|
rspec-core (~> 3.4.0)
|
|
98
98
|
rspec-expectations (~> 3.4.0)
|
|
99
99
|
rspec-mocks (~> 3.4.0)
|
|
100
|
-
rspec-core (3.4.
|
|
100
|
+
rspec-core (3.4.2)
|
|
101
101
|
rspec-support (~> 3.4.0)
|
|
102
102
|
rspec-expectations (3.4.0)
|
|
103
103
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
104
104
|
rspec-support (~> 3.4.0)
|
|
105
|
-
rspec-mocks (3.4.
|
|
105
|
+
rspec-mocks (3.4.1)
|
|
106
106
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
107
107
|
rspec-support (~> 3.4.0)
|
|
108
|
-
rspec-support (3.4.
|
|
108
|
+
rspec-support (3.4.1)
|
|
109
109
|
shellany (0.0.1)
|
|
110
|
-
simplecov (0.
|
|
110
|
+
simplecov (0.11.2)
|
|
111
111
|
docile (~> 1.1.0)
|
|
112
112
|
json (~> 1.8)
|
|
113
113
|
simplecov-html (~> 0.10.0)
|
|
@@ -123,7 +123,7 @@ GEM
|
|
|
123
123
|
thread_safe (~> 0.1)
|
|
124
124
|
unf (0.1.4)
|
|
125
125
|
unf_ext
|
|
126
|
-
unf_ext (0.0.7.
|
|
126
|
+
unf_ext (0.0.7.2)
|
|
127
127
|
|
|
128
128
|
PLATFORMS
|
|
129
129
|
ruby
|
data/README.md
CHANGED
|
@@ -105,7 +105,7 @@ validates_plausible_phone :phone_number, without: /^\+\d+/
|
|
|
105
105
|
validates_plausible_phone :phone_number, presence: true, with: /^\+\d+/
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
-
the i18n key is `:improbable_phone`. Languages supported by default: de, en, fr, it, ja, kh, tr, ua and ru.
|
|
108
|
+
the i18n key is `:improbable_phone`. Languages supported by default: de, en, fr, it, ja, kh, nl, tr, ua and ru.
|
|
109
109
|
|
|
110
110
|
You can also validate if a number has the correct country number:
|
|
111
111
|
|
data/lib/data/country_codes.yaml
CHANGED
data/lib/phony_rails.rb
CHANGED
|
@@ -5,7 +5,6 @@ require 'phony_rails/version'
|
|
|
5
5
|
require 'yaml'
|
|
6
6
|
|
|
7
7
|
module PhonyRails
|
|
8
|
-
|
|
9
8
|
def self.country_number_for(country_code)
|
|
10
9
|
return if country_code.nil?
|
|
11
10
|
|
|
@@ -34,7 +33,7 @@ module PhonyRails
|
|
|
34
33
|
if _country_number = options[:country_number] || country_number_for(options[:country_code])
|
|
35
34
|
options[:add_plus] = true if options[:add_plus].nil?
|
|
36
35
|
# (Force) add country_number if missing
|
|
37
|
-
# NOTE: do we need to force adding country code? Otherwise we can share
|
|
36
|
+
# NOTE: do we need to force adding country code? Otherwise we can share logic with next block
|
|
38
37
|
if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
|
|
39
38
|
number = "#{_country_number}#{number}"
|
|
40
39
|
end
|
|
@@ -42,7 +41,7 @@ module PhonyRails
|
|
|
42
41
|
options[:add_plus] = true if options[:add_plus].nil?
|
|
43
42
|
# We try to add the default country number and see if it is a
|
|
44
43
|
# correct phone number. See https://github.com/joost/phony_rails/issues/87#issuecomment-89324426
|
|
45
|
-
|
|
44
|
+
unless number =~ /\A\+/ # if we don't have a +
|
|
46
45
|
if Phony.plausible?("#{_default_country_number}#{number}") || !Phony.plausible?(number) || country_code_from_number(number).nil?
|
|
47
46
|
number = "#{_default_country_number}#{number}"
|
|
48
47
|
elsif (number =~ /^0[^0]/) && Phony.plausible?("#{_default_country_number}#{number.gsub(/^0/, '')}")
|
|
@@ -72,7 +71,7 @@ module PhonyRails
|
|
|
72
71
|
return false if number.nil? || number.blank?
|
|
73
72
|
number = normalize_number(number, options)
|
|
74
73
|
country_number = options[:country_number] || country_number_for(options[:country_code]) ||
|
|
75
|
-
|
|
74
|
+
options[:default_country_number] || country_number_for(options[:default_country_code])
|
|
76
75
|
Phony.plausible? number, cc: country_number
|
|
77
76
|
rescue
|
|
78
77
|
false
|
|
@@ -94,14 +93,13 @@ module PhonyRails
|
|
|
94
93
|
end
|
|
95
94
|
attributes.each do |attribute|
|
|
96
95
|
attribute_name = options[:as] || attribute
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
fail(RuntimeError, "No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
|
|
97
|
+
send("#{attribute_name}=", PhonyRails.normalize_number(send(attribute), options))
|
|
99
98
|
end
|
|
100
99
|
end
|
|
101
100
|
end
|
|
102
101
|
|
|
103
102
|
module ClassMethods
|
|
104
|
-
|
|
105
103
|
# Use this method on the class level like:
|
|
106
104
|
# phony_normalize :phone_number, :fax_number, :default_country_code => 'NL'
|
|
107
105
|
#
|
|
@@ -117,7 +115,7 @@ module PhonyRails
|
|
|
117
115
|
options[:enforce_record_country] = true if options[:enforce_record_country].nil?
|
|
118
116
|
|
|
119
117
|
# Add before validation that saves a normalized version of the phone number
|
|
120
|
-
|
|
118
|
+
before_validation do
|
|
121
119
|
set_phony_normalized_numbers(attributes, options)
|
|
122
120
|
end
|
|
123
121
|
end
|
|
@@ -129,12 +127,12 @@ module PhonyRails
|
|
|
129
127
|
main_options = attributes.last.is_a?(Hash) ? attributes.pop : {}
|
|
130
128
|
main_options.assert_valid_keys :country_code, :default_country_code
|
|
131
129
|
attributes.each do |attribute|
|
|
132
|
-
|
|
130
|
+
fail(StandardError, "Instance method normalized_#{attribute} already exists on #{self.name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
|
|
133
131
|
define_method :"normalized_#{attribute}" do |*args|
|
|
134
132
|
options = args.first || {}
|
|
135
|
-
|
|
136
|
-
options[:country_code] ||=
|
|
137
|
-
PhonyRails.normalize_number(
|
|
133
|
+
fail(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless self.respond_to?(attribute)
|
|
134
|
+
options[:country_code] ||= country_code if self.respond_to?(:country_code)
|
|
135
|
+
PhonyRails.normalize_number(send(attribute), main_options.merge(options))
|
|
138
136
|
end
|
|
139
137
|
end
|
|
140
138
|
end
|
data/lib/phony_rails/version.rb
CHANGED
|
@@ -1,115 +1,103 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe PhonyRails do
|
|
4
|
-
it
|
|
5
|
-
should_not be_const_defined
|
|
4
|
+
it 'should not pollute the global namespace with a Country class' do
|
|
5
|
+
should_not be_const_defined 'Country'
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
describe 'phony_format String extension' do
|
|
9
|
-
|
|
10
9
|
describe 'the phony_formatted method' do
|
|
11
|
-
|
|
12
10
|
it 'should not modify the original options Hash' do
|
|
13
11
|
options = {normalize: :NL, format: :international}
|
|
14
|
-
|
|
12
|
+
'0101234123'.phony_formatted(options)
|
|
15
13
|
expect(options).to eql({normalize: :NL, format: :international})
|
|
16
14
|
end
|
|
17
15
|
|
|
18
16
|
describe 'with the bang!' do
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
s = "0101234123"
|
|
17
|
+
it 'should change the String using the bang method' do
|
|
18
|
+
s = '0101234123'
|
|
22
19
|
expect(s.phony_formatted!(normalize: :NL, format: :international)).to eql('+31 10 123 4123')
|
|
23
|
-
expect(s).to eql(
|
|
20
|
+
expect(s).to eql('+31 10 123 4123')
|
|
24
21
|
end
|
|
25
|
-
|
|
26
22
|
end
|
|
27
23
|
|
|
28
24
|
describe 'with strict option' do
|
|
29
|
-
|
|
30
|
-
it "should return nil with non plausible number" do
|
|
25
|
+
it 'should return nil with non plausible number' do
|
|
31
26
|
number = '+319090' # not valid
|
|
32
27
|
expect(Phony.plausible?(number)).to be false
|
|
33
28
|
expect(number.phony_formatted(strict: true)).to eql(nil)
|
|
34
29
|
end
|
|
35
30
|
|
|
36
|
-
it
|
|
31
|
+
it 'should not return nil with plausible number' do
|
|
37
32
|
number = '+31101234123' # valid
|
|
38
33
|
expect(Phony.plausible?(number)).to be true
|
|
39
34
|
expect(number.phony_formatted(strict: true)).to_not eql(nil)
|
|
40
35
|
end
|
|
41
|
-
|
|
42
36
|
end
|
|
43
37
|
|
|
44
38
|
describe 'with normalize option' do
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
expect(
|
|
48
|
-
expect("101234123".phony_formatted(normalize: :NL, format: :international)).to eql('+31 10 123 4123')
|
|
39
|
+
it 'should phony_format' do
|
|
40
|
+
expect('101234123'.phony_formatted(normalize: :NL)).to eql('010 123 4123')
|
|
41
|
+
expect('101234123'.phony_formatted(normalize: :NL, format: :international)).to eql('+31 10 123 4123')
|
|
49
42
|
end
|
|
50
43
|
|
|
51
|
-
it
|
|
52
|
-
s =
|
|
44
|
+
it 'should not change original String' do
|
|
45
|
+
s = '0101234123'
|
|
53
46
|
expect(s.phony_formatted(normalize: :NL)).to eql('010 123 4123')
|
|
54
|
-
expect(s).to eql(
|
|
47
|
+
expect(s).to eql('0101234123')
|
|
55
48
|
end
|
|
56
49
|
|
|
57
|
-
it
|
|
58
|
-
expect(
|
|
50
|
+
it 'should phony_format String with country code' do
|
|
51
|
+
expect('31101234123'.phony_formatted(normalize: :NL)).to eql('010 123 4123')
|
|
59
52
|
end
|
|
60
53
|
|
|
61
|
-
it
|
|
62
|
-
expect(
|
|
54
|
+
it 'should phony_format String with country code' do
|
|
55
|
+
expect('31101234123'.phony_formatted(normalize: :NL)).to eql('010 123 4123')
|
|
63
56
|
end
|
|
64
57
|
|
|
65
|
-
it
|
|
66
|
-
expect(
|
|
58
|
+
it 'should accept strings with non-digits in it' do
|
|
59
|
+
expect('+31-10-1234123'.phony_formatted(normalize: :NL, format: :international, spaces: '-')).to eql('+31-10-123-4123')
|
|
67
60
|
end
|
|
68
61
|
|
|
69
|
-
it
|
|
70
|
-
expect(
|
|
62
|
+
it 'should phony_format String with country code different than normalized value' do
|
|
63
|
+
expect('+4790909090'.phony_formatted(normalize: :SE, format: :international)).to eql('+47 909 09 090')
|
|
71
64
|
end
|
|
72
|
-
|
|
73
65
|
end
|
|
74
66
|
|
|
75
67
|
describe 'with raise option' do
|
|
76
|
-
|
|
77
68
|
# https://github.com/joost/phony_rails/issues/79
|
|
78
|
-
|
|
79
69
|
context 'when raise is true' do
|
|
80
|
-
it
|
|
81
|
-
expect(lambda
|
|
70
|
+
it 'should raise the error' do
|
|
71
|
+
expect(lambda do
|
|
82
72
|
"8887716095".phony_formatted(format: :international, raise: true)
|
|
83
|
-
|
|
73
|
+
end).to raise_error(NoMethodError)
|
|
84
74
|
end
|
|
85
75
|
end
|
|
86
76
|
|
|
87
77
|
context 'when raise is false (default)' do
|
|
88
|
-
it
|
|
89
|
-
expect(
|
|
78
|
+
it 'should return original String on exception' do
|
|
79
|
+
expect('8887716095'.phony_formatted(format: :international)).to eq('8887716095')
|
|
90
80
|
end
|
|
91
81
|
end
|
|
92
|
-
|
|
93
82
|
end
|
|
94
83
|
|
|
95
|
-
describe
|
|
96
|
-
|
|
84
|
+
describe 'specific tests from issues' do
|
|
97
85
|
# https://github.com/joost/phony_rails/issues/79
|
|
98
|
-
it
|
|
99
|
-
expect(
|
|
86
|
+
it 'should pass Github issue #42' do
|
|
87
|
+
expect('8887716095'.phony_formatted(format: :international, normalize: 'US', raise: true)).to eq('+1 (888) 771-6095')
|
|
100
88
|
end
|
|
101
89
|
|
|
102
90
|
# https://github.com/joost/phony_rails/issues/42
|
|
103
|
-
it
|
|
91
|
+
it 'should pass Github issue #42' do
|
|
104
92
|
expect(PhonyRails.normalize_number("0606060606", default_country_code: 'FR')).to eq('+33606060606')
|
|
105
93
|
end
|
|
106
94
|
|
|
107
|
-
it
|
|
95
|
+
it 'should pass Github issue #85' do
|
|
108
96
|
expect(PhonyRails.normalize_number("47386160", default_country_code: 'NO')).to eq('+4747386160')
|
|
109
97
|
expect(PhonyRails.normalize_number("47386160", country_number: '47')).to eq('+4747386160')
|
|
110
98
|
end
|
|
111
99
|
|
|
112
|
-
it
|
|
100
|
+
it 'should pass Github issue #87' do
|
|
113
101
|
expect(PhonyRails.normalize_number('2318725305', country_code: 'US')).to eq('+12318725305')
|
|
114
102
|
expect(PhonyRails.normalize_number('2318725305', default_country_code: 'US')).to eq('+12318725305')
|
|
115
103
|
expect(PhonyRails.normalize_number('+2318725305', default_country_code: 'US')).to eq('+2318725305')
|
|
@@ -117,33 +105,33 @@ describe PhonyRails do
|
|
|
117
105
|
expect(PhonyRails.normalize_number('02318725305', default_country_code: 'US')).to eq('+12318725305')
|
|
118
106
|
end
|
|
119
107
|
|
|
120
|
-
it
|
|
108
|
+
it 'should pass Github issue #89' do
|
|
121
109
|
number = '+33 (0)6 87 36 18 75'
|
|
122
110
|
expect(Phony.plausible?(number)).to be true
|
|
123
111
|
expect(PhonyRails.normalize_number(number, country_code: 'FR')).to eq('+33687361875')
|
|
124
112
|
end
|
|
125
113
|
|
|
126
|
-
it
|
|
127
|
-
|
|
128
|
-
|
|
114
|
+
it 'should pass Github issue #90' do
|
|
115
|
+
number = '(0)30 1234 123'
|
|
116
|
+
expect(number.phony_normalized(country_code: 'NL')).to eq('+31301234123')
|
|
129
117
|
end
|
|
130
118
|
|
|
131
|
-
it
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
119
|
+
it 'should pass Github issue #107' do
|
|
120
|
+
number = '04575700834'
|
|
121
|
+
expect(number.phony_normalized(country_code: 'FI')).to eq('+3584575700834')
|
|
122
|
+
# Seems this number can be interpreted as from multiple countries, following fails:
|
|
123
|
+
# expect(number.phony_normalized(default_country_code: 'FI')).to eq('+3584575700834')
|
|
124
|
+
# expect("04575700834".phony_formatted(normalize: 'FI', format: :international)).to eql('+358 45 757 00 834')
|
|
137
125
|
end
|
|
138
126
|
|
|
139
|
-
it
|
|
140
|
-
number =
|
|
127
|
+
it 'should pass Github issue #113' do
|
|
128
|
+
number = '(951) 703-593'
|
|
141
129
|
expect(lambda {
|
|
142
130
|
number.phony_formatted!(normalize: 'US', spaces: '-', strict: true)
|
|
143
131
|
}).to raise_error(ArgumentError)
|
|
144
132
|
end
|
|
145
133
|
|
|
146
|
-
it
|
|
134
|
+
it 'should pass Github issue #95' do
|
|
147
135
|
number = '02031234567'
|
|
148
136
|
expect(number.phony_normalized(default_country_code: 'GB')).to eq('+442031234567')
|
|
149
137
|
end
|
|
@@ -153,124 +141,144 @@ describe PhonyRails do
|
|
|
153
141
|
expect(number.phony_normalized(default_country_code: 'FR')).to eq('+33687738358')
|
|
154
142
|
end
|
|
155
143
|
|
|
156
|
-
it
|
|
157
|
-
expect(Phony).to receive(:plausible?).and_raise(
|
|
144
|
+
it 'returns the original input if all goes wrong' do
|
|
145
|
+
expect(Phony).to receive(:plausible?).and_raise('unexpected error')
|
|
158
146
|
number = '(0)30 1234 123'
|
|
159
147
|
expect(number.phony_normalized(country_code: 'NL')).to eq number
|
|
160
148
|
end
|
|
149
|
+
|
|
150
|
+
it 'should pass Github issue #126 (country_code)' do
|
|
151
|
+
phone = '0143590213' # A plausible FR number
|
|
152
|
+
phone = PhonyRails.normalize_number(phone, country_code: 'FR')
|
|
153
|
+
expect(phone).to eq('+33143590213')
|
|
154
|
+
expect(Phony.plausible?(phone)).to be_truthy
|
|
155
|
+
phone = PhonyRails.normalize_number(phone, country_code: 'FR')
|
|
156
|
+
expect(phone).to eq('+33143590213')
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Adding a country code is expected behavior when a
|
|
160
|
+
# number is nog plausible.
|
|
161
|
+
it 'should pass Github issue #126 (country_code) (intended)' do
|
|
162
|
+
phone = '06123456789' # A non-plausible FR number
|
|
163
|
+
phone = PhonyRails.normalize_number(phone, country_code: 'FR')
|
|
164
|
+
expect(phone).to eq('+336123456789')
|
|
165
|
+
expect(Phony.plausible?(phone)).to be_falsy
|
|
166
|
+
phone = PhonyRails.normalize_number(phone, country_code: 'FR')
|
|
167
|
+
expect(phone).to eq('+33336123456789')
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it 'should pass Github issue #126 (default_country_code)' do
|
|
171
|
+
phone = '06123456789' # French phone numbers have to be 10 chars long
|
|
172
|
+
phone = PhonyRails.normalize_number(phone, default_country_code: 'FR')
|
|
173
|
+
expect(phone).to eq('+336123456789')
|
|
174
|
+
phone = PhonyRails.normalize_number(phone, default_country_code: 'FR')
|
|
175
|
+
expect(phone).to eq('+336123456789')
|
|
176
|
+
end
|
|
161
177
|
end
|
|
162
178
|
|
|
163
|
-
it
|
|
179
|
+
it 'should not change original String' do
|
|
164
180
|
s = '0101234123'
|
|
165
181
|
expect(s.phony_formatted(normalize: :NL)).to eql('010 123 4123')
|
|
166
182
|
expect(s).to eql('0101234123')
|
|
167
183
|
end
|
|
168
184
|
|
|
169
|
-
it
|
|
170
|
-
expect(
|
|
185
|
+
it 'should phony_format a digits string with spaces String' do
|
|
186
|
+
expect('31 10 1234123'.phony_formatted(format: :international, spaces: '-')).to eql('+31-10-123-4123')
|
|
171
187
|
end
|
|
172
188
|
|
|
173
|
-
it
|
|
174
|
-
expect(
|
|
189
|
+
it 'should phony_format a digits String' do
|
|
190
|
+
expect('31101234123'.phony_formatted(format: :international, spaces: '-')).to eql('+31-10-123-4123')
|
|
175
191
|
end
|
|
176
192
|
|
|
177
|
-
it
|
|
178
|
-
expect(
|
|
193
|
+
it 'returns nil if implausible phone' do
|
|
194
|
+
expect('this is not a phone'.phony_formatted).to be_nil
|
|
179
195
|
end
|
|
180
196
|
|
|
181
|
-
it
|
|
197
|
+
it 'returns nil on blank string' do
|
|
182
198
|
expect("".phony_formatted).to be_nil
|
|
183
199
|
end
|
|
184
|
-
|
|
185
200
|
end
|
|
186
201
|
|
|
187
202
|
describe 'the phony_normalized method' do
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
expect("".phony_normalized).to be_nil
|
|
203
|
+
it 'returns blank on blank string' do
|
|
204
|
+
expect(''.phony_normalized).to be_nil
|
|
191
205
|
end
|
|
192
206
|
|
|
193
207
|
it 'should not modify the original options Hash' do
|
|
194
208
|
options = {normalize: :NL, format: :international}
|
|
195
|
-
|
|
209
|
+
'0101234123'.phony_normalized(options)
|
|
196
210
|
expect(options).to eql({normalize: :NL, format: :international})
|
|
197
211
|
end
|
|
198
212
|
|
|
199
|
-
context
|
|
200
|
-
it
|
|
201
|
-
expect(
|
|
213
|
+
context 'when String misses a country_code' do
|
|
214
|
+
it 'should normalize with :country_code option' do
|
|
215
|
+
expect('010 1231234'.phony_normalized(country_code: :NL)).to eql('+31101231234')
|
|
202
216
|
end
|
|
203
217
|
|
|
204
|
-
it
|
|
205
|
-
expect(
|
|
218
|
+
it 'should normalize without :country_code option' do
|
|
219
|
+
expect('010 1231234'.phony_normalized).to eql('101231234')
|
|
206
220
|
end
|
|
207
221
|
|
|
208
|
-
it
|
|
209
|
-
expect(
|
|
222
|
+
it 'should normalize with :add_plus option' do
|
|
223
|
+
expect('010 1231234'.phony_normalized(country_code: :NL, add_plus: false)).to eql('31101231234')
|
|
210
224
|
end
|
|
211
|
-
|
|
212
225
|
end
|
|
213
226
|
|
|
214
|
-
it
|
|
215
|
-
expect(
|
|
227
|
+
it 'should normalize with :add_plus option' do
|
|
228
|
+
expect('+31 (0)10 1231234'.phony_normalized(add_plus: false)).to eql('31101231234')
|
|
216
229
|
end
|
|
217
230
|
|
|
218
|
-
it
|
|
219
|
-
expect(
|
|
231
|
+
it 'should normalize a String' do
|
|
232
|
+
expect('+31 (0)10 1231234'.phony_normalized).to eql('+31101231234')
|
|
220
233
|
end
|
|
221
|
-
|
|
222
234
|
end
|
|
223
|
-
|
|
224
235
|
end
|
|
225
236
|
|
|
226
237
|
describe 'PhonyRails#normalize_number' do
|
|
227
238
|
context 'number with a country code' do
|
|
228
|
-
|
|
229
|
-
it "should not add default_country_code" do
|
|
239
|
+
it 'should not add default_country_code' do
|
|
230
240
|
expect(PhonyRails.normalize_number('+4790909090', default_country_code: 'SE')).to eql('+4790909090') # SE = +46
|
|
231
241
|
expect(PhonyRails.normalize_number('004790909090', default_country_code: 'SE')).to eql('+4790909090')
|
|
232
242
|
expect(PhonyRails.normalize_number('4790909090', default_country_code: 'NO')).to eql('+4790909090') # NO = +47
|
|
233
243
|
end
|
|
234
244
|
|
|
235
|
-
it
|
|
245
|
+
it 'should force add country_code' do
|
|
236
246
|
expect(PhonyRails.normalize_number('+4790909090', country_code: 'SE')).to eql('+464790909090')
|
|
237
247
|
expect(PhonyRails.normalize_number('004790909090', country_code: 'SE')).to eql('+4604790909090') # FIXME: differs due to Phony.normalize in v2.7.1?!
|
|
238
248
|
expect(PhonyRails.normalize_number('4790909090', country_code: 'SE')).to eql('+464790909090')
|
|
239
249
|
end
|
|
240
250
|
|
|
241
|
-
it
|
|
251
|
+
it 'should recognize lowercase country codes' do
|
|
242
252
|
expect(PhonyRails.normalize_number('4790909090', country_code: 'se')).to eql('+464790909090')
|
|
243
253
|
end
|
|
244
|
-
|
|
245
254
|
end
|
|
246
255
|
|
|
247
256
|
context 'number without a country code' do
|
|
248
|
-
|
|
249
|
-
it "should normalize with a default_country_code" do
|
|
257
|
+
it 'should normalize with a default_country_code' do
|
|
250
258
|
expect(PhonyRails.normalize_number('010-1234123', default_country_code: 'NL')).to eql('+31101234123')
|
|
251
259
|
end
|
|
252
260
|
|
|
253
|
-
it
|
|
261
|
+
it 'should normalize with a country_code' do
|
|
254
262
|
expect(PhonyRails.normalize_number('010-1234123', country_code: 'NL', default_country_code: 'DE')).to eql('+31101234123')
|
|
255
263
|
expect(PhonyRails.normalize_number('010-1234123', country_code: 'NL')).to eql('+31101234123')
|
|
256
264
|
end
|
|
257
265
|
|
|
258
|
-
it
|
|
266
|
+
it 'should handle different countries' do
|
|
259
267
|
expect(PhonyRails.normalize_number('(030) 8 61 29 06', country_code: 'DE')).to eql('+49308612906')
|
|
260
268
|
expect(PhonyRails.normalize_number('0203 330 8897', country_code: 'GB')).to eql('+442033308897')
|
|
261
269
|
end
|
|
262
270
|
|
|
263
|
-
it
|
|
271
|
+
it 'should prefer country_code over default_country_code' do
|
|
264
272
|
expect(PhonyRails.normalize_number('(030) 8 61 29 06', country_code: 'DE', default_country_code: 'NL')).to eql('+49308612906')
|
|
265
273
|
end
|
|
266
274
|
|
|
267
|
-
it
|
|
275
|
+
it 'should recognize lowercase country codes' do
|
|
268
276
|
expect(PhonyRails.normalize_number('010-1234123', country_code: 'nl')).to eql('+31101234123')
|
|
269
277
|
end
|
|
270
278
|
|
|
271
279
|
end
|
|
272
280
|
|
|
273
|
-
it
|
|
281
|
+
it 'should handle some edge cases (with country_code)' do
|
|
274
282
|
expect(PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', country_code: 'NL')).to eql('+31101234123')
|
|
275
283
|
expect(PhonyRails.normalize_number('070-4157134', country_code: 'NL')).to eql('+31704157134')
|
|
276
284
|
expect(PhonyRails.normalize_number('0031-70-4157134', country_code: 'NL')).to eql('+31704157134')
|
|
@@ -280,7 +288,7 @@ describe PhonyRails do
|
|
|
280
288
|
expect(PhonyRails.normalize_number('0450 764 000', country_code: 'AU')).to eql('+61450764000')
|
|
281
289
|
end
|
|
282
290
|
|
|
283
|
-
it
|
|
291
|
+
it 'should handle some edge cases (with default_country_code)' do
|
|
284
292
|
expect(PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', country_code: 'NL')).to eql('+31101234123')
|
|
285
293
|
expect(PhonyRails.normalize_number('070-4157134', default_country_code: 'NL')).to eql('+31704157134')
|
|
286
294
|
expect(PhonyRails.normalize_number('0031-70-4157134', default_country_code: 'NL')).to eql('+31704157134')
|
|
@@ -290,7 +298,7 @@ describe PhonyRails do
|
|
|
290
298
|
expect(PhonyRails.normalize_number('0450 764 000', default_country_code: 'AU')).to eql('+61450764000')
|
|
291
299
|
end
|
|
292
300
|
|
|
293
|
-
it
|
|
301
|
+
it 'should normalize even an implausible number' do
|
|
294
302
|
expect(PhonyRails.normalize_number('01')).to eql('1')
|
|
295
303
|
end
|
|
296
304
|
end
|
|
@@ -304,7 +312,7 @@ describe PhonyRails do
|
|
|
304
312
|
let(:empty_number) { '' }
|
|
305
313
|
let(:nil_number) { nil }
|
|
306
314
|
|
|
307
|
-
it
|
|
315
|
+
it 'should return true for a valid number' do
|
|
308
316
|
is_expected.to be_plausible_number valid_number, country_code: 'US'
|
|
309
317
|
end
|
|
310
318
|
|
|
@@ -312,159 +320,159 @@ describe PhonyRails do
|
|
|
312
320
|
is_expected.not_to be_plausible_number invalid_number, country_code: 'US'
|
|
313
321
|
end
|
|
314
322
|
|
|
315
|
-
it
|
|
323
|
+
it 'should return true for a normalizable number' do
|
|
316
324
|
is_expected.to be_plausible_number normalizable_number, country_code: 'US'
|
|
317
325
|
end
|
|
318
326
|
|
|
319
|
-
it
|
|
327
|
+
it 'should return false for a valid number with the wrong country code' do
|
|
320
328
|
is_expected.not_to be_plausible_number normalizable_number, country_code: 'FR'
|
|
321
329
|
end
|
|
322
330
|
|
|
323
|
-
it
|
|
331
|
+
it 'should return true for a well formatted valid number' do
|
|
324
332
|
is_expected.to be_plausible_number formatted_french_number_with_country_code, country_code: 'FR'
|
|
325
333
|
end
|
|
326
334
|
|
|
327
|
-
it
|
|
335
|
+
it 'should return false for an empty number' do
|
|
328
336
|
is_expected.not_to be_plausible_number empty_number, country_code: 'US'
|
|
329
337
|
end
|
|
330
338
|
|
|
331
|
-
it
|
|
339
|
+
it 'should return false for a nil number' do
|
|
332
340
|
is_expected.not_to be_plausible_number nil_number, country_code: 'US'
|
|
333
341
|
end
|
|
334
342
|
|
|
335
|
-
it
|
|
343
|
+
it 'should return false when no country code is supplied' do
|
|
336
344
|
is_expected.not_to be_plausible_number normalizable_number
|
|
337
345
|
end
|
|
338
346
|
|
|
339
|
-
it
|
|
340
|
-
expect(Phony).to receive(:plausible?).twice.and_raise(
|
|
347
|
+
it 'returns false if something goes wrong' do
|
|
348
|
+
expect(Phony).to receive(:plausible?).twice.and_raise('unexpected error')
|
|
341
349
|
is_expected.not_to be_plausible_number normalizable_number, country_code: 'US'
|
|
342
350
|
end
|
|
343
351
|
end
|
|
344
352
|
|
|
345
353
|
shared_examples_for 'model with PhonyRails' do
|
|
346
354
|
describe 'defining model#phony_normalized_method' do
|
|
347
|
-
it
|
|
355
|
+
it 'should add a normalized_phone_attribute method' do
|
|
348
356
|
expect(model_klass.new).to respond_to(:normalized_phone_attribute)
|
|
349
357
|
end
|
|
350
358
|
|
|
351
|
-
it
|
|
359
|
+
it 'should add a normalized_phone_method method' do
|
|
352
360
|
expect(model_klass.new).to respond_to(:normalized_phone_method)
|
|
353
361
|
end
|
|
354
362
|
|
|
355
|
-
it
|
|
363
|
+
it 'should raise error on existing methods' do
|
|
356
364
|
expect(lambda {
|
|
357
365
|
model_klass.phony_normalized_method(:phone_method)
|
|
358
366
|
}).to raise_error(StandardError)
|
|
359
367
|
end
|
|
360
368
|
|
|
361
|
-
it
|
|
369
|
+
it 'should raise error on not existing attribute' do
|
|
362
370
|
model_klass.phony_normalized_method(:phone_non_existing_method)
|
|
363
|
-
expect(lambda
|
|
371
|
+
expect(lambda do
|
|
364
372
|
model_klass.new.normalized_phone_non_existing_method
|
|
365
|
-
|
|
373
|
+
end).to raise_error(ArgumentError)
|
|
366
374
|
end
|
|
367
375
|
end
|
|
368
376
|
|
|
369
377
|
describe 'defining model#phony_normalize' do
|
|
370
|
-
it
|
|
371
|
-
expect(lambda
|
|
378
|
+
it 'should not accept :as option with multiple attribute names' do
|
|
379
|
+
expect(lambda do
|
|
372
380
|
model_klass.phony_normalize(:phone_number, :phone1_method, as: 'non_existing_attribute')
|
|
373
|
-
|
|
381
|
+
end).to raise_error(ArgumentError)
|
|
374
382
|
end
|
|
375
383
|
|
|
376
|
-
it
|
|
377
|
-
expect(lambda
|
|
384
|
+
it 'should accept :as option with non existing attribute name' do
|
|
385
|
+
expect(lambda do
|
|
378
386
|
dummy_klass.phony_normalize(:non_existing_attribute, as: 'non_existing_attribute')
|
|
379
|
-
|
|
387
|
+
end).to_not raise_error
|
|
380
388
|
end
|
|
381
389
|
|
|
382
|
-
it
|
|
383
|
-
expect(lambda
|
|
390
|
+
it 'should accept :as option with single non existing attribute name' do
|
|
391
|
+
expect(lambda do
|
|
384
392
|
dummy_klass.phony_normalize(:phone_number, as: 'something_else')
|
|
385
|
-
|
|
393
|
+
end).to_not raise_error
|
|
386
394
|
end
|
|
387
395
|
|
|
388
|
-
it
|
|
396
|
+
it 'should accept :as option with single existing attribute name' do
|
|
389
397
|
expect(lambda {
|
|
390
398
|
model_klass.phony_normalize(:phone_number, as: 'phone_number_as_normalized')
|
|
391
399
|
}).to_not raise_error
|
|
392
400
|
end
|
|
393
401
|
|
|
394
|
-
it
|
|
402
|
+
it 'should accept a non existing attribute name' do
|
|
395
403
|
expect(lambda {
|
|
396
404
|
dummy_klass.phony_normalize(:non_existing_attribute)
|
|
397
405
|
}).to_not raise_error
|
|
398
406
|
end
|
|
399
407
|
|
|
400
|
-
it
|
|
408
|
+
it 'should accept supported options' do
|
|
401
409
|
options = [:country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country]
|
|
402
410
|
options.each do |option_sym|
|
|
403
|
-
expect(lambda
|
|
411
|
+
expect(lambda do
|
|
404
412
|
dummy_klass.phony_normalize(:phone_number, option_sym => false)
|
|
405
|
-
|
|
413
|
+
end).to_not raise_error
|
|
406
414
|
end
|
|
407
415
|
end
|
|
408
416
|
|
|
409
|
-
it
|
|
410
|
-
expect(lambda
|
|
417
|
+
it 'should not accept unsupported options' do
|
|
418
|
+
expect(lambda do
|
|
411
419
|
dummy_klass.phony_normalize(:phone_number, unsupported_option: false)
|
|
412
|
-
|
|
420
|
+
end).to raise_error(ArgumentError)
|
|
413
421
|
end
|
|
414
422
|
end
|
|
415
423
|
|
|
416
424
|
describe 'using model#phony_normalized_method' do
|
|
417
|
-
|
|
418
|
-
it
|
|
419
|
-
model = model_klass.new(phone_attribute:
|
|
425
|
+
# Following examples have complete number (with country code!)
|
|
426
|
+
it 'should return a normalized version of an attribute' do
|
|
427
|
+
model = model_klass.new(phone_attribute: '+31-(0)10-1234123')
|
|
420
428
|
expect(model.normalized_phone_attribute).to eql('+31101234123')
|
|
421
429
|
end
|
|
422
430
|
|
|
423
431
|
it "should return a normalized version of a method" do
|
|
424
|
-
model = model_klass.new(phone_method:
|
|
432
|
+
model = model_klass.new(phone_method: '+31-(0)10-1234123')
|
|
425
433
|
expect(model.normalized_phone_method).to eql('+31101234123')
|
|
426
434
|
end
|
|
427
435
|
|
|
428
|
-
|
|
429
|
-
it
|
|
430
|
-
model = model_klass.new(phone_attribute:
|
|
436
|
+
# Following examples have incomplete number
|
|
437
|
+
it 'should normalize even a unplausible number (no country code)' do
|
|
438
|
+
model = model_klass.new(phone_attribute: '(0)10-1234123')
|
|
431
439
|
expect(model.normalized_phone_attribute).to eql('101234123')
|
|
432
440
|
end
|
|
433
441
|
|
|
434
|
-
it
|
|
435
|
-
model = model_klass.new(phone_attribute:
|
|
442
|
+
it 'should use country_code option' do
|
|
443
|
+
model = model_klass.new(phone_attribute: '(0)10-1234123')
|
|
436
444
|
expect(model.normalized_phone_attribute(country_code: 'NL')).to eql('+31101234123')
|
|
437
445
|
end
|
|
438
446
|
|
|
439
|
-
it
|
|
440
|
-
model = model_klass.new(phone_attribute:
|
|
447
|
+
it 'should use country_code object method' do
|
|
448
|
+
model = model_klass.new(phone_attribute: '(0)10-1234123', country_code: 'NL')
|
|
441
449
|
expect(model.normalized_phone_attribute).to eql('+31101234123')
|
|
442
450
|
end
|
|
443
451
|
|
|
444
|
-
it
|
|
445
|
-
model = model_klass.new(phone1_method:
|
|
452
|
+
it 'should fallback to default_country_code option' do
|
|
453
|
+
model = model_klass.new(phone1_method: '(030) 8 61 29 06')
|
|
446
454
|
expect(model.normalized_phone1_method).to eql('+49308612906')
|
|
447
455
|
end
|
|
448
456
|
|
|
449
|
-
it
|
|
450
|
-
model = model_klass.new(phone1_method:
|
|
457
|
+
it 'should overwrite default_country_code option with object method' do
|
|
458
|
+
model = model_klass.new(phone1_method: '(030) 8 61 29 06', country_code: 'NL')
|
|
451
459
|
expect(model.normalized_phone1_method).to eql('+31308612906')
|
|
452
460
|
end
|
|
453
461
|
|
|
454
|
-
it
|
|
455
|
-
model = model_klass.new(phone1_method:
|
|
462
|
+
it 'should overwrite default_country_code option with option' do
|
|
463
|
+
model = model_klass.new(phone1_method: '(030) 8 61 29 06')
|
|
456
464
|
expect(model.normalized_phone1_method(country_code: 'NL')).to eql('+31308612906')
|
|
457
465
|
end
|
|
458
466
|
|
|
459
|
-
it
|
|
460
|
-
model = model_klass.new(phone1_method:
|
|
467
|
+
it 'should use last passed options' do
|
|
468
|
+
model = model_klass.new(phone1_method: '(030) 8 61 29 06')
|
|
461
469
|
expect(model.normalized_phone1_method(country_code: 'NL')).to eql('+31308612906')
|
|
462
470
|
expect(model.normalized_phone1_method(country_code: 'DE')).to eql('+49308612906')
|
|
463
471
|
expect(model.normalized_phone1_method(country_code: nil)).to eql('+49308612906')
|
|
464
472
|
end
|
|
465
473
|
|
|
466
|
-
it
|
|
467
|
-
model = model_klass.new(phone1_method:
|
|
474
|
+
it 'should use last object method' do
|
|
475
|
+
model = model_klass.new(phone1_method: '(030) 8 61 29 06')
|
|
468
476
|
model.country_code = 'NL'
|
|
469
477
|
expect(model.normalized_phone1_method).to eql('+31308612906')
|
|
470
478
|
model.country_code = 'DE'
|
|
@@ -475,27 +483,27 @@ describe PhonyRails do
|
|
|
475
483
|
end
|
|
476
484
|
|
|
477
485
|
describe 'using model#phony_normalize' do
|
|
478
|
-
it
|
|
479
|
-
model = model_klass.new(phone_number:
|
|
486
|
+
it 'should not change normalized numbers (see #76)' do
|
|
487
|
+
model = model_klass.new(phone_number: '+31-(0)10-1234123')
|
|
480
488
|
expect(model).to be_valid
|
|
481
489
|
expect(model.phone_number).to eql('+31101234123')
|
|
482
490
|
end
|
|
483
491
|
|
|
484
|
-
it
|
|
492
|
+
it 'should set a normalized version of an attribute using :as option' do
|
|
485
493
|
model_klass.phony_normalize :phone_number, as: :phone_number_as_normalized
|
|
486
|
-
model = model_klass.new(phone_number:
|
|
494
|
+
model = model_klass.new(phone_number: '+31-(0)10-1234123')
|
|
487
495
|
expect(model).to be_valid
|
|
488
496
|
expect(model.phone_number_as_normalized).to eql('+31101234123')
|
|
489
497
|
end
|
|
490
498
|
|
|
491
|
-
it
|
|
499
|
+
it 'should not add a + using :add_plus option' do
|
|
492
500
|
model_klass.phony_normalize :phone_number, add_plus: false
|
|
493
|
-
model = model_klass.new(phone_number:
|
|
501
|
+
model = model_klass.new(phone_number: '+31-(0)10-1234123')
|
|
494
502
|
expect(model).to be_valid
|
|
495
503
|
expect(model.phone_number).to eql('31101234123')
|
|
496
504
|
end
|
|
497
505
|
|
|
498
|
-
it
|
|
506
|
+
it 'should raise a RuntimeError at validation if the attribute doesn\'t exist' do
|
|
499
507
|
dummy_klass.phony_normalize :non_existing_attribute
|
|
500
508
|
dummy = dummy_klass.new
|
|
501
509
|
expect(lambda {
|
|
@@ -503,7 +511,7 @@ describe PhonyRails do
|
|
|
503
511
|
}).to raise_error(RuntimeError)
|
|
504
512
|
end
|
|
505
513
|
|
|
506
|
-
it
|
|
514
|
+
it 'should raise a RuntimeError at validation if the :as option attribute doesn\'t exist' do
|
|
507
515
|
dummy_klass.phony_normalize :phone_number, as: :non_existing_attribute
|
|
508
516
|
dummy = dummy_klass.new
|
|
509
517
|
expect(lambda {
|
|
@@ -518,7 +526,7 @@ describe PhonyRails do
|
|
|
518
526
|
let(:dummy_klass) { ActiveRecordDummy }
|
|
519
527
|
it_behaves_like 'model with PhonyRails'
|
|
520
528
|
|
|
521
|
-
it
|
|
529
|
+
it 'should correctly keep a hard set country_code' do
|
|
522
530
|
model = model_klass.new(fax_number: '+1 978 555 0000')
|
|
523
531
|
expect(model.valid?).to be true
|
|
524
532
|
expect(model.fax_number).to eql('+19785550000')
|
|
@@ -157,6 +157,14 @@ describe PhonyPlausibleValidator do
|
|
|
157
157
|
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
|
158
158
|
end
|
|
159
159
|
|
|
160
|
+
it "should translate the error message in Dutch" do
|
|
161
|
+
I18n.with_locale(:nl) do
|
|
162
|
+
@home.phone_number = INVALID_NUMBER
|
|
163
|
+
@home.valid?
|
|
164
|
+
expect(@home.errors.messages).to include(phone_number: ["is geen geldig nummer"])
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
160
168
|
it "should translate the error message in English" do
|
|
161
169
|
I18n.with_locale(:en) do
|
|
162
170
|
@home.phone_number = INVALID_NUMBER
|
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.12.
|
|
4
|
+
version: 0.12.13
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Joost Hietbrink
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-02-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: phony
|
|
@@ -93,6 +93,7 @@ files:
|
|
|
93
93
|
- lib/phony_rails/locales/it.yml
|
|
94
94
|
- lib/phony_rails/locales/ja.yml
|
|
95
95
|
- lib/phony_rails/locales/km.yml
|
|
96
|
+
- lib/phony_rails/locales/nl.yml
|
|
96
97
|
- lib/phony_rails/locales/ru.yml
|
|
97
98
|
- lib/phony_rails/locales/tr.yml
|
|
98
99
|
- lib/phony_rails/locales/uk.yml
|