phony_rails 0.14.7 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,8 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
+ EXT_PREFIXES = %w[ext ex x xt # :].freeze
5
6
  describe PhonyRails do
6
- EXT_PREFIXES = %w[ext ex x xt # :].freeze
7
-
8
7
  it 'should not pollute the global namespace with a Country class' do
9
8
  should_not be_const_defined 'Country'
10
9
  end
@@ -20,7 +19,7 @@ describe PhonyRails do
20
19
  describe 'with the bang!' do
21
20
  it 'changes the String using the bang method' do
22
21
  # Mutable String
23
- s = +'0101234123' rescue '0101234123' # rubocop:disable Style/RescueModifier
22
+ s = +'0101234123' rescue '0101234123'
24
23
  expect(s.phony_formatted!(normalize: :NL, format: :international)).to eql('+31 10 123 4123')
25
24
  expect(s).to eql('+31 10 123 4123')
26
25
  end
@@ -200,17 +199,20 @@ describe PhonyRails do
200
199
  end
201
200
  end
202
201
 
202
+ # rubocop:disable Lint/ConstantDefinitionInBlock
203
203
  class NormalHome < ActiveRecord::Base
204
204
  attr_accessor :phone_number
205
+
205
206
  phony_normalize :phone_number, default_country_code: 'US'
206
207
  validates :phone_number, phony_plausible: true
207
208
  end
209
+ # rubocop:enable Lint/ConstantDefinitionInBlock
208
210
 
209
211
  normal = NormalHome.new
210
212
  normal.phone_number = 'HAHA'
211
213
  expect(normal).to_not be_valid
212
214
  expect(normal.phone_number).to eq('HAHA')
213
- expect(normal.errors.messages).to include(phone_number: ['is an invalid number'])
215
+ expect(normal.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
214
216
  end
215
217
 
216
218
  it 'should pass Github issue #170' do
@@ -218,6 +220,42 @@ describe PhonyRails do
218
220
  phone = PhonyRails.normalize_number(phone)
219
221
  expect(phone).to eq('+491751234567')
220
222
  end
223
+
224
+ it 'should pass Github issue #170 (part 2)' do
225
+ phone = '(+49) 175 123 4567'
226
+ phone = PhonyRails.normalize_number(phone, default_country_code: 'DE')
227
+ expect(phone).to eq('+491751234567')
228
+ end
229
+
230
+ it 'should pass Github issue #175' do
231
+ phone = '0041 23456789'
232
+ phone = PhonyRails.normalize_number(phone, default_country_code: 'DE')
233
+ expect(phone).to eq('+4123456789')
234
+ end
235
+
236
+ it 'should pass Github issue #175' do
237
+ phone = '+1 260-437-9123'
238
+ phone = PhonyRails.normalize_number(phone, default_country_code: 'DE')
239
+ expect(phone).to eq('+12604379123')
240
+ end
241
+
242
+ it 'should pass Github issue #187' do
243
+ phone1 = '0037253400030'
244
+ phone1 = PhonyRails.normalize_number(phone1, default_country_code: 'EE')
245
+ expect(phone1).to eq('+37253400030')
246
+
247
+ phone2 = '0037275016183'
248
+ phone2 = PhonyRails.normalize_number(phone2, default_country_code: 'EE')
249
+ expect(phone2).to eq('+37275016183')
250
+ end
251
+
252
+ it 'should pass Github issue #180' do
253
+ phone = '5555555555'
254
+ phone = PhonyRails.normalize_number(phone, default_country_code: 'AU')
255
+ expect(phone).to eq('+615555555555')
256
+ phone = PhonyRails.normalize_number(phone, default_country_code: 'AU')
257
+ expect(phone).to eq('+615555555555')
258
+ end
221
259
  end
222
260
 
223
261
  it 'should not change original String' do
@@ -403,6 +441,7 @@ describe PhonyRails do
403
441
  subject { described_class }
404
442
  let(:valid_number) { '1 555 555 5555' }
405
443
  let(:invalid_number) { '123456789 123456789 123456789 123456789' }
444
+ let(:another_invalid_number) { '441212' }
406
445
  let(:normalizable_number) { '555 555 5555' }
407
446
  let(:formatted_french_number_with_country_code) { '+33 627899541' }
408
447
  let(:empty_number) { '' }
@@ -412,8 +451,11 @@ describe PhonyRails do
412
451
  is_expected.to be_plausible_number valid_number, country_code: 'US'
413
452
  end
414
453
 
415
- it 'returns false for an invalid number' do
454
+ it 'returns false for an invalid numbers' do
455
+ is_expected.not_to be_plausible_number invalid_number
416
456
  is_expected.not_to be_plausible_number invalid_number, country_code: 'US'
457
+ is_expected.not_to be_plausible_number another_invalid_number
458
+ is_expected.not_to be_plausible_number another_invalid_number, country_code: 'US'
417
459
  end
418
460
 
419
461
  it 'returns true for a normalizable number' do
@@ -445,6 +487,10 @@ describe PhonyRails do
445
487
  is_expected.not_to be_plausible_number normalizable_number, country_code: 'US'
446
488
  end
447
489
 
490
+ it 'should pass Github issue #95' do
491
+ is_expected.to be_plausible_number '+358414955444', default_country_code: :de
492
+ end
493
+
448
494
  context 'with default_country_code set' do
449
495
  before { PhonyRails.default_country_code = 'FR' }
450
496
  after { PhonyRails.default_country_code = nil }
@@ -466,7 +512,7 @@ describe PhonyRails do
466
512
  after { PhonyRails.default_country_code = nil }
467
513
 
468
514
  it 'can set a global default country code' do
469
- expect(PhonyRails.default_country_code). to eq 'US'
515
+ expect(PhonyRails.default_country_code).to eq 'US'
470
516
  end
471
517
 
472
518
  it 'can set a global default country code' do
@@ -495,6 +541,12 @@ describe PhonyRails do
495
541
  end
496
542
  end
497
543
 
544
+ describe 'PhonyRails.country_code_from_number' do
545
+ it 'returns the code of the plausible phone number' do
546
+ expect(PhonyRails.country_code_from_number('+32475000000')).to eq '32'
547
+ end
548
+ end
549
+
498
550
  describe 'PhonyRails.country_from_number' do
499
551
  it 'returns the country of the plausible phone number' do
500
552
  expect(PhonyRails.country_from_number('+32475000000')).to eq 'BE'
@@ -722,6 +774,30 @@ describe PhonyRails do
722
774
  expect(model.symboled_phone).to eql('+33606060606')
723
775
  end
724
776
 
777
+ context 'relational normalization' do
778
+ it 'should normalize based on custom attribute of the current model' do
779
+ model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.custom_country_code }
780
+ model = model_klass.new phone_number: '012 416 0001', custom_country_code: 'MY'
781
+ expect(model).to be_valid
782
+ expect(model.phone_number).to eq('+60124160001')
783
+ end
784
+
785
+ it 'should normalize based on specific attribute of the associative model' do
786
+ model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.home_country.country_code }
787
+ home_country = double('home_country', country_code: 'MY')
788
+ model = model_klass.new phone_number: '012 416 0001', home_country: home_country
789
+ expect(model).to be_valid
790
+ expect(model.phone_number).to eq('+60124160001')
791
+ end
792
+
793
+ it 'should normalize based on default value if missing associative model' do
794
+ model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.home_country&.country_code || 'MY' }
795
+ model = model_klass.new phone_number: '012 416 0001', home_country: nil
796
+ expect(model).to be_valid
797
+ expect(model.phone_number).to eq('+60124160001')
798
+ end
799
+ end
800
+
725
801
  context 'conditional normalization' do
726
802
  context 'standalone methods' do
727
803
  it 'should only normalize if the :if conditional is true' do
@@ -849,9 +925,9 @@ describe PhonyRails do
849
925
  end
850
926
  end
851
927
 
852
- describe 'Mongoid' do
853
- let(:model_klass) { MongoidModel }
854
- let(:dummy_klass) { MongoidDummy }
855
- it_behaves_like 'model with PhonyRails'
856
- end
928
+ # describe 'Mongoid' do
929
+ # let(:model_klass) { MongoidModel }
930
+ # let(:dummy_klass) { MongoidDummy }
931
+ # it_behaves_like 'model with PhonyRails'
932
+ # end
857
933
  end
@@ -60,71 +60,86 @@ ActiveRecord::Schema.define do
60
60
  table.column :phone_number, :string
61
61
  table.column :phone_number_country_code, :string
62
62
  end
63
+
64
+ create_table :normalizabled_phone_homes do |table|
65
+ table.column :phone_number, :string
66
+ end
63
67
  end
64
68
 
65
69
  #--------------------
66
70
  class SimpleHome < ActiveRecord::Base
67
71
  attr_accessor :phone_number
72
+
68
73
  validates :phone_number, phony_plausible: true
69
74
  end
70
75
 
71
76
  #--------------------
