phony_rails 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Preempt the default loading so that we don't get an unqualified Country class imported.
4
4
  gem 'countries', :require => 'iso3166'
data/Gemfile.lock CHANGED
@@ -7,7 +7,7 @@ PATH
7
7
  phony (>= 1.7.7)
8
8
 
9
9
  GEM
10
- remote: http://rubygems.org/
10
+ remote: https://rubygems.org/
11
11
  specs:
12
12
  activemodel (3.2.13)
13
13
  activesupport (= 3.2.13)
data/README.md CHANGED
@@ -44,11 +44,14 @@ PhonyRails will also check your model for a country_code method to use when norm
44
44
  You can also do-it-yourself and call:
45
45
 
46
46
  # Options:
47
- # :country_code => The country code we should use.
47
+ # :country_code => The country code we should use (forced).
48
48
  # :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).
49
49
 
50
50
  PhonyRails.normalize_number('some number', :country_code => 'NL')
51
51
 
52
+ PhonyRails.normalize_number('+4790909090', :country_code => 'SE') # => '464790909090' (forced to +46)
53
+ PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE') # => '4790909090' (still +47 so not changed)
54
+
52
55
  ### Validation
53
56
 
54
57
  In your model use the Phony.plausible method to validate an attribute:
@@ -98,6 +101,10 @@ Say you want to find a record by a phone number. Best is to normalize user input
98
101
 
99
102
  ## Changelog
100
103
 
104
+ 0.3.0
105
+ * Now ability to force change a country_code.
106
+ See: https://github.com/joost/phony_rails/pull/23#issuecomment-17480463
107
+
101
108
  0.2.1
102
109
  * Better error handling by @k4nar
103
110
 
data/lib/phony_rails.rb CHANGED
@@ -21,9 +21,12 @@ module PhonyRails
21
21
  number = number.clone # Just to be sure, we don't want to change the original.
22
22
  number.gsub!(/[^\d\+]/, '') # Strips weird stuff from the number
23
23
  return if number.blank?
24
- if country_number = country_number_for(options[:country_code] || options[:default_country_code])
25
- # Add country_number if missing
24
+ if country_number = country_number_for(options[:country_code])
25
+ # (Force) add country_number if missing
26
26
  number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
27
+ elsif default_country_number = country_number_for(options[:default_country_code])
28
+ # Add default_country_number if missing
29
+ number = "#{default_country_number}#{number}" if not number =~ /^(00|\+)/
27
30
  end
28
31
  number = Phony.normalize(number) if Phony.plausible?(number)
29
32
  return number.to_s
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -68,19 +68,38 @@ describe PhonyRails do
68
68
  end
69
69
 
70
70
  describe 'PhonyRails#normalize_number' do
71
- it "should normalize a number with a default_country_code" do
72
- PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('31101234123')
73
- end
71
+ context 'number with a country code' do
72
+
73
+ it "should not add default_country_code" do
74
+ PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE').should eql('4790909090') # SE = +46
75
+ end
76
+
77
+ it "should force add country_code" do
78
+ PhonyRails.normalize_number('+4790909090', :country_code => 'SE').should eql('464790909090')
79
+ end
74
80
 
75
- it "should normalize a number with a country_code" do
76
- PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
77
- PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
78
81
  end
79
82
 
80
- it "should handle different countries" do
81
- PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
82
- PhonyRails.normalize_number('+43 664 3830412', :country_code => 'AT').should eql('436643830412')
83
- PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('442033308897')
83
+ context 'number without a country code' do
84
+
85
+ it "should normalize with a default_country_code" do
86
+ PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('31101234123')
87
+ end
88
+
89
+ it "should normalize with a country_code" do
90
+ PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
91
+ PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
92
+ end
93
+
94
+ it "should handle different countries" do
95
+ PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
96
+ PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('442033308897')
97
+ end
98
+
99
+ it "should prefer country_code over default_country_code" do
100
+ PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL').should eql('49308612906')
101
+ end
102
+
84
103
  end
85
104
 
86
105
  it "should handle some edge cases" 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.2.2
4
+ version: 0.3.0
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: 2013-04-26 00:00:00.000000000 Z
12
+ date: 2013-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: phony