phony_rails 0.12.10 → 0.12.11
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/Gemfile.lock +34 -37
- data/README.md +85 -49
- data/lib/phony_rails.rb +7 -1
- data/lib/phony_rails/locales/ru.yml +4 -0
- data/lib/phony_rails/locales/uk.yml +4 -0
- data/lib/phony_rails/string_extensions.rb +1 -0
- data/lib/phony_rails/version.rb +1 -1
- data/phony_rails.gemspec +1 -1
- data/spec/lib/phony_rails_spec.rb +132 -94
- data/spec/lib/validators/phony_validator_spec.rb +43 -27
- data/spec/spec_helper.rb +9 -9
- metadata +5 -3
@@ -55,7 +55,7 @@ end
|
|
55
55
|
#--------------------
|
56
56
|
class SimpleHome < ActiveRecord::Base
|
57
57
|
attr_accessor :phone_number
|
58
|
-
validates :phone_number, :
|
58
|
+
validates :phone_number, phony_plausible: true
|
59
59
|
end
|
60
60
|
|
61
61
|
#--------------------
|
@@ -67,55 +67,55 @@ end
|
|
67
67
|
#--------------------
|
68
68
|
class RequiredHelpfulHome < ActiveRecord::Base
|
69
69
|
attr_accessor :phone_number
|
70
|
-
validates_plausible_phone :phone_number, :
|
70
|
+
validates_plausible_phone :phone_number, presence: true
|
71
71
|
end
|
72
72
|
|
73
73
|
#--------------------
|
74
74
|
class OptionalHelpfulHome < ActiveRecord::Base
|
75
75
|
attr_accessor :phone_number
|
76
|
-
validates_plausible_phone :phone_number, :
|
76
|
+
validates_plausible_phone :phone_number, presence: false
|
77
77
|
end
|
78
78
|
|
79
79
|
#--------------------
|
80
80
|
class FormattedHelpfulHome < ActiveRecord::Base
|
81
81
|
attr_accessor :phone_number
|
82
|
-
validates_plausible_phone :phone_number, :
|
82
|
+
validates_plausible_phone :phone_number, with: /\A\+\d+/
|
83
83
|
end
|
84
84
|
|
85
85
|
#--------------------
|
86
86
|
class NotFormattedHelpfulHome < ActiveRecord::Base
|
87
87
|
attr_accessor :phone_number
|
88
|
-
validates_plausible_phone :phone_number, :
|
88
|
+
validates_plausible_phone :phone_number, without: /\A\+\d+/
|
89
89
|
end
|
90
90
|
|
91
91
|
#--------------------
|
92
92
|
class NormalizableHelpfulHome < ActiveRecord::Base
|
93
93
|
attr_accessor :phone_number
|
94
|
-
validates_plausible_phone :phone_number, :
|
94
|
+
validates_plausible_phone :phone_number, normalized_country_code: 'US'
|
95
95
|
end
|
96
96
|
|
97
97
|
#--------------------
|
98
98
|
class AustralianHelpfulHome < ActiveRecord::Base
|
99
99
|
attr_accessor :phone_number
|
100
|
-
validates_plausible_phone :phone_number, :
|
100
|
+
validates_plausible_phone :phone_number, country_number: "61"
|
101
101
|
end
|
102
102
|
|
103
103
|
#--------------------
|
104
104
|
class PolishHelpfulHome < ActiveRecord::Base
|
105
105
|
attr_accessor :phone_number
|
106
|
-
validates_plausible_phone :phone_number, :
|
106
|
+
validates_plausible_phone :phone_number, country_code: "PL"
|
107
107
|
end
|
108
108
|
|
109
109
|
#--------------------
|
110
110
|
class BigHelpfulHome < ActiveRecord::Base
|
111
111
|
attr_accessor :phone_number
|
112
|
-
validates_plausible_phone :phone_number, :
|
112
|
+
validates_plausible_phone :phone_number, presence: true, with: /\A\+\d+/, country_number: "33"
|
113
113
|
end
|
114
114
|
|
115
115
|
#--------------------
|
116
116
|
class MismatchedHelpfulHome < ActiveRecord::Base
|
117
117
|
attr_accessor :phone_number, :country_code
|
118
|
-
validates :phone_number, :
|
118
|
+
validates :phone_number, phony_plausible: {ignore_record_country_code: true}
|
119
119
|
end
|
120
120
|
#-----------------------------------------------------------------------------------------------------------------------
|
121
121
|
# Tests
|
@@ -154,14 +154,14 @@ describe PhonyPlausibleValidator do
|
|
154
154
|
it "should invalidate an invalid number" do
|
155
155
|
@home.phone_number = INVALID_NUMBER
|
156
156
|
expect(@home).to_not be_valid
|
157
|
-
expect(@home.errors.messages).to include(:
|
157
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
158
158
|
end
|
159
159
|
|
160
160
|
it "should translate the error message in English" do
|
161
161
|
I18n.with_locale(:en) do
|
162
162
|
@home.phone_number = INVALID_NUMBER
|
163
163
|
@home.valid?
|
164
|
-
expect(@home.errors.messages).to include(:
|
164
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
@@ -169,7 +169,7 @@ describe PhonyPlausibleValidator do
|
|
169
169
|
I18n.with_locale(:fr) do
|
170
170
|
@home.phone_number = INVALID_NUMBER
|
171
171
|
@home.valid?
|
172
|
-
expect(@home.errors.messages).to include(:
|
172
|
+
expect(@home.errors.messages).to include(phone_number: ["est un numéro invalide"])
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
@@ -177,7 +177,7 @@ describe PhonyPlausibleValidator do
|
|
177
177
|
I18n.with_locale(:ja) do
|
178
178
|
@home.phone_number = INVALID_NUMBER
|
179
179
|
@home.valid?
|
180
|
-
expect(@home.errors.messages).to include(:
|
180
|
+
expect(@home.errors.messages).to include(phone_number: ["は正しい電話番号ではありません"])
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
@@ -185,7 +185,23 @@ describe PhonyPlausibleValidator do
|
|
185
185
|
I18n.with_locale(:km) do
|
186
186
|
@home.phone_number = INVALID_NUMBER
|
187
187
|
@home.valid?
|
188
|
-
expect(@home.errors.messages).to include(:
|
188
|
+
expect(@home.errors.messages).to include(phone_number: ["គឺជាលេខមិនត្រឹមត្រូវ"])
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should translate the error message in Ukrainian" do
|
193
|
+
I18n.with_locale(:uk) do
|
194
|
+
@home.phone_number = INVALID_NUMBER
|
195
|
+
@home.valid?
|
196
|
+
expect(@home.errors.messages).to include(phone_number: ["є недійсним номером"])
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should translate the error message in Russian" do
|
201
|
+
I18n.with_locale(:ru) do
|
202
|
+
@home.phone_number = INVALID_NUMBER
|
203
|
+
@home.valid?
|
204
|
+
expect(@home.errors.messages).to include(phone_number: ["является недействительным номером"])
|
189
205
|
end
|
190
206
|
end
|
191
207
|
end
|
@@ -216,7 +232,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
216
232
|
it "should invalidate an invalid number" do
|
217
233
|
@home.phone_number = INVALID_NUMBER
|
218
234
|
expect(@home).to_not be_valid
|
219
|
-
expect(@home.errors.messages).to include(:
|
235
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
220
236
|
end
|
221
237
|
|
222
238
|
end
|
@@ -230,7 +246,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
230
246
|
|
231
247
|
it "should invalidate an empty number" do
|
232
248
|
expect(@home).to_not be_valid
|
233
|
-
expect(@home.errors.messages).to include(:
|
249
|
+
expect(@home.errors.messages).to include(phone_number: ["can't be blank"])
|
234
250
|
end
|
235
251
|
|
236
252
|
it "should validate a valid number" do
|
@@ -241,7 +257,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
241
257
|
it "should invalidate an invalid number" do
|
242
258
|
@home.phone_number = INVALID_NUMBER
|
243
259
|
expect(@home).to_not be_valid
|
244
|
-
expect(@home.errors.messages).to include(:
|
260
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
245
261
|
end
|
246
262
|
|
247
263
|
end
|
@@ -265,7 +281,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
265
281
|
it "should invalidate an invalid number" do
|
266
282
|
@home.phone_number = INVALID_NUMBER
|
267
283
|
expect(@home).to_not be_valid
|
268
|
-
expect(@home.errors.messages).to include(:
|
284
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
269
285
|
end
|
270
286
|
|
271
287
|
end
|
@@ -279,7 +295,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
279
295
|
|
280
296
|
it "should invalidate an empty number" do
|
281
297
|
expect(@home).to_not be_valid
|
282
|
-
expect(@home.errors.messages).to include(:
|
298
|
+
expect(@home.errors.messages).to include(phone_number: ["is invalid"])
|
283
299
|
end
|
284
300
|
|
285
301
|
it "should validate a well formatted valid number" do
|
@@ -290,7 +306,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
290
306
|
it "should invalidate a bad formatted valid number" do
|
291
307
|
@home.phone_number = VALID_NUMBER
|
292
308
|
expect(@home).to_not be_valid
|
293
|
-
expect(@home.errors.messages).to include(:
|
309
|
+
expect(@home.errors.messages).to include(phone_number: ["is invalid"])
|
294
310
|
end
|
295
311
|
|
296
312
|
end
|
@@ -314,7 +330,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
314
330
|
it "should invalidate a bad formatted valid number" do
|
315
331
|
@home.phone_number = "+#{VALID_NUMBER}"
|
316
332
|
expect(@home).to_not be_valid
|
317
|
-
expect(@home.errors.messages).to include(:
|
333
|
+
expect(@home.errors.messages).to include(phone_number: ["is invalid"])
|
318
334
|
end
|
319
335
|
|
320
336
|
end
|
@@ -338,13 +354,13 @@ describe ActiveModel::Validations::HelperMethods do
|
|
338
354
|
it "should invalidate a valid number with the wrong country code" do
|
339
355
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
340
356
|
expect(@home).to_not be_valid
|
341
|
-
expect(@home.errors.messages).to include(:
|
357
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
342
358
|
end
|
343
359
|
|
344
360
|
it "should invalidate a valid number without a country code" do
|
345
361
|
@home.phone_number = VALID_NUMBER
|
346
362
|
expect(@home).to_not be_valid
|
347
|
-
expect(@home.errors.messages).to include(:
|
363
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
348
364
|
end
|
349
365
|
|
350
366
|
end
|
@@ -373,7 +389,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
373
389
|
it "should invalidate an invalid number" do
|
374
390
|
@home.phone_number = INVALID_NUMBER
|
375
391
|
expect(@home).to_not be_valid
|
376
|
-
expect(@home.errors.messages).to include(:
|
392
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
377
393
|
end
|
378
394
|
|
379
395
|
end
|
@@ -397,13 +413,13 @@ describe ActiveModel::Validations::HelperMethods do
|
|
397
413
|
it "should invalidate a valid number with the wrong country code" do
|
398
414
|
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
399
415
|
expect(@home).to_not be_valid
|
400
|
-
expect(@home.errors.messages).to include(:
|
416
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
401
417
|
end
|
402
418
|
|
403
419
|
it "should invalidate a valid number without a country code" do
|
404
420
|
@home.phone_number = VALID_NUMBER
|
405
421
|
expect(@home).to_not be_valid
|
406
|
-
expect(@home.errors.messages).to include(:
|
422
|
+
expect(@home.errors.messages).to include(phone_number: ["is an invalid number"])
|
407
423
|
end
|
408
424
|
|
409
425
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,8 +11,8 @@ require 'mongoid'
|
|
11
11
|
require 'phony_rails'
|
12
12
|
|
13
13
|
ActiveRecord::Base.establish_connection(
|
14
|
-
:
|
15
|
-
:
|
14
|
+
adapter: "sqlite3",
|
15
|
+
database: ":memory:"
|
16
16
|
)
|
17
17
|
|
18
18
|
ActiveRecord::Schema.define do
|
@@ -30,9 +30,9 @@ module SharedModelMethods
|
|
30
30
|
attr_accessor :phone_method, :phone1_method, :country_code
|
31
31
|
phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
|
32
32
|
phony_normalized_method :phone_method # adds normalized_phone_method method
|
33
|
-
phony_normalized_method :phone1_method, :
|
33
|
+
phony_normalized_method :phone1_method, default_country_code: 'DE' # adds normalized_phone_method method
|
34
34
|
phony_normalize :phone_number # normalized on validation
|
35
|
-
phony_normalize :fax_number, :
|
35
|
+
phony_normalize :fax_number, default_country_code: 'AU'
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -44,7 +44,7 @@ class RelaxedActiveRecordModel < ActiveRecord::Base
|
|
44
44
|
self.table_name = 'active_record_models'
|
45
45
|
attr_accessor :phone_number, :country_code
|
46
46
|
|
47
|
-
phony_normalize :phone_number, :
|
47
|
+
phony_normalize :phone_number, enforce_record_country: false
|
48
48
|
end
|
49
49
|
|
50
50
|
class ActiveRecordDummy < ActiveRecordModel
|
@@ -53,9 +53,9 @@ end
|
|
53
53
|
class MongoidModel
|
54
54
|
include Mongoid::Document
|
55
55
|
include Mongoid::Phony
|
56
|
-
field :phone_attribute, :
|
57
|
-
field :phone_number, :
|
58
|
-
field :phone_number_as_normalized, :
|
56
|
+
field :phone_attribute, type: String
|
57
|
+
field :phone_number, type: String
|
58
|
+
field :phone_number_as_normalized, type: String
|
59
59
|
field :fax_number
|
60
60
|
include SharedModelMethods
|
61
61
|
end
|
@@ -67,4 +67,4 @@ I18n.config.enforce_available_locales = true
|
|
67
67
|
|
68
68
|
RSpec.configure do |config|
|
69
69
|
# some (optional) config here
|
70
|
-
end
|
70
|
+
end
|
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.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joost Hietbrink
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phony
|
@@ -93,7 +93,9 @@ 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/ru.yml
|
96
97
|
- lib/phony_rails/locales/tr.yml
|
98
|
+
- lib/phony_rails/locales/uk.yml
|
97
99
|
- lib/phony_rails/string_extensions.rb
|
98
100
|
- lib/phony_rails/version.rb
|
99
101
|
- lib/validators/phony_validator.rb
|
@@ -105,7 +107,7 @@ homepage: https://github.com/joost/phony_rails
|
|
105
107
|
licenses:
|
106
108
|
- MIT
|
107
109
|
metadata: {}
|
108
|
-
post_install_message: It now
|
110
|
+
post_install_message: It now adds a '+' to the normalized number when it starts with
|
109
111
|
a country number!
|
110
112
|
rdoc_options: []
|
111
113
|
require_paths:
|