72
77
  class HelpfulHome < ActiveRecord::Base
73
78
  attr_accessor :phone_number
79
+
74
80
  validates_plausible_phone :phone_number
75
81
  end
76
82
 
77
83
  #--------------------
78
84
  class RequiredHelpfulHome < ActiveRecord::Base
79
85
  attr_accessor :phone_number
86
+
80
87
  validates_plausible_phone :phone_number, presence: true
81
88
  end
82
89
 
83
90
  #--------------------
84
91
  class OptionalHelpfulHome < ActiveRecord::Base
85
92
  attr_accessor :phone_number
93
+
86
94
  validates_plausible_phone :phone_number, presence: false
87
95
  end
88
96
 
89
97
  #--------------------
90
98
  class FormattedHelpfulHome < ActiveRecord::Base
91
99
  attr_accessor :phone_number
100
+
92
101
  validates_plausible_phone :phone_number, with: /\A\+\d+/
93
102
  end
94
103
 
95
104
  #--------------------
96
105
  class NotFormattedHelpfulHome < ActiveRecord::Base
97
106
  attr_accessor :phone_number
107
+
98
108
  validates_plausible_phone :phone_number, without: /\A\+\d+/
99
109
  end
100
110
 
101
111
  #--------------------
102
112
  class NormalizableHelpfulHome < ActiveRecord::Base
103
113
  attr_accessor :phone_number
114
+
104
115
  validates_plausible_phone :phone_number, normalized_country_code: 'US'
105
116
  end
106
117
 
107
118
  #--------------------
108
119
  class AustralianHelpfulHome < ActiveRecord::Base
109
120
  attr_accessor :phone_number
121
+
110
122
  validates_plausible_phone :phone_number, country_number: '61'
111
123
  end
112
124
 
113
125
  #--------------------
114
126
  class PolishHelpfulHome < ActiveRecord::Base
115
127
  attr_accessor :phone_number
128
+
116
129
  validates_plausible_phone :phone_number, country_code: 'PL'
117
130
  end
118
131
 
119
132
  #--------------------
120
133
  class BigHelpfulHome < ActiveRecord::Base
121
134
  attr_accessor :phone_number
135
+
122
136
  validates_plausible_phone :phone_number, presence: true, with: /\A\+\d+/, country_number: '33'
123
137
  end
124
138
 
125
139
  #--------------------
126
140
  class MismatchedHelpfulHome < ActiveRecord::Base
127
141
  attr_accessor :phone_number, :country_code
142
+
128
143
  validates :phone_number, phony_plausible: { ignore_record_country_code: true }
129
144
  end
130
145
 
@@ -132,6 +147,7 @@ end
132
147
 
133
148
  class InvalidCountryCodeHelpfulHome < ActiveRecord::Base
134
149
  attr_accessor :phone_number
150
+
135
151
  validates_plausible_phone :phone_number
136
152
 
137
153
  def country_code
@@ -142,30 +158,45 @@ end
142
158
  #--------------------
143
159
  class SymbolizableHelpfulHome < ActiveRecord::Base
144
160
  attr_accessor :phone_number, :phone_number_country_code
161
+
145
162
  validates_plausible_phone :phone_number, country_code: :phone_number_country_code
146
163
  end
147
164
 
148
165
  #--------------------
149
166
  class NoModelMethod < HelpfulHome
150
167
  attr_accessor :phone_number
168
+
151
169
  validates_plausible_phone :phone_number, country_code: :nonexistent_method
152
170
  end
153
171
 
154
172
  #--------------------
155
173
  class MessageOptionUndefinedInModel < HelpfulHome
156
174
  attr_accessor :phone_number
175
+
157
176
  validates_plausible_phone :phone_number, message: :email
158
177
  end
159
178
 
160
179
  #--------------------
161
180
  class MessageOptionSameAsModelMethod < HelpfulHome
162
181
  attr_accessor :phone_number
182
+
163
183
  validates_plausible_phone :phone_number, message: :email
164
184
 
165
185
  def email
166
186
  'user@example.com'
167
187
  end
168
188
  end
189
+
190
+ #--------------------
191
+ class NormalizabledPhoneHome < ActiveRecord::Base
192
+ attr_accessor :phone_number, :phone_number2, :country_code
193
+
194
+ validates_plausible_phone :phone_number
195
+ validates_plausible_phone :phone_number2
196
+ phony_normalize :phone_number, country_code: 'PL', normalize_when_valid: true
197
+ phony_normalize :phone_number2, country_code: 'PL', normalize_when_valid: true
198
+ end
199
+
169
200
  #-----------------------------------------------------------------------------------------------------------------------
170
201
  # Tests
171
202
  #-----------------------------------------------------------------------------------------------------------------------
@@ -180,6 +211,7 @@ POLISH_NUMBER_WITH_COUNTRY_CODE = '48600600600'
180
211
  FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '+61 390133997'
181
212
  FRENCH_NUMBER_WITH_COUNTRY_CODE = '33627899541'
182
213
  FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE = '+33 627899541'
214
+ CROATIA_NUMBER_WITH_COUNTRY_CODE = '385 98 352 085'
183
215
  INVALID_NUMBER = '123456789 123456789 123456789 123456789'
184
216
  NOT_A_NUMBER = 'HAHA'
185
217
  JAPAN_COUNTRY = 'jp'
@@ -209,26 +241,26 @@ describe PhonyPlausibleValidator do
209
241
  it 'should invalidate an invalid number' do
210
242
  @home.phone_number = INVALID_NUMBER
211
243
  expect(@home).to_not be_valid
212
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
244
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
213
245
  end
214
246
 
215
247
  it 'should invalidate an valid number with invalid extension' do
216
248
  @home.phone_number = VALID_NUMBER_WITH_INVALID_EXTENSION
217
249
  expect(@home).to_not be_valid
218
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
250
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
219
251
  end
220
252
 
221
253
  it 'should invalidate not a number' do
222
254
  @home.phone_number = NOT_A_NUMBER
223
255
  expect(@home).to_not be_valid
224
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
256
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
225
257
  end
226
258
 
227
259
  it 'should translate the error message in Dutch' do
228
260
  I18n.with_locale(:nl) do
229
261
  @home.phone_number = INVALID_NUMBER
230
262
  @home.valid?
231
- expect(@home.errors.messages).to include(phone_number: ['is geen geldig nummer'])
263
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is geen geldig nummer'])
232
264
  end
233
265
  end
234
266
 
@@ -236,7 +268,15 @@ describe PhonyPlausibleValidator do
236
268
  I18n.with_locale(:en) do
237
269
  @home.phone_number = INVALID_NUMBER
238
270
  @home.valid?
239
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
271
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
272
+ end
273
+ end
274
+
275
+ it 'should translate the error message in Spanish' do
276
+ I18n.with_locale(:es) do
277
+ @home.phone_number = INVALID_NUMBER
278
+ @home.valid?
279
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['es un número inválido'])
240
280
  end
241
281
  end
242
282
 
@@ -244,7 +284,7 @@ describe PhonyPlausibleValidator do
244
284
  I18n.with_locale(:fr) do
245
285
  @home.phone_number = INVALID_NUMBER
246
286
  @home.valid?
247
- expect(@home.errors.messages).to include(phone_number: ['est un numéro invalide'])
287
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['est un numéro invalide'])
248
288
  end
249
289
  end
250
290
 
@@ -252,7 +292,7 @@ describe PhonyPlausibleValidator do
252
292
  I18n.with_locale(:ja) do
253
293
  @home.phone_number = INVALID_NUMBER
254
294
  @home.valid?
255
- expect(@home.errors.messages).to include(phone_number: ['は正しい電話番号ではありません'])
295
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['は正しい電話番号ではありません'])
256
296
  end
257
297
  end
258
298
 
@@ -260,7 +300,15 @@ describe PhonyPlausibleValidator do
260
300
  I18n.with_locale(:km) do
261
301
  @home.phone_number = INVALID_NUMBER
262
302
  @home.valid?
263
- expect(@home.errors.messages).to include(phone_number: ['គឺជាលេខមិនត្រឹមត្រូវ'])
303
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['គឺជាលេខមិនត្រឹមត្រូវ'])
304
+ end
305
+ end
306
+
307
+ it 'should translate the error message in Korean' do
308
+ I18n.with_locale(:ko) do
309
+ @home.phone_number = INVALID_NUMBER
310
+ @home.valid?
311
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['는 올바른 전화번호가 아닙니다'])
264
312
  end
265
313
  end
266
314
 
@@ -268,7 +316,7 @@ describe PhonyPlausibleValidator do
268
316
  I18n.with_locale(:uk) do
269
317
  @home.phone_number = INVALID_NUMBER
270
318
  @home.valid?
271
- expect(@home.errors.messages).to include(phone_number: ['є недійсним номером'])
319
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['є недійсним номером'])
272
320
  end
273
321
  end
274
322
 
@@ -276,7 +324,7 @@ describe PhonyPlausibleValidator do
276
324
  I18n.with_locale(:ru) do
277
325
  @home.phone_number = INVALID_NUMBER
278
326
  @home.valid?
279
- expect(@home.errors.messages).to include(phone_number: ['является недействительным номером'])
327
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['является недействительным номером'])
280
328
  end
281
329
  end
282
330
  end
@@ -304,7 +352,7 @@ describe ActiveModel::Validations::HelperMethods do
304
352
  it 'should invalidate an invalid number' do
305
353
  @home.phone_number = INVALID_NUMBER
306
354
  expect(@home).to_not be_valid
307
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
355
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
308
356
  end
309
357
  end
310
358
 
@@ -316,7 +364,7 @@ describe ActiveModel::Validations::HelperMethods do
316
364
 
317
365
  it 'should invalidate an empty number' do
318
366
  expect(@home).to_not be_valid
319
- expect(@home.errors.messages).to include(phone_number: ["can't be blank"])
367
+ expect(@home.errors.messages.to_hash).to include(phone_number: ["can't be blank"])
320
368
  end
321
369
 
322
370
  it 'should validate a valid number' do
@@ -327,7 +375,7 @@ describe ActiveModel::Validations::HelperMethods do
327
375
  it 'should invalidate an invalid number' do
328
376
  @home.phone_number = INVALID_NUMBER
329
377
  expect(@home).to_not be_valid
330
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
378
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
331
379
  end
332
380
  end
333
381
 
@@ -355,7 +403,7 @@ describe ActiveModel::Validations::HelperMethods do
355
403
  it 'should invalidate an invalid number' do
356
404
  @home.phone_number = INVALID_NUMBER
357
405
  expect(@home).to_not be_valid
358
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
406
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
359
407
  end
360
408
  end
361
409
 
@@ -367,7 +415,7 @@ describe ActiveModel::Validations::HelperMethods do
367
415
 
368
416
  it 'should invalidate an empty number' do
369
417
  expect(@home).to_not be_valid
370
- expect(@home.errors.messages).to include(phone_number: ['is invalid'])
418
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is invalid'])
371
419
  end
372
420
 
373
421
  it 'should validate a well formatted valid number' do
@@ -378,7 +426,7 @@ describe ActiveModel::Validations::HelperMethods do
378
426
  it 'should invalidate a bad formatted valid number' do
379
427
  @home.phone_number = VALID_NUMBER
380
428
  expect(@home).to_not be_valid
381
- expect(@home.errors.messages).to include(phone_number: ['is invalid'])
429
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is invalid'])
382
430
  end
383
431
  end
384
432
 
@@ -400,7 +448,7 @@ describe ActiveModel::Validations::HelperMethods do
400
448
  it 'should invalidate a bad formatted valid number' do
401
449
  @home.phone_number = "+#{VALID_NUMBER}"
402
450
  expect(@home).to_not be_valid
403
- expect(@home.errors.messages).to include(phone_number: ['is invalid'])
451
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is invalid'])
404
452
  end
405
453
  end
406
454
 
@@ -422,13 +470,13 @@ describe ActiveModel::Validations::HelperMethods do
422
470
  it 'should invalidate a valid number with the wrong country code' do
423
471
  @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
424
472
  expect(@home).to_not be_valid
425
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
473
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
426
474
  end
427
475
 
428
476
  it 'should invalidate a valid number without a country code' do
429
477
  @home.phone_number = VALID_NUMBER
430
478
  expect(@home).to_not be_valid
431
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
479
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
432
480
  end
433
481
  end
434
482
 
@@ -455,7 +503,7 @@ describe ActiveModel::Validations::HelperMethods do
455
503
  it 'should invalidate an invalid number' do
456
504
  @home.phone_number = INVALID_NUMBER
457
505
  expect(@home).to_not be_valid
458
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
506
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
459
507
  end
460
508
  end
461
509
 
@@ -477,13 +525,13 @@ describe ActiveModel::Validations::HelperMethods do
477
525
  it 'should invalidate a valid number with the wrong country code' do
478
526
  @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
479
527
  expect(@home).to_not be_valid
480
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
528
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
481
529
  end
482
530
 
483
531
  it 'should invalidate a valid number without a country code' do
484
532
  @home.phone_number = VALID_NUMBER
485
533
  expect(@home).to_not be_valid
486
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
534
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
487
535
  end
488
536
  end
489
537
 
@@ -576,14 +624,20 @@ describe ActiveModel::Validations::HelperMethods do
576
624
  @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
577
625
  @home.phone_number_country_code = 'PL'
578
626
  expect(@home).to_not be_valid
579
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
627
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
580
628
  end
581
629
 
582
630
  it 'should invalidate a valid number without a country code' do
583
631
  @home.phone_number = VALID_NUMBER
584
632
  @home.phone_number_country_code = 'PL'
585
633
  expect(@home).to_not be_valid
586
- expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
634
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is an invalid number'])
635
+ end
636
+
637
+ it 'should pass Gitlab issue #165' do
638
+ @home.phone_number = CROATIA_NUMBER_WITH_COUNTRY_CODE
639
+ @home.phone_number_country_code = 'HR'
640
+ expect(@home).to be_valid
587
641
  end
588
642
  end
589
643
 
@@ -618,5 +672,38 @@ describe ActiveModel::Validations::HelperMethods do
618
672
  @home.save
619
673
  end
620
674
  end
675
+
676
+ context 'when a number has already code_number' do
677
+ it 'does not normalize code after validation' do
678
+ @home = NormalizabledPhoneHome.new
679
+ @home.phone_number = '+44 799 449 595'
680
+ @home.country_code = 'PL'
681
+
682
+ expect(@home).to_not be_valid
683
+ expect(@home.phone_number).to eql('+44 799 449 595')
684
+
685
+ @home.phone_number = '+48 799 449 595'
686
+
687
+ expect(@home).to be_valid
688
+ expect(@home.phone_number).to eql('+48799449595')
689
+ end
690
+
691
+ it 'does not normalize code after validation with multiple attributes' do
692
+ @home = NormalizabledPhoneHome.new
693
+ @home.phone_number = '+44 799 449 595'
694
+ @home.phone_number2 = '+44 222 111 333'
695
+ @home.country_code = 'PL'
696
+
697
+ expect(@home).to_not be_valid
698
+ expect(@home.phone_number).to eql('+44 799 449 595')
699
+ expect(@home.phone_number2).to eql('+44 222 111 333')
700
+
701
+ @home.phone_number = '+48 799 449 595'
702
+ @home.phone_number2 = '+48 222 111 333'
703
+ expect(@home).to be_valid
704
+ expect(@home.phone_number).to eql('+48799449595')
705
+ expect(@home.phone_number2).to eql('+48222111333')
706
+ end
707
+ end
621
708
  end
622
709
  end
data/spec/spec_helper.rb CHANGED
@@ -9,7 +9,7 @@ require 'rubygems'
9
9
  require 'bundler/setup'
10
10
 
11
11
  require 'active_record'
12
- require 'mongoid'
12
+ # require 'mongoid'
13
13
  require 'phony_rails'
14
14
 
15
15
  ActiveRecord::Base.establish_connection(
@@ -31,7 +31,11 @@ end
31
31
  module SharedModelMethods
32
32
  extend ActiveSupport::Concern
33
33
  included do
34
- attr_accessor :phone_method, :phone1_method, :symboled_phone_method, :country_code, :country_code_attribute, :recipient, :delivery_method
34
+ attr_accessor(
35
+ :country_code, :country_code_attribute, :custom_country_code, :delivery_method,
36
+ :home_country, :phone_method, :phone1_method, :recipient, :symboled_phone_method
37
+ )
38
+
35
39
  phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
36
40
  phony_normalized_method :phone_method # adds normalized_phone_method method
37
41
  phony_normalized_method :phone1_method, default_country_code: 'DE' # adds normalized_phone_method method
@@ -77,20 +81,20 @@ end
77
81
  class ActiveModelDummy < ActiveModelModel
78
82
  end
79
83
 
80
- class MongoidModel
81
- include Mongoid::Document
82
- include Mongoid::Phony
83
- field :phone_attribute, type: String
84
- field :phone_number, type: String
85
- field :phone_number_as_normalized, type: String
86
- field :fax_number
87
- field :country_code_attribute, type: String
88
- field :symboled_phone, type: String
89
- include SharedModelMethods
90
- end
84
+ # class MongoidModel
85
+ # include Mongoid::Document
86
+ # include Mongoid::Phony
87
+ # field :phone_attribute, type: String
88
+ # field :phone_number, type: String
89
+ # field :phone_number_as_normalized, type: String
90
+ # field :fax_number
91
+ # field :country_code_attribute, type: String
92
+ # field :symboled_phone, type: String
93
+ # include SharedModelMethods
94
+ # end
91
95
 
92
- class MongoidDummy < MongoidModel
93
- end
96
+ # class MongoidDummy < MongoidModel
97
+ # end
94
98
 
95
99
  I18n.config.enforce_available_locales = true
96
100