phony_rails 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +5 -0
- data/lib/phony_rails/version.rb +1 -1
- data/lib/validators/phony_validator.rb +6 -0
- data/spec/lib/validators/phony_validator_spec.rb +40 -0
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -91,6 +91,11 @@ or correct country code:
|
|
91
91
|
|
92
92
|
validates_plausible_phone :phone_number, :country_code => 'AU'
|
93
93
|
|
94
|
+
You can validate against the normalized input as opposed to the raw input:
|
95
|
+
|
96
|
+
phony_normalize :phone_number, as: :phone_number_normalized, :default_country_code => 'US'
|
97
|
+
validates_plausible_phone :phone_number, :normalized_country_code => 'US'
|
98
|
+
|
94
99
|
### Display / Views
|
95
100
|
|
96
101
|
In your views use:
|
data/lib/phony_rails/version.rb
CHANGED
@@ -8,6 +8,8 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
|
|
8
8
|
return if value.blank?
|
9
9
|
|
10
10
|
@record = record
|
11
|
+
|
12
|
+
value = PhonyRails.normalize_number value, default_country_code: normalized_country_code if normalized_country_code
|
11
13
|
@record.errors.add(attribute, error_message) if not Phony.plausible?(value, cc: country_number)
|
12
14
|
end
|
13
15
|
|
@@ -37,6 +39,10 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
|
|
37
39
|
@record.country_code if @record.respond_to?(:country_code)
|
38
40
|
end
|
39
41
|
|
42
|
+
def normalized_country_code
|
43
|
+
options[:normalized_country_code]
|
44
|
+
end
|
45
|
+
|
40
46
|
end
|
41
47
|
|
42
48
|
module ActiveModel
|
@@ -31,6 +31,10 @@ ActiveRecord::Schema.define do
|
|
31
31
|
table.column :phone_number, :string
|
32
32
|
end
|
33
33
|
|
34
|
+
create_table :normalizable_helpful_homes do |table|
|
35
|
+
table.column :phone_number, :string
|
36
|
+
end
|
37
|
+
|
34
38
|
create_table :big_helpful_homes do |table|
|
35
39
|
table.column :phone_number, :string
|
36
40
|
end
|
@@ -80,6 +84,12 @@ class NotFormattedHelpfulHome < ActiveRecord::Base
|
|
80
84
|
validates_plausible_phone :phone_number, :without => /\A\+\d+/
|
81
85
|
end
|
82
86
|
|
87
|
+
#--------------------
|
88
|
+
class NormalizableHelpfulHome < ActiveRecord::Base
|
89
|
+
attr_accessor :phone_number
|
90
|
+
validates_plausible_phone :phone_number, :normalized_country_code => 'US'
|
91
|
+
end
|
92
|
+
|
83
93
|
#--------------------
|
84
94
|
class AustralianHelpfulHome < ActiveRecord::Base
|
85
95
|
attr_accessor :phone_number
|
@@ -104,6 +114,7 @@ end
|
|
104
114
|
|
105
115
|
I18n.locale = :en
|
106
116
|
VALID_NUMBER = '1 555 555 5555'
|
117
|
+
NORMALIZABLE_NUMBER = '555 555 5555'
|
107
118
|
AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '61390133997'
|
108
119
|
POLISH_NUMBER_WITH_COUNTRY_CODE = '48600600600'
|
109
120
|
FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '+61 390133997'
|
@@ -320,6 +331,35 @@ describe ActiveModel::Validations::HelperMethods do
|
|
320
331
|
|
321
332
|
end
|
322
333
|
|
334
|
+
#--------------------
|
335
|
+
context 'when a number must be validated after normalization' do
|
336
|
+
|
337
|
+
before(:each) do
|
338
|
+
@home = NormalizableHelpfulHome.new
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should validate an empty number" do
|
342
|
+
@home.should be_valid
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should validate a valid number" do
|
346
|
+
@home.phone_number = VALID_NUMBER
|
347
|
+
@home.should be_valid
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should validate a normalizable number" do
|
351
|
+
@home.phone_number = NORMALIZABLE_NUMBER
|
352
|
+
@home.should be_valid
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should invalidate an invalid number" do
|
356
|
+
@home.phone_number = INVALID_NUMBER
|
357
|
+
@home.should_not be_valid
|
358
|
+
@home.errors.messages.should include(:phone_number => ["is an invalid number"])
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
323
363
|
#--------------------
|
324
364
|
context 'when a number must include a specific country code' do
|
325
365
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phony_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-10-
|
12
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: phony
|