phony_rails 0.12.7 → 0.12.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1bf0320fb96ad7df05e5ff32c9f8c8ebc45d0ea
4
- data.tar.gz: e7229d45cd8439a917d44bedede1ebd790f3a7c7
3
+ metadata.gz: 5f955ac8d52e7faa2965313a1d8643dce32d780d
4
+ data.tar.gz: 50cf60a64472724f47bfa733458a8182aaafe651
5
5
  SHA512:
6
- metadata.gz: c692be57b1024ee4169a81e2eea41667869f1ed61988138cc3429aeac75a60d27f8f2654b92f0f380aaba2a13c6980391737c82dda56220af3bb088fd6198f83
7
- data.tar.gz: 9feb8d8b598c9593036995f935a70e8735ea2a2e1254ea46f77357f354a1d21ded8e0ea9341280a6823028712ff3a65fddd87f0e16045b8484dd74de7a1033e2
6
+ metadata.gz: ad42f17903eff99998805e0ae79609a7dd86e13fce1a27003448ac356fb65f01efdf8ad3449de11369c825bf9c25bdd7408c08c613074953663293642a9db756
7
+ data.tar.gz: 24e081e4b42f81dcd035c2b3afadb6c83e11c523bffe9e18fec3ea6a84f5dafc216d7904942d733816528b62476da00dcedec9d1b9e86f51bccc0de4e3e0acfc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # Change Log
2
2
 
3
+ ## [v0.12.8](https://github.com/joost/phony_rails/tree/v0.12.8) (2015-06-22)
4
+
5
+ [Full Changelog](https://github.com/joost/phony_rails/compare/v0.12.7...v0.12.8)
6
+
7
+ **Closed issues:**
8
+
9
+ - activerecord dependency [\#99](https://github.com/joost/phony_rails/issues/99)
10
+
11
+ - Using a number different from the country [\#97](https://github.com/joost/phony_rails/issues/97)
12
+
13
+ - UK 0203 numbers not handled correctly [\#95](https://github.com/joost/phony_rails/issues/95)
14
+
15
+ - Consider keeping a Changelog for changes in each version. [\#91](https://github.com/joost/phony_rails/issues/91)
16
+
17
+ - Phone numbers with extensions [\#78](https://github.com/joost/phony_rails/issues/78)
18
+
19
+ **Merged pull requests:**
20
+
21
+ - remove active\_record dependency [\#100](https://github.com/joost/phony_rails/pull/100) ([sbounmy](https://github.com/sbounmy))
22
+
23
+ - Add enforce\_record\_country option to phony\_normalize method [\#98](https://github.com/joost/phony_rails/pull/98) ([phillipp](https://github.com/phillipp))
24
+
3
25
  ## [v0.12.7](https://github.com/joost/phony_rails/tree/v0.12.7) (2015-06-18)
4
26
 
5
27
  [Full Changelog](https://github.com/joost/phony_rails/compare/v0.12.6...v0.12.7)
data/README.md CHANGED
@@ -106,7 +106,9 @@ You can validate against the normalized input as opposed to the raw input:
106
106
 
107
107
  #### Allowing records country codes to not match phone number country codes
108
108
 
109
- 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.
109
+ 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.
110
+
111
+ Additionally, `phony_normalize` will always add the records country code as the country number (eg. the user enters '+81xxx' for Japan and the records `country_code` is 'DE' then `phony_normalize` will change the number to '+4981'). You can turn this off by adding `:enforce_record_country => false` to the validation options. The country_code will then only be added if no country code is specified.
110
112
 
111
113
  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:
112
114
 
data/lib/phony_rails.rb CHANGED
@@ -76,7 +76,10 @@ module PhonyRails
76
76
  # It also adds the country_code (number), eg. 31 for NL numbers.
77
77
  def set_phony_normalized_numbers(attributes, options = {})
78
78
  options = options.clone
79
- options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
79
+ if self.respond_to?(:country_code)
80
+ set_country_as = options[:enforce_record_country] ? :country_code : :default_country_code
81
+ options[set_country_as] ||= self.country_code
82
+ end
80
83
  attributes.each do |attribute|
81
84
  attribute_name = options[:as] || attribute
82
85
  raise RuntimeError, "No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)" if not self.class.attribute_method?(attribute_name)
@@ -94,11 +97,13 @@ module PhonyRails
94
97
  # you've geocoded before calling this method!
95
98
  def phony_normalize(*attributes)
96
99
  options = attributes.last.is_a?(Hash) ? attributes.pop : {}
97
- options.assert_valid_keys :country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as
100
+ options.assert_valid_keys :country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country
98
101
  if options[:as].present?
99
102
  raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1
100
103
  end
101
104
 
105
+ options[:enforce_record_country] = true if options[:enforce_record_country].nil?
106
+
102
107
  # Add before validation that saves a normalized version of the phone number
103
108
  self.before_validation do
104
109
  set_phony_normalized_numbers(attributes, options)
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.12.7"
2
+ VERSION = "0.12.8"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  # Uses the Phony.plausible method to validate an attribute.
2
2
  # Usage:
3
3
  # validate :phone_number, :phony_plausible => true
4
- require 'active_record'
4
+ require 'active_model'
5
5
  class PhonyPlausibleValidator < ActiveModel::EachValidator
6
6
 
7
7
  # Validates a String using Phony.plausible? method.
@@ -360,7 +360,7 @@ describe PhonyRails do
360
360
  end
361
361
 
362
362
  it "should accept supported options" do
363
- options = [:country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as]
363
+ options = [:country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country]
364
364
  options.each do |option_sym|
365
365
  expect(lambda {
366
366
  dummy_klass.phony_normalize(:phone_number, option_sym => false)
@@ -493,6 +493,55 @@ describe PhonyRails do
493
493
  model.reload
494
494
  expect(model.fax_number).to eql('+61308612906')
495
495
  end
496
+
497
+ context 'when enforce_record_country is turned off' do
498
+ let(:model_klass) { RelaxedActiveRecordModel }
499
+ let(:record) { model_klass.new }
500
+
501
+ before {
502
+ record.phone_number = phone_number
503
+ record.country_code = 'DE'
504
+ record.valid? # run the empty validation chain to execute the before hook (normalized the number)
505
+ }
506
+
507
+ context 'when the country_code attribute does not match the country number' do
508
+ context 'when the number is prefixed with a country number and a plus' do
509
+ let(:phone_number) { '+436601234567' }
510
+
511
+ it 'should not add the records country number' do
512
+ expect(record.phone_number).to eql('+436601234567')
513
+ end
514
+ end
515
+
516
+ # In this case it's not clear if there is a country number, so it should be added
517
+ context 'when the number is prefixed with a country number' do
518
+ let(:phone_number) { '436601234567' }
519
+
520
+ it 'should add the records country number' do
521
+ expect(record.phone_number).to eql('+49436601234567')
522
+ end
523
+ end
524
+ end
525
+
526
+ # This should be the case anyways
527
+ context 'when the country_code attribute matches the country number' do
528
+ context 'when the number includes a country number and a plus' do
529
+ let(:phone_number) { '+491721234567' }
530
+
531
+ it 'should not add the records country number' do
532
+ expect(record.phone_number).to eql('+491721234567')
533
+ end
534
+ end
535
+
536
+ context 'when the number has neither country number nor plus' do
537
+ let(:phone_number) { '01721234567' }
538
+
539
+ it 'should not add the records country number' do
540
+ expect(record.phone_number).to eql('+491721234567')
541
+ end
542
+ end
543
+ end
544
+ end
496
545
  end
497
546
 
498
547
  describe 'Mongoid' do
data/spec/spec_helper.rb CHANGED
@@ -35,6 +35,13 @@ class ActiveRecordModel < ActiveRecord::Base
35
35
  include SharedModelMethods
36
36
  end
37
37
 
38
+ class RelaxedActiveRecordModel < ActiveRecord::Base
39
+ self.table_name = 'active_record_models'
40
+ attr_accessor :phone_number, :country_code
41
+
42
+ phony_normalize :phone_number, :enforce_record_country => false
43
+ end
44
+
38
45
  class ActiveRecordDummy < ActiveRecordModel
39
46
  end
40
47
 
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.7
4
+ version: 0.12.8
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-06-18 00:00:00.000000000 Z
11
+ date: 2015-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phony