phony_rails 0.14.9 → 0.15.0

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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PhonyRails
4
- VERSION = '0.14.9'
4
+ VERSION = '0.15.0'
5
5
  end
@@ -4,6 +4,7 @@
4
4
  # Usage:
5
5
  # validate :phone_number, :phony_plausible => true
6
6
  require 'active_model'
7
+
7
8
  class PhonyPlausibleValidator < ActiveModel::EachValidator
8
9
  # Validates a String using Phony.plausible? method.
9
10
  def validate_each(record, attribute, value)
@@ -13,6 +14,8 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
13
14
  value = PhonyRails.normalize_number(value.dup, default_country_code: normalized_country_code) if normalized_country_code
14
15
  value = PhonyRails.extract_extension(value).first
15
16
  @record.errors.add(attribute, error_message) unless Phony.plausible?(value, cc: country_number)
17
+ @record.public_send("#{attribute}=", @record.public_send("#{attribute}_original")) if @record.respond_to?("#{attribute}_original") &&
18
+ !Phony.plausible?(value, cc: country_number)
16
19
  end
17
20
 
18
21
  private
data/phony_rails.gemspec CHANGED
@@ -16,12 +16,14 @@ Gem::Specification.new do |gem|
16
16
  gem.name = 'phony_rails'
17
17
  gem.require_paths = ['lib']
18
18
  gem.version = PhonyRails::VERSION
19
+ gem.required_ruby_version = '>= 2.4'
19
20
 
20
- gem.post_install_message = 'PhonyRails v0.10.0 changes the way numbers are stored!'
21
- gem.post_install_message = "It now adds a '+' to the normalized number when it starts with a country number!"
21
+ gem.post_install_message = "PhonyRails v0.10.0 changes the way numbers are stored!\nIt now adds a ' + ' to the normalized number when it starts with a country number!"
22
22
 
23
23
  gem.add_runtime_dependency 'activesupport', '>= 3.0'
24
- gem.add_runtime_dependency 'phony', '> 2.15'
24
+ gem.add_runtime_dependency 'phony', '>= 2.18.12'
25
25
  gem.add_development_dependency 'activerecord', '>= 3.0'
26
- gem.add_development_dependency 'mongoid', '>= 3.0'
26
+
27
+ # For testing
28
+ gem.add_development_dependency 'sqlite3', '>= 1.4.0'
27
29
  end
@@ -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
@@ -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
@@ -224,6 +226,36 @@ describe PhonyRails do
224
226
  phone = PhonyRails.normalize_number(phone, default_country_code: 'DE')
225
227
  expect(phone).to eq('+491751234567')
226
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
227
259
  end
228
260
 
229
261
  it 'should not change original String' do
@@ -409,6 +441,7 @@ describe PhonyRails do
409
441
  subject { described_class }
410
442
  let(:valid_number) { '1 555 555 5555' }
411
443
  let(:invalid_number) { '123456789 123456789 123456789 123456789' }
444
+ let(:another_invalid_number) { '441212' }
412
445
  let(:normalizable_number) { '555 555 5555' }
413
446
  let(:formatted_french_number_with_country_code) { '+33 627899541' }
414
447
  let(:empty_number) { '' }
@@ -418,8 +451,11 @@ describe PhonyRails do
418
451
  is_expected.to be_plausible_number valid_number, country_code: 'US'
419
452
  end
420
453
 
421
- 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
422
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'
423
459
  end
424
460
 
425
461
  it 'returns true for a normalizable number' do
@@ -451,6 +487,10 @@ describe PhonyRails do
451
487
  is_expected.not_to be_plausible_number normalizable_number, country_code: 'US'
452
488
  end
453
489
 
490
+ it 'should pass Github issue #95' do
491
+ is_expected.to be_plausible_number '+358414955444', default_country_code: :de
492
+ end
493
+
454
494
  context 'with default_country_code set' do
455
495
  before { PhonyRails.default_country_code = 'FR' }
456
496
  after { PhonyRails.default_country_code = nil }
@@ -472,7 +512,7 @@ describe PhonyRails do
472
512
  after { PhonyRails.default_country_code = nil }
