Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Already normalized numbers have default country code prepended #76

Closed
mroach opened this issue Jan 19, 2015 · 0 comments
Closed

Already normalized numbers have default country code prepended #76

mroach opened this issue Jan 19, 2015 · 0 comments

Comments

@mroach
Copy link

mroach commented Jan 19, 2015

Scenario: A model has 3 phone number fields all with a default_country_code: 'AU'. Setting one of them to explicitly be a foreign number using a + in the number correctly normalises the value as expected. However, after modifying the model and saving it again, the number stored in the database will have the default country code prepended to the phone number that was already perfectly valid. Here's code to illustrate the problem:

class Person < ActiveRecord::Base
  phony_normalize :phone_home,   default_country_code: 'AU'
  phony_normalize :phone_mobile, default_country_code: 'AU'
  phony_normalize :phone_work,   default_country_code: 'AU'
end

Now, I'll create a person with an Australian mobile number and American home number.

When the model is saved, the numbers are correctly normalised. The Australian number is prefixed with 61 and the American number is prefixed with 1. Everything is perfect.

Then, I set the work phone number to another perfectly valid Australian number. When saving, the number is again correctly normalised with a leading 61. However, you'll notice that the home number, the American one, got 61 tacked onto the front of it! This is the problem.

If the assumption is that the numbers stored in the database are already normalised and start with the country code, why is the country code being added again?

irb(main):001:0> p = Person.new(phone_mobile: '0450 764 000', phone_home: '+1 978 555 0000')
=> #<Person id: nil, phone_home: "+1 978 555 0000", phone_mobile: "0450 764 000", phone_work: nil>
irb(main):002:0> p.save!
   (0.2ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "people" ("phone_mobile", "phone_home") VALUES (?, ?)  [["phone_mobile", "61450764000"], ["phone_home", "19785550000"]]
   (2.0ms)  commit transaction
=> true
irb(main):003:0> p.phone_work = '0200 777 000'
=> "0200 777 000"
irb(main):004:0> p
=> #<Person id: 1, phone_home: "19785550000", phone_mobile: "61450764000", phone_work: "0200 777 000">
irb(main):005:0> p.save
   (0.1ms)  begin transaction
  SQL (0.5ms)  UPDATE "people" SET "phone_work" = ?, "phone_home" = ? WHERE "people"."id" = ?  [["phone_work", "61200777000"], ["phone_home", "6119785550000"], ["id", 1]]
   (1.1ms)  commit transaction
=> true
@joost joost closed this as completed in 1ad0ed5 Jan 21, 2015
ignisf added a commit to initLab/fauna that referenced this issue Mar 9, 2015
Due to a breaking change introduced by the fix of
joost/phony_rails#76 (more specifically
joost/phony_rails@1ad0ed5)
all normalized phone numbers must now have a plus sign before the
country code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant