phony_rails 0.12.6 → 0.12.7
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/CHANGELOG.md +291 -0
- data/Gemfile +4 -4
- data/Gemfile.lock +49 -35
- data/README.md +3 -1
- data/lib/phony_rails/locales/he.yml +4 -0
- data/lib/phony_rails/version.rb +1 -1
- data/spec/lib/phony_rails_spec.rb +134 -129
- data/spec/lib/validators/phony_validator_spec.rb +56 -56
- metadata +4 -2
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
# PhonyRails [](https://travis-ci.org/joost/phony_rails)
|
1
|
+
# PhonyRails [](https://travis-ci.org/joost/phony_rails)  
|
2
2
|
|
3
3
|
This small Gem adds useful methods to your Rails app to validate, display and save phone numbers.
|
4
4
|
It uses the super awesome Phony gem (https://github.com/floere/phony).
|
5
5
|
|
6
|
+
Find version information in the [CHANGELOG](CHANGELOG.md).
|
7
|
+
|
6
8
|
## Installation
|
7
9
|
|
8
10
|
Add this line to your application's Gemfile:
|
data/lib/phony_rails/version.rb
CHANGED
@@ -12,15 +12,15 @@ describe PhonyRails do
|
|
12
12
|
it 'should not modify the original options Hash' do
|
13
13
|
options = {:normalize => :NL, :format => :international}
|
14
14
|
"0101234123".phony_formatted(options)
|
15
|
-
options.
|
15
|
+
expect(options).to eql({:normalize => :NL, :format => :international})
|
16
16
|
end
|
17
17
|
|
18
18
|
describe 'with the bang!' do
|
19
19
|
|
20
20
|
it "should change the String using the bang method" do
|
21
21
|
s = "0101234123"
|
22
|
-
s.phony_formatted!(:normalize => :NL, :format => :international).
|
23
|
-
s.
|
22
|
+
expect(s.phony_formatted!(:normalize => :NL, :format => :international)).to eql('+31 10 123 4123')
|
23
|
+
expect(s).to eql("+31 10 123 4123")
|
24
24
|
end
|
25
25
|
|
26
26
|
end
|
@@ -29,14 +29,14 @@ describe PhonyRails do
|
|
29
29
|
|
30
30
|
it "should return nil with non plausible number" do
|
31
31
|
number = '+319090' # not valid
|
32
|
-
Phony.plausible?(number).
|
33
|
-
number.phony_formatted(:strict => true).
|
32
|
+
expect(Phony.plausible?(number)).to be false
|
33
|
+
expect(number.phony_formatted(:strict => true)).to eql(nil)
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should not return nil with plausible number" do
|
37
37
|
number = '+31101234123' # valid
|
38
|
-
Phony.plausible?(number).
|
39
|
-
number.phony_formatted(:strict => true).
|
38
|
+
expect(Phony.plausible?(number)).to be true
|
39
|
+
expect(number.phony_formatted(:strict => true)).to_not eql(nil)
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
@@ -44,30 +44,30 @@ describe PhonyRails do
|
|
44
44
|
describe 'with normalize option' do
|
45
45
|
|
46
46
|
it "should phony_format" do
|
47
|
-
"101234123".phony_formatted(:normalize => :NL).
|
48
|
-
"101234123".phony_formatted(:normalize => :NL, :format => :international).
|
47
|
+
expect("101234123".phony_formatted(:normalize => :NL)).to eql('010 123 4123')
|
48
|
+
expect("101234123".phony_formatted(:normalize => :NL, :format => :international)).to eql('+31 10 123 4123')
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should not change original String" do
|
52
52
|
s = "0101234123"
|
53
|
-
s.phony_formatted(:normalize => :NL).
|
54
|
-
s.
|
53
|
+
expect(s.phony_formatted(:normalize => :NL)).to eql('010 123 4123')
|
54
|
+
expect(s).to eql("0101234123")
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should phony_format String with country code" do
|
58
|
-
"31101234123".phony_formatted(:normalize => :NL).
|
58
|
+
expect("31101234123".phony_formatted(:normalize => :NL)).to eql('010 123 4123')
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should phony_format String with country code" do
|
62
|
-
"31101234123".phony_formatted(:normalize => :NL).
|
62
|
+
expect("31101234123".phony_formatted(:normalize => :NL)).to eql('010 123 4123')
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should accept strings with non-digits in it" do
|
66
|
-
"+31-10-1234123".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-').
|
66
|
+
expect("+31-10-1234123".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-')).to eql('+31-10-123-4123')
|
67
67
|
end
|
68
68
|
|
69
69
|
it "should phony_format String with country code different than normalized value" do
|
70
|
-
"+4790909090".phony_formatted(:normalize => :SE, :format => :international).
|
70
|
+
expect("+4790909090".phony_formatted(:normalize => :SE, :format => :international)).to eql('+47 909 09 090')
|
71
71
|
end
|
72
72
|
|
73
73
|
end
|
@@ -78,15 +78,15 @@ describe PhonyRails do
|
|
78
78
|
|
79
79
|
context 'when raise is true' do
|
80
80
|
it "should raise the error" do
|
81
|
-
lambda {
|
81
|
+
expect(lambda {
|
82
82
|
"8887716095".phony_formatted(format: :international, raise: true)
|
83
|
-
}.
|
83
|
+
}).to raise_error(NoMethodError)
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
87
|
context 'when raise is false (default)' do
|
88
88
|
it "should return original String on exception" do
|
89
|
-
"8887716095".phony_formatted(format: :international).
|
89
|
+
expect("8887716095".phony_formatted(format: :international)).to eq('8887716095')
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -96,52 +96,58 @@ describe PhonyRails do
|
|
96
96
|
|
97
97
|
# https://github.com/joost/phony_rails/issues/79
|
98
98
|
it "should pass Github issue #42" do
|
99
|
-
"8887716095".phony_formatted(format: :international, normalize: 'US', raise: true).
|
99
|
+
expect("8887716095".phony_formatted(format: :international, normalize: 'US', raise: true)).to eq('+1 (888) 771-6095')
|
100
100
|
end
|
101
101
|
|
102
102
|
# https://github.com/joost/phony_rails/issues/42
|
103
103
|
it "should pass Github issue #42" do
|
104
|
-
PhonyRails.normalize_number("0606060606", default_country_code: 'FR').
|
104
|
+
expect(PhonyRails.normalize_number("0606060606", default_country_code: 'FR')).to eq('+33606060606')
|
105
105
|
end
|
106
106
|
|
107
107
|
it "should pass Github issue #85" do
|
108
|
-
PhonyRails.normalize_number("47386160", default_country_code: 'NO').
|
109
|
-
PhonyRails.normalize_number("47386160", country_number: '47').
|
108
|
+
expect(PhonyRails.normalize_number("47386160", default_country_code: 'NO')).to eq('+4747386160')
|
109
|
+
expect(PhonyRails.normalize_number("47386160", country_number: '47')).to eq('+4747386160')
|
110
110
|
end
|
111
111
|
|
112
112
|
it "should pass Github issue #87" do
|
113
|
-
PhonyRails.normalize_number('2318725305', country_code: 'US').
|
114
|
-
PhonyRails.normalize_number('2318725305', default_country_code: 'US').
|
115
|
-
PhonyRails.normalize_number('+2318725305', default_country_code: 'US').
|
113
|
+
expect(PhonyRails.normalize_number('2318725305', country_code: 'US')).to eq('+12318725305')
|
114
|
+
expect(PhonyRails.normalize_number('2318725305', default_country_code: 'US')).to eq('+12318725305')
|
115
|
+
expect(PhonyRails.normalize_number('+2318725305', default_country_code: 'US')).to eq('+2318725305')
|
116
116
|
end
|
117
117
|
|
118
118
|
it "should pass Github issue #89" do
|
119
119
|
number = '+33 (0)6 87 36 18 75'
|
120
|
-
Phony.plausible?(number).
|
121
|
-
PhonyRails.normalize_number(number, country_code: 'FR').
|
120
|
+
expect(Phony.plausible?(number)).to be true
|
121
|
+
expect(PhonyRails.normalize_number(number, country_code: 'FR')).to eq('+33687361875')
|
122
122
|
end
|
123
|
+
|
124
|
+
it "should pass Github issue #90" do
|
125
|
+
number = '(0)30 1234 123'
|
126
|
+
expect(number.phony_normalized(country_code: 'NL')).to eq('+31301234123')
|
127
|
+
end
|
128
|
+
|
123
129
|
end
|
124
130
|
|
125
131
|
it "should not change original String" do
|
126
132
|
s = '0101234123'
|
127
|
-
s.phony_formatted(:normalize => :NL).
|
128
|
-
s.
|
133
|
+
expect(s.phony_formatted(:normalize => :NL)).to eql('010 123 4123')
|
134
|
+
expect(s).to eql('0101234123')
|
129
135
|
end
|
130
136
|
|
131
137
|
it "should phony_format a digits string with spaces String" do
|
132
|
-
"31 10 1234123".phony_formatted(:format => :international, :spaces => '-').
|
138
|
+
expect("31 10 1234123".phony_formatted(:format => :international, :spaces => '-')).to eql('+31-10-123-4123')
|
133
139
|
end
|
134
140
|
|
135
141
|
it "should phony_format a digits String" do
|
136
|
-
"31101234123".phony_formatted(:format => :international, :spaces => '-').
|
142
|
+
expect("31101234123".phony_formatted(:format => :international, :spaces => '-')).to eql('+31-10-123-4123')
|
137
143
|
end
|
138
144
|
|
139
145
|
it "returns nil if implausible phone" do
|
140
|
-
"this is not a phone".phony_formatted.
|
146
|
+
expect("this is not a phone".phony_formatted).to be_nil
|
141
147
|
end
|
142
148
|
|
143
149
|
it "returns nil on blank string" do
|
144
|
-
"".phony_formatted.
|
150
|
+
expect("".phony_formatted).to be_nil
|
145
151
|
end
|
146
152
|
|
147
153
|
end
|
@@ -149,36 +155,36 @@ describe PhonyRails do
|
|
149
155
|
describe 'the phony_normalized method' do
|
150
156
|
|
151
157
|
it "returns blank on blank string" do
|
152
|
-
"".phony_normalized.
|
158
|
+
expect("".phony_normalized).to be_nil
|
153
159
|
end
|
154
160
|
|
155
161
|
it 'should not modify the original options Hash' do
|
156
162
|
options = {:normalize => :NL, :format => :international}
|
157
163
|
"0101234123".phony_normalized(options)
|
158
|
-
options.
|
164
|
+
expect(options).to eql({:normalize => :NL, :format => :international})
|
159
165
|
end
|
160
166
|
|
161
167
|
context "when String misses a country_code" do
|
162
168
|
it "should normalize with :country_code option" do
|
163
|
-
"010 1231234".phony_normalized(:country_code => :NL).
|
169
|
+
expect("010 1231234".phony_normalized(:country_code => :NL)).to eql("+31101231234")
|
164
170
|
end
|
165
171
|
|
166
172
|
it "should normalize without :country_code option" do
|
167
|
-
"010 1231234".phony_normalized.
|
173
|
+
expect("010 1231234".phony_normalized).to eql("101231234")
|
168
174
|
end
|
169
175
|
|
170
176
|
it "should normalize with :add_plus option" do
|
171
|
-
"010 1231234".phony_normalized(:country_code => :NL, :add_plus => false).
|
177
|
+
expect("010 1231234".phony_normalized(:country_code => :NL, :add_plus => false)).to eql("31101231234")
|
172
178
|
end
|
173
179
|
|
174
180
|
end
|
175
181
|
|
176
182
|
it "should normalize with :add_plus option" do
|
177
|
-
"+31 (0)10 1231234".phony_normalized(:add_plus => false).
|
183
|
+
expect("+31 (0)10 1231234".phony_normalized(:add_plus => false)).to eql("31101231234")
|
178
184
|
end
|
179
185
|
|
180
186
|
it "should normalize a String" do
|
181
|
-
"+31 (0)10 1231234".phony_normalized.
|
187
|
+
expect("+31 (0)10 1231234".phony_normalized).to eql("+31101231234")
|
182
188
|
end
|
183
189
|
|
184
190
|
end
|
@@ -189,19 +195,19 @@ describe PhonyRails do
|
|
189
195
|
context 'number with a country code' do
|
190
196
|
|
191
197
|
it "should not add default_country_code" do
|
192
|
-
PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE').
|
193
|
-
PhonyRails.normalize_number('004790909090', :default_country_code => 'SE').
|
194
|
-
PhonyRails.normalize_number('4790909090', :default_country_code => 'NO').
|
198
|
+
expect(PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE')).to eql('+4790909090') # SE = +46
|
199
|
+
expect(PhonyRails.normalize_number('004790909090', :default_country_code => 'SE')).to eql('+4790909090')
|
200
|
+
expect(PhonyRails.normalize_number('4790909090', :default_country_code => 'NO')).to eql('+4790909090') # NO = +47
|
195
201
|
end
|
196
202
|
|
197
203
|
it "should force add country_code" do
|
198
|
-
PhonyRails.normalize_number('+4790909090', :country_code => 'SE').
|
199
|
-
PhonyRails.normalize_number('004790909090', :country_code => 'SE').
|
200
|
-
PhonyRails.normalize_number('4790909090', :country_code => 'SE').
|
204
|
+
expect(PhonyRails.normalize_number('+4790909090', :country_code => 'SE')).to eql('+464790909090')
|
205
|
+
expect(PhonyRails.normalize_number('004790909090', :country_code => 'SE')).to eql('+4604790909090') # FIXME: differs due to Phony.normalize in v2.7.1?!
|
206
|
+
expect(PhonyRails.normalize_number('4790909090', :country_code => 'SE')).to eql('+464790909090')
|
201
207
|
end
|
202
208
|
|
203
209
|
it "should recognize lowercase country codes" do
|
204
|
-
PhonyRails.normalize_number('4790909090', :country_code => 'se').
|
210
|
+
expect(PhonyRails.normalize_number('4790909090', :country_code => 'se')).to eql('+464790909090')
|
205
211
|
end
|
206
212
|
|
207
213
|
end
|
@@ -209,51 +215,51 @@ describe PhonyRails do
|
|
209
215
|
context 'number without a country code' do
|
210
216
|
|
211
217
|
it "should normalize with a default_country_code" do
|
212
|
-
PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').
|
218
|
+
expect(PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL')).to eql('+31101234123')
|
213
219
|
end
|
214
220
|
|
215
221
|
it "should normalize with a country_code" do
|
216
|
-
PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').
|
217
|
-
PhonyRails.normalize_number('010-1234123', :country_code => 'NL').
|
222
|
+
expect(PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE')).to eql('+31101234123')
|
223
|
+
expect(PhonyRails.normalize_number('010-1234123', :country_code => 'NL')).to eql('+31101234123')
|
218
224
|
end
|
219
225
|
|
220
226
|
it "should handle different countries" do
|
221
|
-
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').
|
222
|
-
PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').
|
227
|
+
expect(PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE')).to eql('+49308612906')
|
228
|
+
expect(PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB')).to eql('+442033308897')
|
223
229
|
end
|
224
230
|
|
225
231
|
it "should prefer country_code over default_country_code" do
|
226
|
-
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL').
|
232
|
+
expect(PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL')).to eql('+49308612906')
|
227
233
|
end
|
228
234
|
|
229
235
|
it "should recognize lowercase country codes" do
|
230
|
-
PhonyRails.normalize_number('010-1234123', :country_code => 'nl').
|
236
|
+
expect(PhonyRails.normalize_number('010-1234123', :country_code => 'nl')).to eql('+31101234123')
|
231
237
|
end
|
232
238
|
|
233
239
|
end
|
234
240
|
|
235
241
|
it "should handle some edge cases (with country_code)" do
|
236
|
-
PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL').
|
237
|
-
PhonyRails.normalize_number('070-4157134', :country_code => 'NL').
|
238
|
-
PhonyRails.normalize_number('0031-70-4157134', :country_code => 'NL').
|
239
|
-
PhonyRails.normalize_number('+31-70-4157134', :country_code => 'NL').
|
240
|
-
PhonyRails.normalize_number('0322-69497', :country_code => 'BE').
|
241
|
-
PhonyRails.normalize_number('+32 3 226 94 97', :country_code => 'BE').
|
242
|
-
PhonyRails.normalize_number('0450 764 000', :country_code => 'AU').
|
242
|
+
expect(PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL')).to eql('+31101234123')
|
243
|
+
expect(PhonyRails.normalize_number('070-4157134', :country_code => 'NL')).to eql('+31704157134')
|
244
|
+
expect(PhonyRails.normalize_number('0031-70-4157134', :country_code => 'NL')).to eql('+31704157134')
|
245
|
+
expect(PhonyRails.normalize_number('+31-70-4157134', :country_code => 'NL')).to eql('+31704157134')
|
246
|
+
expect(PhonyRails.normalize_number('0322-69497', :country_code => 'BE')).to eql('+3232269497')
|
247
|
+
expect(PhonyRails.normalize_number('+32 3 226 94 97', :country_code => 'BE')).to eql('+3232269497')
|
248
|
+
expect(PhonyRails.normalize_number('0450 764 000', :country_code => 'AU')).to eql('+61450764000')
|
243
249
|
end
|
244
250
|
|
245
251
|
it "should handle some edge cases (with default_country_code)" do
|
246
|
-
PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL').
|
247
|
-
PhonyRails.normalize_number('070-4157134', :default_country_code => 'NL').
|
248
|
-
PhonyRails.normalize_number('0031-70-4157134', :default_country_code => 'NL').
|
249
|
-
PhonyRails.normalize_number('+31-70-4157134', :default_country_code => 'NL').
|
250
|
-
PhonyRails.normalize_number('0322-69497', :default_country_code => 'BE').
|
251
|
-
PhonyRails.normalize_number('+32 3 226 94 97', :default_country_code => 'BE').
|
252
|
-
PhonyRails.normalize_number('0450 764 000', :default_country_code => 'AU').
|
252
|
+
expect(PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL')).to eql('+31101234123')
|
253
|
+
expect(PhonyRails.normalize_number('070-4157134', :default_country_code => 'NL')).to eql('+31704157134')
|
254
|
+
expect(PhonyRails.normalize_number('0031-70-4157134', :default_country_code => 'NL')).to eql('+31704157134')
|
255
|
+
expect(PhonyRails.normalize_number('+31-70-4157134', :default_country_code => 'NL')).to eql('+31704157134')
|
256
|
+
expect(PhonyRails.normalize_number('0322-69497', :default_country_code => 'BE')).to eql('+3232269497')
|
257
|
+
expect(PhonyRails.normalize_number('+32 3 226 94 97', :default_country_code => 'BE')).to eql('+3232269497')
|
258
|
+
expect(PhonyRails.normalize_number('0450 764 000', :default_country_code => 'AU')).to eql('+61450764000')
|
253
259
|
end
|
254
260
|
|
255
261
|
it "should normalize even an implausible number" do
|
256
|
-
PhonyRails.normalize_number('01').
|
262
|
+
expect(PhonyRails.normalize_number('01')).to eql('1')
|
257
263
|
end
|
258
264
|
end
|
259
265
|
|
@@ -266,106 +272,106 @@ describe PhonyRails do
|
|
266
272
|
let(:nil_number) { nil }
|
267
273
|
|
268
274
|
it "should return true for a valid number" do
|
269
|
-
PhonyRails.plausible_number?(valid_number, country_code: 'US').
|
275
|
+
expect(PhonyRails.plausible_number?(valid_number, country_code: 'US')).to be true
|
270
276
|
end
|
271
277
|
|
272
278
|
it "should return false for an invalid number" do
|
273
|
-
PhonyRails.plausible_number?(invalid_number, country_code: 'US').
|
279
|
+
expect(PhonyRails.plausible_number?(invalid_number, country_code: 'US')).to be false
|
274
280
|
end
|
275
281
|
|
276
282
|
it "should return true for a normalizable number" do
|
277
|
-
PhonyRails.plausible_number?(normalizable_number, country_code: 'US').
|
283
|
+
expect(PhonyRails.plausible_number?(normalizable_number, country_code: 'US')).to be true
|
278
284
|
end
|
279
285
|
|
280
286
|
it "should return false for a valid number with the wrong country code" do
|
281
|
-
PhonyRails.plausible_number?(valid_number, country_code: 'FR').
|
287
|
+
expect(PhonyRails.plausible_number?(valid_number, country_code: 'FR')).to be false
|
282
288
|
end
|
283
289
|
|
284
290
|
it "should return true for a well formatted valid number" do
|
285
|
-
PhonyRails.plausible_number?(formatted_french_number_with_country_code, country_code: 'FR').
|
291
|
+
expect(PhonyRails.plausible_number?(formatted_french_number_with_country_code, country_code: 'FR')).to be true
|
286
292
|
end
|
287
293
|
|
288
294
|
it "should return false for an empty number" do
|
289
|
-
PhonyRails.plausible_number?(empty_number, country_code: 'US').
|
295
|
+
expect(PhonyRails.plausible_number?(empty_number, country_code: 'US')).to be false
|
290
296
|
end
|
291
297
|
|
292
298
|
it "should return false for a nil number" do
|
293
|
-
PhonyRails.plausible_number?(nil_number, country_code: 'US').
|
299
|
+
expect(PhonyRails.plausible_number?(nil_number, country_code: 'US')).to be false
|
294
300
|
end
|
295
301
|
|
296
302
|
it "should return false when no country code is supplied" do
|
297
|
-
PhonyRails.plausible_number?(normalizable_number).
|
303
|
+
expect(PhonyRails.plausible_number?(normalizable_number)).to be false
|
298
304
|
end
|
299
305
|
end
|
300
306
|
|
301
307
|
shared_examples_for 'model with PhonyRails' do
|
302
308
|
describe 'defining model#phony_normalized_method' do
|
303
309
|
it "should add a normalized_phone_attribute method" do
|
304
|
-
model_klass.new.
|
310
|
+
expect(model_klass.new).to respond_to(:normalized_phone_attribute)
|
305
311
|
end
|
306
312
|
|
307
313
|
it "should add a normalized_phone_method method" do
|
308
|
-
model_klass.new.
|
314
|
+
expect(model_klass.new).to respond_to(:normalized_phone_method)
|
309
315
|
end
|
310
316
|
|
311
317
|
it "should raise error on existing methods" do
|
312
|
-
lambda {
|
318
|
+
expect(lambda {
|
313
319
|
model_klass.phony_normalized_method(:phone_method)
|
314
|
-
}.
|
320
|
+
}).to raise_error(StandardError)
|
315
321
|
end
|
316
322
|
|
317
323
|
it "should raise error on not existing attribute" do
|
318
324
|
model_klass.phony_normalized_method(:phone_non_existing_method)
|
319
|
-
lambda {
|
325
|
+
expect(lambda {
|
320
326
|
model_klass.new.normalized_phone_non_existing_method
|
321
|
-
}.
|
327
|
+
}).to raise_error(ArgumentError)
|
322
328
|
end
|
323
329
|
end
|
324
330
|
|
325
331
|
describe 'defining model#phony_normalize' do
|
326
332
|
it "should not accept :as option with multiple attribute names" do
|
327
|
-
lambda {
|
333
|
+
expect(lambda {
|
328
334
|
model_klass.phony_normalize(:phone_number, :phone1_method, :as => 'non_existing_attribute')
|
329
|
-
}.
|
335
|
+
}).to raise_error(ArgumentError)
|
330
336
|
end
|
331
337
|
|
332
338
|
it "should accept :as option with non existing attribute name" do
|
333
|
-
lambda {
|
339
|
+
expect(lambda {
|
334
340
|
dummy_klass.phony_normalize(:non_existing_attribute, :as => 'non_existing_attribute')
|
335
|
-
}.
|
341
|
+
}).to_not raise_error
|
336
342
|
end
|
337
343
|
|
338
344
|
it "should accept :as option with single non existing attribute name" do
|
339
|
-
lambda {
|
345
|
+
expect(lambda {
|
340
346
|
dummy_klass.phony_normalize(:phone_number, :as => 'something_else')
|
341
|
-
}.
|
347
|
+
}).to_not raise_error
|
342
348
|
end
|
343
349
|
|
344
350
|
it "should accept :as option with single existing attribute name" do
|
345
|
-
lambda {
|
351
|
+
expect(lambda {
|
346
352
|
model_klass.phony_normalize(:phone_number, :as => 'phone_number_as_normalized')
|
347
|
-
}.
|
353
|
+
}).to_not raise_error
|
348
354
|
end
|
349
355
|
|
350
356
|
it "should accept a non existing attribute name" do
|
351
|
-
lambda {
|
357
|
+
expect(lambda {
|
352
358
|
dummy_klass.phony_normalize(:non_existing_attribute)
|
353
|
-
}.
|
359
|
+
}).to_not raise_error
|
354
360
|
end
|
355
361
|
|
356
362
|
it "should accept supported options" do
|
357
363
|
options = [:country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as]
|
358
364
|
options.each do |option_sym|
|
359
|
-
lambda {
|
365
|
+
expect(lambda {
|
360
366
|
dummy_klass.phony_normalize(:phone_number, option_sym => false)
|
361
|
-
}.
|
367
|
+
}).to_not raise_error
|
362
368
|
end
|
363
369
|
end
|
364
370
|
|
365
371
|
it "should not accept unsupported options" do
|
366
|
-
lambda {
|
372
|
+
expect(lambda {
|
367
373
|
dummy_klass.phony_normalize(:phone_number, unsupported_option: false)
|
368
|
-
}.
|
374
|
+
}).to raise_error(ArgumentError)
|
369
375
|
end
|
370
376
|
end
|
371
377
|
|
@@ -373,99 +379,98 @@ describe PhonyRails do
|
|
373
379
|
# Following examples have complete number (with country code!)
|
374
380
|
it "should return a normalized version of an attribute" do
|
375
381
|
model = model_klass.new(:phone_attribute => "+31-(0)10-1234123")
|
376
|
-
model.normalized_phone_attribute.
|
382
|
+
expect(model.normalized_phone_attribute).to eql('+31101234123')
|
377
383
|
end
|
378
384
|
|
379
385
|
it "should return a normalized version of a method" do
|
380
386
|
model = model_klass.new(:phone_method => "+31-(0)10-1234123")
|
381
|
-
model.normalized_phone_method.
|
387
|
+
expect(model.normalized_phone_method).to eql('+31101234123')
|
382
388
|
end
|
383
389
|
|
384
390
|
# Following examples have incomplete number
|
385
391
|
it "should normalize even a unplausible number (no country code)" do
|
386
392
|
model = model_klass.new(:phone_attribute => "(0)10-1234123")
|
387
|
-
model.normalized_phone_attribute.
|
393
|
+
expect(model.normalized_phone_attribute).to eql('101234123')
|
388
394
|
end
|
389
395
|
|
390
396
|
it "should use country_code option" do
|
391
397
|
model = model_klass.new(:phone_attribute => "(0)10-1234123")
|
392
|
-
model.normalized_phone_attribute(:country_code => 'NL').
|
398
|
+
expect(model.normalized_phone_attribute(:country_code => 'NL')).to eql('+31101234123')
|
393
399
|
end
|
394
400
|
|
395
401
|
it "should use country_code object method" do
|
396
402
|
model = model_klass.new(:phone_attribute => "(0)10-1234123", :country_code => 'NL')
|
397
|
-
model.normalized_phone_attribute.
|
403
|
+
expect(model.normalized_phone_attribute).to eql('+31101234123')
|
398
404
|
end
|
399
405
|
|
400
406
|
it "should fallback to default_country_code option" do
|
401
407
|
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
402
|
-
model.normalized_phone1_method.
|
408
|
+
expect(model.normalized_phone1_method).to eql('+49308612906')
|
403
409
|
end
|
404
410
|
|
405
411
|
it "should overwrite default_country_code option with object method" do
|
406
412
|
model = model_klass.new(:phone1_method => "(030) 8 61 29 06", :country_code => 'NL')
|
407
|
-
model.normalized_phone1_method.
|
413
|
+
expect(model.normalized_phone1_method).to eql('+31308612906')
|
408
414
|
end
|
409
415
|
|
410
416
|
it "should overwrite default_country_code option with option" do
|
411
417
|
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
412
|
-
model.normalized_phone1_method(:country_code => 'NL').
|
418
|
+
expect(model.normalized_phone1_method(:country_code => 'NL')).to eql('+31308612906')
|
413
419
|
end
|
414
420
|
|
415
421
|
it "should use last passed options" do
|
416
422
|
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
417
|
-
model.normalized_phone1_method(:country_code => 'NL').
|
418
|
-
model.normalized_phone1_method(:country_code => 'DE').
|
419
|
-
model.normalized_phone1_method(:country_code => nil).
|
423
|
+
expect(model.normalized_phone1_method(:country_code => 'NL')).to eql('+31308612906')
|
424
|
+
expect(model.normalized_phone1_method(:country_code => 'DE')).to eql('+49308612906')
|
425
|
+
expect(model.normalized_phone1_method(:country_code => nil)).to eql('+49308612906')
|
420
426
|
end
|
421
427
|
|
422
428
|
it "should use last object method" do
|
423
429
|
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
424
430
|
model.country_code = 'NL'
|
425
|
-
model.normalized_phone1_method.
|
431
|
+
expect(model.normalized_phone1_method).to eql('+31308612906')
|
426
432
|
model.country_code = 'DE'
|
427
|
-
model.normalized_phone1_method.
|
433
|
+
expect(model.normalized_phone1_method).to eql('+49308612906')
|
428
434
|
model.country_code = nil
|
429
|
-
model.normalized_phone1_method(:country_code => nil).
|
435
|
+
expect(model.normalized_phone1_method(:country_code => nil)).to eql('+49308612906')
|
430
436
|
end
|
431
437
|
end
|
432
438
|
|
433
439
|
describe 'using model#phony_normalize' do
|
434
440
|
it "should not change normalized numbers (see #76)" do
|
435
441
|
model = model_klass.new(:phone_number => "+31-(0)10-1234123")
|
436
|
-
model.valid
|
437
|
-
model.phone_number.
|
438
|
-
|
442
|
+
expect(model.valid?).to be true
|
443
|
+
expect(model.phone_number).to eql('+31101234123')
|
439
444
|
end
|
440
445
|
|
441
446
|
it "should set a normalized version of an attribute using :as option" do
|
442
447
|
model_klass.phony_normalize :phone_number, :as => :phone_number_as_normalized
|
443
448
|
model = model_klass.new(:phone_number => "+31-(0)10-1234123")
|
444
|
-
model.valid
|
445
|
-
model.phone_number_as_normalized.
|
449
|
+
expect(model.valid?).to be true
|
450
|
+
expect(model.phone_number_as_normalized).to eql('+31101234123')
|
446
451
|
end
|
447
452
|
|
448
453
|
it "should not add a + using :add_plus option" do
|
449
454
|
model_klass.phony_normalize :phone_number, :add_plus => false
|
450
455
|
model = model_klass.new(:phone_number => "+31-(0)10-1234123")
|
451
|
-
model.valid
|
452
|
-
model.phone_number.
|
456
|
+
expect(model.valid?).to be true
|
457
|
+
expect(model.phone_number).to eql('31101234123')
|
453
458
|
end
|
454
459
|
|
455
460
|
it "should raise a RuntimeError at validation if the attribute doesn't exist" do
|
456
461
|
dummy_klass.phony_normalize :non_existing_attribute
|
457
462
|
dummy = dummy_klass.new
|
458
|
-
lambda {
|
463
|
+
expect(lambda {
|
459
464
|
dummy.valid?
|
460
|
-
}.
|
465
|
+
}).to raise_error(RuntimeError)
|
461
466
|
end
|
462
467
|
|
463
468
|
it "should raise a RuntimeError at validation if the :as option attribute doesn't exist" do
|
464
469
|
dummy_klass.phony_normalize :phone_number, :as => :non_existing_attribute
|
465
470
|
dummy = dummy_klass.new
|
466
|
-
lambda {
|
471
|
+
expect(lambda {
|
467
472
|
dummy.valid?
|
468
|
-
}.
|
473
|
+
}).to raise_error(RuntimeError)
|
469
474
|
end
|
470
475
|
end
|
471
476
|
end
|
@@ -477,16 +482,16 @@ describe PhonyRails do
|
|
477
482
|
|
478
483
|
it "should correctly keep a hard set country_code" do
|
479
484
|
model = model_klass.new(:fax_number => '+1 978 555 0000')
|
480
|
-
model.valid
|
481
|
-
model.fax_number.
|
482
|
-
model.save.
|
483
|
-
model.save.
|
485
|
+
expect(model.valid?).to be true
|
486
|
+
expect(model.fax_number).to eql('+19785550000')
|
487
|
+
expect(model.save).to be true
|
488
|
+
expect(model.save).to be true # revalidate
|
484
489
|
model.reload
|
485
|
-
model.fax_number.
|
490
|
+
expect(model.fax_number).to eql('+19785550000')
|
486
491
|
model.fax_number = '(030) 8 61 29 06'
|
487
|
-
model.save.
|
492
|
+
expect(model.save).to be true # revalidate
|
488
493
|
model.reload
|
489
|
-
model.fax_number.
|
494
|
+
expect(model.fax_number).to eql('+61308612906')
|
490
495
|
end
|
491
496
|
end
|
492
497
|
|