phony_rails 0.9.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80dc843f05314cf9e2803669393f3b098a673c46
4
+ data.tar.gz: 2d0bf093da3ee71fb2067d87e61dd15fd706dbaa
5
+ SHA512:
6
+ metadata.gz: 05c20a589cf245c3ab11f971e8111a32734e362e67562ce0911b56779fda6f86a79a2c4598b049cc443457a3c96c4f3bd37c28ec39aac9184a49293739f4a1e4
7
+ data.tar.gz: dd3085cd688a02853f4fae937a45208cc2ba895b20be49bcef1b32fa20e9cc4ab67c8686a410f14c0a7284067b73c60b63bc8f7b7fb94addd88588c42da231a2
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  /.rvmrc
2
2
  /.idea/
3
+ .ruby-gemset
4
+ .ruby-version
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phony_rails (0.8.2)
4
+ phony_rails (0.10.1)
5
5
  activesupport (>= 3.0)
6
6
  countries (>= 0.8.2)
7
- phony (~> 2.10.1)
7
+ phony (~> 2.11.1)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -71,7 +71,7 @@ GEM
71
71
  nenv (0.1.1)
72
72
  optionable (0.2.0)
73
73
  origin (2.1.1)
74
- phony (2.10.1)
74
+ phony (2.11.1)
75
75
  pry (0.10.1)
76
76
  coderay (~> 1.1.0)
77
77
  method_source (~> 0.8.1)
data/README.md CHANGED
@@ -51,7 +51,7 @@ For **Mongoid**, in keeping with Mongoid plug-in conventions you must include th
51
51
 
52
52
  The `:default_country_code` options is used to specify a country_code when normalizing.
53
53
 
54
- PhonyRails will also check your model for a country_code method to use when normalizing the number. So `'070-12341234'` with `country_code` 'NL' will get normalized to `'317012341234'`.
54
+ PhonyRails will also check your model for a country_code method to use when normalizing the number. So `'070-12341234'` with `country_code` 'NL' will get normalized to `'+317012341234'`.
55
55
 
56
56
  You can also do-it-yourself and call:
57
57
 
@@ -61,8 +61,8 @@ You can also do-it-yourself and call:
61
61
 
62
62
  PhonyRails.normalize_number('some number', :country_code => 'NL')
63
63
 
64
- PhonyRails.normalize_number('+4790909090', :country_code => 'SE') # => '464790909090' (forced to +46)
65
- PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE') # => '4790909090' (still +47 so not changed)
64
+ PhonyRails.normalize_number('+4790909090', :country_code => 'SE') # => '+464790909090' (forced to +46)
65
+ PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE') # => '+4790909090' (still +47 so not changed)
66
66
 