473
513
 
474
514
  it 'can set a global default country code' do
475
- expect(PhonyRails.default_country_code). to eq 'US'
515
+ expect(PhonyRails.default_country_code).to eq 'US'
476
516
  end
477
517
 
478
518
  it 'can set a global default country code' do
@@ -501,6 +541,12 @@ describe PhonyRails do
501
541
  end
502
542
  end
503
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
+
504
550
  describe 'PhonyRails.country_from_number' do
505
551
  it 'returns the country of the plausible phone number' do
506
552
  expect(PhonyRails.country_from_number('+32475000000')).to eq 'BE'
@@ -879,9 +925,9 @@ describe PhonyRails do
879
925
  end
880
926
  end
881
927
 
882
- describe 'Mongoid' do
883
- let(:model_klass) { MongoidModel }
884
- let(:dummy_klass) { MongoidDummy }
885
- it_behaves_like 'model with PhonyRails'
886
- end
928
+ # describe 'Mongoid' do
929
+ # let(:model_klass) { MongoidModel }
930
+ # let(:dummy_klass) { MongoidDummy }
931
+ # it_behaves_like 'model with PhonyRails'
932
+ # end
887
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,7 @@ 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'])
240
272
  end
241
273
  end
242
274
 
@@ -244,7 +276,7 @@ describe PhonyPlausibleValidator do
244
276
  I18n.with_locale(:es) do
245
277
  @home.phone_number = INVALID_NUMBER
246
278
  @home.valid?
247
- expect(@home.errors.messages).to include(phone_number: ['es un número inválido'])
279
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['es un número inválido'])
248
280
  end
249
281
  end
250
282
 
@@ -252,7 +284,7 @@ describe PhonyPlausibleValidator do
252
284
  I18n.with_locale(:fr) do
253
285
  @home.phone_number = INVALID_NUMBER
254
286
  @home.valid?
255
- 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'])
256
288
  end
257
289
  end
258
290
 
@@ -260,7 +292,7 @@ describe PhonyPlausibleValidator do
260
292
  I18n.with_locale(:ja) do
261
293
  @home.phone_number = INVALID_NUMBER
262
294
  @home.valid?
263
- expect(@home.errors.messages).to include(phone_number: ['は正しい電話番号ではありません'])
295
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['は正しい電話番号ではありません'])
264
296
  end
265
297
  end
266
298
 
@@ -268,7 +300,15 @@ describe PhonyPlausibleValidator do
268
300
  I18n.with_locale(:km) do
269
301
  @home.phone_number = INVALID_NUMBER
270
302
  @home.valid?
271
- 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: ['는 올바른 전화번호가 아닙니다'])
272
312
  end
273
313
  end
274
314
 
@@ -276,7 +316,7 @@ describe PhonyPlausibleValidator do
276
316
  I18n.with_locale(:uk) do
277
317
  @home.phone_number = INVALID_NUMBER
278
318
  @home.valid?
279
- expect(@home.errors.messages).to include(phone_number: ['є недійсним номером'])
319
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['є недійсним номером'])
280
320
  end
281
321
  end
282
322
 
@@ -284,7 +324,7 @@ describe PhonyPlausibleValidator do
284
324
  I18n.with_locale(:ru) do
285
325
  @home.phone_number = INVALID_NUMBER
286
326
  @home.valid?
287
- expect(@home.errors.messages).to include(phone_number: ['является недействительным номером'])
327
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['является недействительным номером'])
288
328
  end
289
329
  end
290
330
  end
@@ -312,7 +352,7 @@ describe ActiveModel::Validations::HelperMethods do
312
352
  it 'should invalidate an invalid number' do
313
353
  @home.phone_number = INVALID_NUMBER
314
354
  expect(@home).to_not be_valid
315
- 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'])
316
356
  end
317
357
  end
318
358
 
@@ -324,7 +364,7 @@ describe ActiveModel::Validations::HelperMethods do
324
364
 
325
365
  it 'should invalidate an empty number' do
326
366
  expect(@home).to_not be_valid
327
- 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"])
328
368
  end
329
369
 
330
370
  it 'should validate a valid number' do
@@ -335,7 +375,7 @@ describe ActiveModel::Validations::HelperMethods do
335
375
  it 'should invalidate an invalid number' do
336
376
  @home.phone_number = INVALID_NUMBER
337
377
  expect(@home).to_not be_valid
338
- 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'])
339
379
  end
340
380
  end
341
381
 
@@ -363,7 +403,7 @@ describe ActiveModel::Validations::HelperMethods do
363
403
  it 'should invalidate an invalid number' do
364
404
  @home.phone_number = INVALID_NUMBER
365
405
  expect(@home).to_not be_valid
366
- 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'])
367
407
  end
368
408
  end
369
409
 
@@ -375,7 +415,7 @@ describe ActiveModel::Validations::HelperMethods do
375
415
 
376
416
  it 'should invalidate an empty number' do
377
417
  expect(@home).to_not be_valid
378
- expect(@home.errors.messages).to include(phone_number: ['is invalid'])
418
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is invalid'])
379
419
  end
380
420
 
381
421
  it 'should validate a well formatted valid number' do
@@ -386,7 +426,7 @@ describe ActiveModel::Validations::HelperMethods do
386
426
  it 'should invalidate a bad formatted valid number' do
387
427
  @home.phone_number = VALID_NUMBER
388
428
  expect(@home).to_not be_valid
389
- expect(@home.errors.messages).to include(phone_number: ['is invalid'])
429
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is invalid'])
390
430
  end
391
431
  end
392
432
 
@@ -408,7 +448,7 @@ describe ActiveModel::Validations::HelperMethods do
408
448
  it 'should invalidate a bad formatted valid number' do
409
449
  @home.phone_number = "+#{VALID_NUMBER}"
410
450
  expect(@home).to_not be_valid
411
- expect(@home.errors.messages).to include(phone_number: ['is invalid'])
451
+ expect(@home.errors.messages.to_hash).to include(phone_number: ['is invalid'])
412
452
  end
413
453
  end
414
454
 
@@ -430,13 +470,13 @@ describe ActiveModel::Validations::HelperMethods do
430
470
  it 'should invalidate a valid number with the wrong country code' do
431
471
  @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
432
472
  expect(@home).to_not be_valid
433
- 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'])
434
474
  end
435
475
 
436
476
  it 'should invalidate a valid number without a country code' do
437
477
  @home.phone_number = VALID_NUMBER
438
478
  expect(@home).to_not be_valid
439
- 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'])
440
480
  end
441
481
  end
442
482
 
@@ -463,7 +503,7 @@ describe ActiveModel::Validations::HelperMethods do
463
503
  it 'should invalidate an invalid number' do
464
504
  @home.phone_number = INVALID_NUMBER
465
505
  expect(@home).to_not be_valid
466
- 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'])
467
507
  end
468
508
  end
469
509
 
@@ -485,13 +525,13 @@ describe ActiveModel::Validations::HelperMethods do
485
525
  it 'should invalidate a valid number with the wrong country code' do
486
526
  @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
487
527
  expect(@home).to_not be_valid
488
- 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'])
489
529
  end
490
530
 
491
531
  it 'should invalidate a valid number without a country code' do
492
532
  @home.phone_number = VALID_NUMBER
493
533
  expect(@home).to_not be_valid
494
- 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'])
495
535
  end
496
536
  end
497
537
 
@@ -584,14 +624,20 @@ describe ActiveModel::Validations::HelperMethods do
584
624
  @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
585
625
  @home.phone_number_country_code = 'PL'
586
626
  expect(@home).to_not be_valid
587
- 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'])
588
628
  end
589
629
 
590
630
  it 'should invalidate a valid number without a country code' do
591
631
  @home.phone_number = VALID_NUMBER
592
632
  @home.phone_number_country_code = 'PL'
593
633
  expect(@home).to_not be_valid
594
- 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
595
641
  end
596
642
  end
597
643
 
@@ -626,5 +672,38 @@ describe ActiveModel::Validations::HelperMethods do
626
672
  @home.save
627
673
  end
628
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
629
708
  end
630
709
  end