phony_rails 0.6.0 → 0.6.1
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.
- data/Gemfile.lock +1 -1
- data/README.md +8 -2
- data/lib/phony_rails/version.rb +1 -1
- data/lib/validators/phony_validator.rb +11 -3
- data/spec/lib/validators/phony_validator_spec.rb +44 -3
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -83,9 +83,13 @@ so we can use:
|
|
83
83
|
|
84
84
|
the i18n key is `:improbable_phone`
|
85
85
|
|
86
|
-
You can also validate if a number has the correct country
|
86
|
+
You can also validate if a number has the correct country number:
|
87
87
|
|
88
|
-
validates_plausible_phone :phone_number, :
|
88
|
+
validates_plausible_phone :phone_number, :country_number => '61'
|
89
|
+
|
90
|
+
or correct country code:
|
91
|
+
|
92
|
+
validates_plausible_phone :phone_number, :country_code => 'AU'
|
89
93
|
|
90
94
|
### Display / Views
|
91
95
|
|
@@ -193,3 +197,5 @@ Say you want to find a record by a phone number. Best is to normalize user input
|
|
193
197
|
5. Create new Pull Request
|
194
198
|
|
195
199
|
Don't forget to add tests and run rspec before creating a pull request :)
|
200
|
+
|
201
|
+
See all contributors on https://github.com/joost/phony_rails/graphs/contributors.
|
data/lib/phony_rails/version.rb
CHANGED
@@ -8,7 +8,7 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
|
|
8
8
|
return if value.blank?
|
9
9
|
|
10
10
|
@record = record
|
11
|
-
@record.errors.add(attribute, error_message) if not Phony.plausible?(value, cc:
|
11
|
+
@record.errors.add(attribute, error_message) if not Phony.plausible?(value, cc: country_number)
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
@@ -17,14 +17,22 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
|
|
17
17
|
options[:message] || :improbable_phone
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
options[:
|
20
|
+
def country_number
|
21
|
+
options[:country_number] || record_country_number || country_number_from_country_code
|
22
22
|
end
|
23
23
|
|
24
24
|
def record_country_number
|
25
25
|
@record.country_number if @record.respond_to?(:country_number)
|
26
26
|
end
|
27
27
|
|
28
|
+
def country_number_from_country_code
|
29
|
+
PhonyRails.country_number_for(country_code)
|
30
|
+
end
|
31
|
+
|
32
|
+
def country_code
|
33
|
+
options[:country_code] || record_country_code
|
34
|
+
end
|
35
|
+
|
28
36
|
def record_country_code
|
29
37
|
@record.country_code if @record.respond_to?(:country_code)
|
30
38
|
end
|
@@ -38,6 +38,10 @@ ActiveRecord::Schema.define do
|
|
38
38
|
create_table :australian_helpful_homes do |table|
|
39
39
|
table.column :phone_number, :string
|
40
40
|
end
|
41
|
+
|
42
|
+
create_table :polish_helpful_homes do |table|
|
43
|
+
table.column :phone_number, :string
|
44
|
+
end
|
41
45
|
end
|
42
46
|
|
43
47
|
#--------------------
|
@@ -79,13 +83,19 @@ end
|
|
79
83
|
#--------------------
|
80
84
|
class AustralianHelpfulHome < ActiveRecord::Base
|
81
85
|
attr_accessor :phone_number
|
82
|
-
validates_plausible_phone :phone_number, :
|
86
|
+
validates_plausible_phone :phone_number, :country_number => "61"
|
87
|
+
end
|
88
|
+
|
89
|
+
#--------------------
|
90
|
+
class PolishHelpfulHome < ActiveRecord::Base
|
91
|
+
attr_accessor :phone_number
|
92
|
+
validates_plausible_phone :phone_number, :country_code => "PL"
|
83
93
|
end
|
84
94
|
|
85
95
|
#--------------------
|
86
96
|
class BigHelpfulHome < ActiveRecord::Base
|
87
97
|
attr_accessor :phone_number
|
88
|
-
validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/, :
|
98
|
+
validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/, :country_number => "33"
|
89
99
|
end
|
90
100
|
|
91
101
|
#-----------------------------------------------------------------------------------------------------------------------
|
@@ -95,6 +105,7 @@ end
|
|
95
105
|
I18n.locale = :en
|
96
106
|
VALID_NUMBER = '1 555 555 5555'
|
97
107
|
AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '61390133997'
|
108
|
+
POLISH_NUMBER_WITH_COUNTRY_CODE = '48600600600'
|
98
109
|
FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '+61 390133997'
|
99
110
|
FRENCH_NUMBER_WITH_COUNTRY_CODE = '33627899541'
|
100
111
|
FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE = '+33 627899541'
|
@@ -280,7 +291,7 @@ describe ActiveModel::Validations::HelperMethods do
|
|
280
291
|
end
|
281
292
|
|
282
293
|
#--------------------
|
283
|
-
context 'when a number must include a specific country
|
294
|
+
context 'when a number must include a specific country number' do
|
284
295
|
|
285
296
|
before(:each) do
|
286
297
|
@home = AustralianHelpfulHome.new
|
@@ -309,6 +320,36 @@ describe ActiveModel::Validations::HelperMethods do
|
|
309
320
|
|
310
321
|
end
|
311
322
|
|
323
|
+
#--------------------
|
324
|
+
context 'when a number must include a specific country code' do
|
325
|
+
|
326
|
+
before(:each) do
|
327
|
+
@home = PolishHelpfulHome.new
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should validate an empty number" do
|
331
|
+
@home.should be_valid
|
332
|
+
end
|
333
|
+
|
334
|
+
it "should validate a valid number with the right country code" do
|
335
|
+
@home.phone_number = POLISH_NUMBER_WITH_COUNTRY_CODE
|
336
|
+
@home.should be_valid
|
337
|
+
end
|
338
|
+
|
339
|
+
it "should invalidate a valid number with the wrong country code" do
|
340
|
+
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
|
341
|
+
@home.should_not be_valid
|
342
|
+
@home.errors.messages.should include(:phone_number => ["is an invalid number"])
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should invalidate a valid number without a country code" do
|
346
|
+
@home.phone_number = VALID_NUMBER
|
347
|
+
@home.should_not be_valid
|
348
|
+
@home.errors.messages.should include(:phone_number => ["is an invalid number"])
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
352
|
+
|
312
353
|
context 'when lots of things are being validated simultaneously' do
|
313
354
|
|
314
355
|
before(:each) do
|
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.6.
|
4
|
+
version: 0.6.1
|
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-
|
12
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: phony
|