67
67
  The country_code should always be a ISO 3166-1 alpha-2 (http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
68
68
 
@@ -102,6 +102,16 @@ You can validate against the normalized input as opposed to the raw input:
102
102
  phony_normalize :phone_number, as: :phone_number_normalized, :default_country_code => 'US'
103
103
  validates_plausible_phone :phone_number, :normalized_country_code => 'US'
104
104
 
105
+ #### Allowing records country codes to not match phone number country codes
106
+
107
+ You may have a record specifying one country (via a `country_code` attribute) but using a phone number from another country. For example, your record may be from Japan but have a phone number from the Philippines. By default, `phony_rails` will consider your record's `country_code` as part of the validation. If that country doesn't match the country code in the phone number, validation will fail.
108
+
109
+ If you want to allow records from one country to have phone numbers from a different one, there are a couple of options you can use: `ignore_record_country_number` and `ignore_record_country_code`. Use them like so:
110
+
111
+ validates :phone_number, :phony_plausible => { :ignore_record_country_code => true, :ignore_record_country_number => true }
112
+
113
+ Obviously, you don't have to use both, and you may not need or want to set either.
114
+
105
115
  ### Display / Views
106
116
 
107
117
  In your views use:
@@ -136,17 +146,6 @@ Say you want to find a record by a phone number. Best is to normalize user input
136
146
 
137
147
  Home.find_by_normalized_phone_number(PhonyRails.normalize_number(params[:phone_number]))
138
148
 
139
- ## Changelog
140
-
141
- 0.8.2
142
- * Added String#phony_normalized method
143
-
144
- ## TODO
145
-
146
- * Make this work: Home.find_by_normalized_phone_number(Home.normalize_number(params[:phone_number]))
147
- So we use Home.normalize_number instead of PhonyRails.normalize_number. This way we can use the same default_country_code.
148
- * Make country_code method configurable.
149
-
150
149
  ## Contributing
151
150
 
152
151
  1. Fork it
@@ -16,6 +16,7 @@ module PhonyRails
16
16
  # :default_country_number => Fallback country code.
17
17
  # :country_code => The country code we should use.
18
18
  # :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).
19
+ # :add_plus => Add a '+' in front so we know the country code is added. (default: true)
19
20
  # This idea came from:
20
21
  # http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
21
22
  def self.normalize_number(number, options = {})
@@ -23,14 +24,18 @@ module PhonyRails
23
24
  number = number.clone # Just to be sure, we don't want to change the original.
24
25
  number.gsub!(/[^\d\+]/, '') # Strips weird stuff from the number
25
26
  return if number.blank?
26
- if country_number = options[:country_number] || country_number_for(options[:country_code])
27
+ if _country_number = options[:country_number] || country_number_for(options[:country_code])
28
+ options[:add_plus] = true if options[:add_plus].nil?
27
29
  # (Force) add country_number if missing
28
- number = "#{country_number}#{number}" if not number =~ /\A(00|\+)?#{country_number}/
29
- elsif default_country_number = options[:default_country_number] || country_number_for(options[:default_country_code])
30
+ number = "#{_country_number}#{number}" if not number =~ /\A(00|\+)?#{_country_number}/
31
+ elsif _default_country_number = options[:default_country_number] || country_number_for(options[:default_country_code])
32
+ options[:add_plus] = true if options[:add_plus].nil?
30
33
  # Add default_country_number if missing
31
- number = "#{default_country_number}#{number}" if not number =~ /\A(00|\+|#{default_country_number})/
34
+ number = "#{_default_country_number}#{number}" if not number =~ /\A(00|\+|#{_default_country_number})/
32
35
  end
33
- Phony.normalize(number)
36
+ normalized_number = Phony.normalize(number)
37
+ options[:add_plus] = true if options[:add_plus].nil? && Phony.plausible?(normalized_number)
38
+ options[:add_plus] ? "+#{normalized_number}" : normalized_number
34
39
  rescue
35
40
  number # If all goes wrong .. we still return the original input.
36
41
  end
@@ -1,4 +1,4 @@
1
1
  ja:
2
2
  errors:
3
3
  messages:
4
- improbable_phone: "は正し電話番号ではありません"
4
+ improbable_phone: "は正しい電話番号ではありません"
@@ -1,7 +1,7 @@
1
1
  class String
2
2
 
3
3
  # Usage:
4
- # "+31 (0)30 1234 123".phony_normalized # => '31301234123'
4
+ # "+31 (0)30 1234 123".phony_normalized # => '+31301234123'
5
5
  # "(0)30 1234 123".phony_normalized # => '301234123'
6
6
  # "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
7
7
  def phony_normalized(options = {})
@@ -17,12 +17,21 @@
17
17
  # "010-12341234".phony_formatted(:normalize => :NL)
18
18
  # To return nil when a number is not correct (checked using Phony.plausible?) use
19
19
  # "010-12341234".phony_formatted(strict: true)
20
+ # When an error occurs during conversion it will return the original String.
21
+ # To raise an error use:
22
+ # "somestring".phone_formatted(raise: true)
20
23
  def phony_formatted(options = {})
21
24
  normalize_country_code = options.delete(:normalize)
22
- s = (normalize_country_code ? PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s) : self.gsub(/\D/, ''))
25
+ s = (normalize_country_code ? PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s, :add_plus => false) : self.gsub(/\D/, ''))
23
26
  return if s.blank?
24
27
  return if options[:strict] && !Phony.plausible?(s)
25
28
  Phony.format(s, options.reverse_merge(:format => :national))
29
+ rescue
30
+ if options[:raise]
31
+ raise
32
+ else
33
+ s
34
+ end
26
35
  end
27
36
 
28
37
  # The bang method
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.9.0"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -24,7 +24,7 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
24
24
  end
25
25
 
26
26
  def record_country_number
27
- @record.country_number if @record.respond_to?(:country_number)
27
+ @record.country_number if @record.respond_to?(:country_number) && !options[:ignore_record_country_number]
28
28
  end
29
29
 
30
30
  def country_number_from_country_code
@@ -36,7 +36,7 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
36
36
  end
37
37
 
38
38
  def record_country_code
39
- @record.country_code if @record.respond_to?(:country_code)
39
+ @record.country_code if @record.respond_to?(:country_code) && !options[:ignore_record_country_code]
40
40
  end
41
41
 
42
42
  def normalized_country_code
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.description = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
8
8
  gem.summary = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
9
9
  gem.homepage = "https://github.com/joost/phony_rails"
10
- gem.license = 'MIT'
10
+ gem.license = 'MIT'
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
13
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -16,8 +16,11 @@ Gem::Specification.new do |gem|
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = PhonyRails::VERSION
18
18
 
19
- gem.add_dependency "phony", "~> 2.10.1"
20
- gem.add_dependency "countries", ">= 0.8.2"
19
+ gem.post_install_message = "PhonyRails v0.10.0 changes the way numbers are stored!"
20
+ gem.post_install_message = "It now ads a '+' to the normalized number when it starts with a country number!"
21
+
22
+ gem.add_dependency "phony", '~> 2.11', '>= 2.11.1'
23
+ gem.add_dependency "countries", '~> 0.8', '>= 0.8.2'
21
24
  gem.add_dependency "activesupport", ">= 3.0"
22
25
  gem.add_development_dependency "activerecord", ">= 3.0"
23
26
  gem.add_development_dependency "mongoid", ">= 3.0"
@@ -66,19 +66,44 @@ describe PhonyRails do
66
66
 
67
67
  end
68
68
 
69
+ describe 'with raise option' do
70
+
71
+ # https://github.com/joost/phony_rails/issues/79
72
+
73
+ context 'when raise is true' do
74
+ it "should raise the error" do
75
+ lambda {
76
+ "8887716095".phony_formatted(format: :international, raise: true)
77
+ }.should raise_error(NoMethodError)
78
+ end
79
+ end
80
+
81
+ context 'when raise is false (default)' do
82
+ it "should return original String on exception" do
83
+ "8887716095".phony_formatted(format: :international).should eq('8887716095')
84
+ end
85
+ end
86
+
87
+ end
88
+
69
89
  describe "specific tests from issues" do
70
90
 
91
+ # https://github.com/joost/phony_rails/issues/79
92
+ it "should pass issue Github issue #42" do
93
+ "8887716095".phony_formatted(format: :international, normalize: 'US', raise: true).should eq('+1 888 771 6095')
94
+ end
95
+
71
96
  # https://github.com/joost/phony_rails/issues/42
72
97
  it "should pass issue Github issue #42" do
73
- PhonyRails.normalize_number("0606060606", default_country_code: 'FR').should eq('33606060606')
98
+ PhonyRails.normalize_number("0606060606", default_country_code: 'FR').should eq('+33606060606')
74
99
  end
75
100
 
76
101
  end
77
102
 
78
103
  it "should not change original String" do
79
- s = "0101234123"
104
+ s = '0101234123'
80
105
  s.phony_formatted(:normalize => :NL).should eql('010 123 4123')
81
- s.should eql("0101234123")
106
+ s.should eql('0101234123')
82
107
  end
83
108
 
84
109
  it "should phony_format a digits string with spaces String" do
@@ -107,7 +132,7 @@ describe PhonyRails do
107
132
 
108
133
  context "when String misses a country_code" do
109
134
  it "should normalize with :country_code option" do
110
- "010 1231234".phony_normalized(:country_code => :NL).should eql("31101231234")
135
+ "010 1231234".phony_normalized(:country_code => :NL).should eql("+31101231234")
111
136
  end
112
137
 
113
138
  it "should normalize without :country_code option" do
@@ -116,7 +141,7 @@ describe PhonyRails do
116
141
  end
117
142
 
118
143
  it "should normalize a String" do
119
- "+31 (0)10 1231234".phony_normalized.should eql("31101231234")
144
+ "+31 (0)10 1231234".phony_normalized.should eql("+31101231234")
120
145
  end
121
146
 
122
147
  end
@@ -127,19 +152,19 @@ describe PhonyRails do
127
152
  context 'number with a country code' do
128
153
 
129
154
  it "should not add default_country_code" do
130
- PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE').should eql('4790909090') # SE = +46
131
- PhonyRails.normalize_number('004790909090', :default_country_code => 'SE').should eql('4790909090')
132
- PhonyRails.normalize_number('4790909090', :default_country_code => 'NO').should eql('4790909090') # NO = +47
155
+ PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE').should eql('+4790909090') # SE = +46
156
+ PhonyRails.normalize_number('004790909090', :default_country_code => 'SE').should eql('+4790909090')
157
+ PhonyRails.normalize_number('4790909090', :default_country_code => 'NO').should eql('+4790909090') # NO = +47
133
158
  end
134
159
 
135
160
  it "should force add country_code" do
136
- PhonyRails.normalize_number('+4790909090', :country_code => 'SE').should eql('464790909090')
137
- PhonyRails.normalize_number('004790909090', :country_code => 'SE').should eql('4604790909090') # FIXME: differs due to Phony.normalize in v2.7.1?!
138
- PhonyRails.normalize_number('4790909090', :country_code => 'SE').should eql('464790909090')
161
+ PhonyRails.normalize_number('+4790909090', :country_code => 'SE').should eql('+464790909090')
162
+ PhonyRails.normalize_number('004790909090', :country_code => 'SE').should eql('+4604790909090') # FIXME: differs due to Phony.normalize in v2.7.1?!
163
+ PhonyRails.normalize_number('4790909090', :country_code => 'SE').should eql('+464790909090')
139
164
  end
140
165
 
141
166
  it "should recognize lowercase country codes" do
142
- PhonyRails.normalize_number('4790909090', :country_code => 'se').should eql('464790909090')
167
+ PhonyRails.normalize_number('4790909090', :country_code => 'se').should eql('+464790909090')
143
168
  end
144
169
 
145
170
  end
@@ -147,36 +172,47 @@ describe PhonyRails do
147
172
  context 'number without a country code' do
148
173
 
149
174
  it "should normalize with a default_country_code" do
150
- PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('31101234123')
175
+ PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('+31101234123')
151
176
  end
152
177
 
153
178
  it "should normalize with a country_code" do
154
- PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
155
- PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
179
+ PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('+31101234123')
180
+ PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('+31101234123')
156
181
  end
157
182
 
158
183
  it "should handle different countries" do
159
- PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
160
- PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('442033308897')
184
+ PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('+49308612906')
185
+ PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('+442033308897')
161
186
  end
162
187
 
163
188
  it "should prefer country_code over default_country_code" do
164
- PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL').should eql('49308612906')
189
+ PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL').should eql('+49308612906')
165
190
  end
166
191
 
167
192
  it "should recognize lowercase country codes" do
168
- PhonyRails.normalize_number('010-1234123', :country_code => 'nl').should eql('31101234123')
193
+ PhonyRails.normalize_number('010-1234123', :country_code => 'nl').should eql('+31101234123')
169
194
  end
170
195
 
171
196
  end
172
197
 
173
- it "should handle some edge cases" do
174
- PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL').should eql('31101234123')
175
- PhonyRails.normalize_number('070-4157134', :country_code => 'NL').should eql('31704157134')
176
- PhonyRails.normalize_number('0031-70-4157134', :country_code => 'NL').should eql('31704157134')
177
- PhonyRails.normalize_number('+31-70-4157134', :country_code => 'NL').should eql('31704157134')
178
- PhonyRails.normalize_number('0322-69497', :country_code => 'BE').should eql('3232269497')
179
- PhonyRails.normalize_number('+32 3 226 94 97', :country_code => 'BE').should eql('3232269497')
198
+ it "should handle some edge cases (with country_code)" do
199
+ PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL').should eql('+31101234123')
200
+ PhonyRails.normalize_number('070-4157134', :country_code => 'NL').should eql('+31704157134')
201
+ PhonyRails.normalize_number('0031-70-4157134', :country_code => 'NL').should eql('+31704157134')
202
+ PhonyRails.normalize_number('+31-70-4157134', :country_code => 'NL').should eql('+31704157134')
203
+ PhonyRails.normalize_number('0322-69497', :country_code => 'BE').should eql('+3232269497')
204
+ PhonyRails.normalize_number('+32 3 226 94 97', :country_code => 'BE').should eql('+3232269497')
205
+ PhonyRails.normalize_number('0450 764 000', :country_code => 'AU').should eql('+61450764000')
206
+ end
207
+
208
+ it "should handle some edge cases (with default_country_code)" do
209
+ PhonyRails.normalize_number('some nasty stuff in this +31 number 10-1234123 string', :country_code => 'NL').should eql('+31101234123')
210
+ PhonyRails.normalize_number('070-4157134', :default_country_code => 'NL').should eql('+31704157134')
211
+ PhonyRails.normalize_number('0031-70-4157134', :default_country_code => 'NL').should eql('+31704157134')
212
+ PhonyRails.normalize_number('+31-70-4157134', :default_country_code => 'NL').should eql('+31704157134')
213
+ PhonyRails.normalize_number('0322-69497', :default_country_code => 'BE').should eql('+3232269497')
214
+ PhonyRails.normalize_number('+32 3 226 94 97', :default_country_code => 'BE').should eql('+3232269497')
215
+ PhonyRails.normalize_number('0450 764 000', :default_country_code => 'AU').should eql('+61450764000')
180
216
  end
181
217
 
182
218
  it "should normalize even an implausible number" do
@@ -285,12 +321,12 @@ describe PhonyRails do
285
321
  # Following examples have complete number (with country code!)
286
322
  it "should return a normalized version of an attribute" do
287
323
  model = model_klass.new(:phone_attribute => "+31-(0)10-1234123")
288
- model.normalized_phone_attribute.should eql('31101234123')
324
+ model.normalized_phone_attribute.should eql('+31101234123')
289
325
  end
290
326
 
291
327
  it "should return a normalized version of a method" do
292
328
  model = model_klass.new(:phone_method => "+31-(0)10-1234123")
293
- model.normalized_phone_method.should eql('31101234123')
329
+ model.normalized_phone_method.should eql('+31101234123')
294
330
  end
295
331
 
296
332
  # Following examples have incomplete number
@@ -301,59 +337,60 @@ describe PhonyRails do
301
337
 
302
338
  it "should use country_code option" do
303
339
  model = model_klass.new(:phone_attribute => "(0)10-1234123")
304
- model.normalized_phone_attribute(:country_code => 'NL').should eql('31101234123')
340
+ model.normalized_phone_attribute(:country_code => 'NL').should eql('+31101234123')
305
341
  end
306
342
 
307
343
  it "should use country_code object method" do
308
344
  model = model_klass.new(:phone_attribute => "(0)10-1234123", :country_code => 'NL')
309
- model.normalized_phone_attribute.should eql('31101234123')
345
+ model.normalized_phone_attribute.should eql('+31101234123')
310
346
  end
311
347
 
312
348
  it "should fallback to default_country_code option" do
313
349
  model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
314
- model.normalized_phone1_method.should eql('49308612906')
350
+ model.normalized_phone1_method.should eql('+49308612906')
315
351
  end
316
352
 
317
353
  it "should overwrite default_country_code option with object method" do
318
354
  model = model_klass.new(:phone1_method => "(030) 8 61 29 06", :country_code => 'NL')
319
- model.normalized_phone1_method.should eql('31308612906')
355
+ model.normalized_phone1_method.should eql('+31308612906')
320
356
  end
321
357
 
322
358
  it "should overwrite default_country_code option with option" do
323
359
  model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
324
- model.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
360
+ model.normalized_phone1_method(:country_code => 'NL').should eql('+31308612906')
325
361
  end
326
362
 
327
363
  it "should use last passed options" do
328
364
  model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
329
- model.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
330
- model.normalized_phone1_method(:country_code => 'DE').should eql('49308612906')
331
- model.normalized_phone1_method(:country_code => nil).should eql('49308612906')
365
+ model.normalized_phone1_method(:country_code => 'NL').should eql('+31308612906')
366
+ model.normalized_phone1_method(:country_code => 'DE').should eql('+49308612906')
367
+ model.normalized_phone1_method(:country_code => nil).should eql('+49308612906')
332
368
  end
333
369
 
334
370
  it "should use last object method" do
335
371
  model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
336
372
  model.country_code = 'NL'
337
- model.normalized_phone1_method.should eql('31308612906')
373
+ model.normalized_phone1_method.should eql('+31308612906')
338
374
  model.country_code = 'DE'
339
- model.normalized_phone1_method.should eql('49308612906')
375
+ model.normalized_phone1_method.should eql('+49308612906')
340
376
  model.country_code = nil
341
- model.normalized_phone1_method(:country_code => nil).should eql('49308612906')
377
+ model.normalized_phone1_method(:country_code => nil).should eql('+49308612906')
342
378
  end
343
379
  end
344
380
 
345
381
  describe 'using model#phony_normalize' do
346
- it "should set a normalized version of an attribute" do
382
+ it "should not change normalized numbers (see #76)" do
347
383
  model = model_klass.new(:phone_number => "+31-(0)10-1234123")
348
384
  model.valid?.should be_true
349
- model.phone_number.should eql('31101234123')
385
+ model.phone_number.should eql('+31101234123')
386
+
350
387
  end
351
388
 
352
389
  it "should set a normalized version of an attribute using :as option" do
353
390
  model_klass.phony_normalize :phone_number, :as => :phone_number_as_normalized
354
391
  model = model_klass.new(:phone_number => "+31-(0)10-1234123")
355
392
  model.valid?.should be_true
356
- model.phone_number_as_normalized.should eql('31101234123')
393
+ model.phone_number_as_normalized.should eql('+31101234123')
357
394
  end
358
395
 
359
396
  it "should raise a RuntimeError at validation if the attribute doesn't exist" do
@@ -378,6 +415,20 @@ describe PhonyRails do
378
415
  let(:model_klass){ ActiveRecordModel }
379
416
  let(:dummy_klass){ ActiveRecordDummy }
380
417
  it_behaves_like 'model with PhonyRails'
418
+
419
+ it "should correctly keep a hard set country_code" do
420
+ model = model_klass.new(:fax_number => '+1 978 555 0000')
421
+ model.valid?.should be_true
422
+ model.fax_number.should eql('+19785550000')
423
+ model.save.should be_true
424
+ model.save.should be_true # revalidate
425
+ model.reload
426
+ model.fax_number.should eql('+19785550000')
427
+ model.fax_number = '(030) 8 61 29 06'
428
+ model.save.should be_true # revalidate
429
+ model.reload
430
+ model.fax_number.should eql('+61308612906')
431
+ end
381
432
  end
382
433
 
383
434
  describe 'Mongoid' do
@@ -46,6 +46,10 @@ ActiveRecord::Schema.define do
46
46
  create_table :polish_helpful_homes do |table|
47
47
  table.column :phone_number, :string
48
48
  end
49
+
50
+ create_table :mismatched_helpful_homes do |table|
51
+ table.column :phone_number, :string
52
+ end
49
53
  end
50
54
 
51
55
  #--------------------
@@ -108,6 +112,11 @@ class BigHelpfulHome < ActiveRecord::Base
108
112
  validates_plausible_phone :phone_number, :presence => true, :with => /\A\+\d+/, :country_number => "33"
109
113
  end
110
114
 
115
+ #--------------------
116
+ class MismatchedHelpfulHome < ActiveRecord::Base
117
+ attr_accessor :phone_number, :country_code
118
+ validates :phone_number, :phony_plausible => {:ignore_record_country_code => true}
119
+ end
111
120
  #-----------------------------------------------------------------------------------------------------------------------
112
121
  # Tests
113
122
  #-----------------------------------------------------------------------------------------------------------------------
@@ -121,6 +130,7 @@ FORMATTED_AUSTRALIAN_NUMBER_WITH_COUNTRY_CODE = '+61 390133997'
121
130
  FRENCH_NUMBER_WITH_COUNTRY_CODE = '33627899541'
122
131
  FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE = '+33 627899541'
123
132
  INVALID_NUMBER = '123456789 123456789 123456789 123456789'
133
+ JAPAN_COUNTRY = 'jp'
124
134
 
125
135
  #-----------------------------------------------------------------------------------------------------------------------
126
136
  describe PhonyPlausibleValidator do
@@ -167,7 +177,7 @@ describe PhonyPlausibleValidator do
167
177
  I18n.with_locale(:ja) do
168
178
  @home.phone_number = INVALID_NUMBER
169
179
  @home.valid?
170
- @home.errors.messages.should include(:phone_number => ["は正し電話番号ではありません"])
180
+ @home.errors.messages.should include(:phone_number => ["は正しい電話番号ではありません"])
171
181
  end
172
182
  end
173
183
 
@@ -433,6 +443,19 @@ describe ActiveModel::Validations::HelperMethods do
433
443
 
434
444
  end
435
445
 
446
+ #--------------------
447
+ context 'when a phone number does not match the records country' do
448
+ before(:each) do
449
+ @home = MismatchedHelpfulHome.new
450
+ @home.country_code = JAPAN_COUNTRY
451
+ @home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
452
+ end
453
+
454
+ it "should allow this number" do
455
+ @home.should be_valid
456
+ end
457
+ end
458
+
436
459
  end
437
460
 
438
461
  end
@@ -15,6 +15,7 @@ ActiveRecord::Schema.define do
15
15
  table.column :phone_attribute, :string
16
16
  table.column :phone_number, :string
17
17
  table.column :phone_number_as_normalized, :string
18
+ table.column :fax_number, :string
18
19
  end
19
20
  end
20
21
 
@@ -26,6 +27,7 @@ module SharedModelMethods
26
27
  phony_normalized_method :phone_method # adds normalized_phone_method method
27
28
  phony_normalized_method :phone1_method, :default_country_code => 'DE' # adds normalized_phone_method method
28
29
  phony_normalize :phone_number # normalized on validation
30
+ phony_normalize :fax_number, :default_country_code => 'AU'
29
31
  end
30
32
  end
31
33
 
@@ -42,6 +44,7 @@ class MongoidModel
42
44
  field :phone_attribute, :type => String
43
45
  field :phone_number, :type => String
44
46
  field :phone_number_as_normalized, :type => String
47
+ field :fax_number
45
48
  include SharedModelMethods
46
49
  end
47
50
 
metadata CHANGED
@@ -1,94 +1,95 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
5
- prerelease:
4
+ version: 0.11.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Joost Hietbrink
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-01-13 00:00:00.000000000 Z
11
+ date: 2015-03-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: phony
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 2.10.1
19
+ version: '2.11'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.11.1
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.11'
30
+ - - ">="
28
31
  - !ruby/object:Gem::Version
29
- version: 2.10.1
32
+ version: 2.11.1
30
33
  - !ruby/object:Gem::Dependency
31
34
  name: countries
32
35
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
36
  requirements:
35
- - - ! '>='
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ - - ">="
36
41
  - !ruby/object:Gem::Version
37
42
  version: 0.8.2
38
43
  type: :runtime
39
44
  prerelease: false
40
45
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
46
  requirements:
43
- - - ! '>='
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.8'
50
+ - - ">="
44
51
  - !ruby/object:Gem::Version
45
52
  version: 0.8.2
46
53
  - !ruby/object:Gem::Dependency
47
54
  name: activesupport
48
55
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
56
  requirements:
51
- - - ! '>='
57
+ - - ">="
52
58
  - !ruby/object:Gem::Version
53
59
  version: '3.0'
54
60
  type: :runtime
55
61
  prerelease: false
56
62
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
63
  requirements:
59
- - - ! '>='
64
+ - - ">="
60
65
  - !ruby/object:Gem::Version
61
66
  version: '3.0'
62
67
  - !ruby/object:Gem::Dependency
63
68
  name: activerecord
64
69
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
70
  requirements:
67
- - - ! '>='
71
+ - - ">="
68
72
  - !ruby/object:Gem::Version
69
73
  version: '3.0'
70
74
  type: :development
71
75
  prerelease: false
72
76
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
77
  requirements:
75
- - - ! '>='
78
+ - - ">="
76
79
  - !ruby/object:Gem::Version
77
80
  version: '3.0'
78
81
  - !ruby/object:Gem::Dependency
79
82
  name: mongoid
80
83
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
84
  requirements:
83
- - - ! '>='
85
+ - - ">="
84
86
  - !ruby/object:Gem::Version
85
87
  version: '3.0'
86
88
  type: :development
87
89
  prerelease: false
88
90
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
91
  requirements:
91
- - - ! '>='
92
+ - - ">="
92
93
  - !ruby/object:Gem::Version
93
94
  version: '3.0'
94
95
  description: This Gem adds useful methods to your Rails app to validate, display and
@@ -99,8 +100,8 @@ executables: []
99
100
  extensions: []
100
101
  extra_rdoc_files: []
101
102
  files:
102
- - .gitignore
103
- - .rspec
103
+ - ".gitignore"
104
+ - ".rspec"
104
105
  - Gemfile
105
106
  - Gemfile.lock
106
107
  - Guardfile
@@ -125,27 +126,27 @@ files:
125
126
  homepage: https://github.com/joost/phony_rails
126
127
  licenses:
127
128
  - MIT
128
- post_install_message:
129
+ metadata: {}
130
+ post_install_message: It now ads a '+' to the normalized number when it starts with
131
+ a country number!
129
132
  rdoc_options: []
130
133
  require_paths:
131
134
  - lib
132
135
  required_ruby_version: !ruby/object:Gem::Requirement
133
- none: false
134
136
  requirements:
135
- - - ! '>='
137
+ - - ">="
136
138
  - !ruby/object:Gem::Version
137
139
  version: '0'
138
140
  required_rubygems_version: !ruby/object:Gem::Requirement
139
- none: false
140
141
  requirements:
141
- - - ! '>='
142
+ - - ">="
142
143
  - !ruby/object:Gem::Version
143
144
  version: '0'
144
145
  requirements: []
145
146
  rubyforge_project:
146
- rubygems_version: 1.8.23
147
+ rubygems_version: 2.2.2
147
148
  signing_key:
148
- specification_version: 3
149
+ specification_version: 4
149
150
  summary: This Gem adds useful methods to your Rails app to validate, display and save
150
151
  phone numbers.
151
152
  test_files: