Happy Phone Number provides easy methods to format phone numbers from your Rails models. Virtually any country specific format or convention could be supported, including national and international format. For the not (yet) supported countries, a general method using a simple mask is also provided.
- Phone number must be save with digits only. So, without spaces, hyphens, or dots. Just digits.
Let's say you have a
Contact model with a
To display the phone number you could do this:
<%= @contact.phone %> #=> "0123456789"
Using Happy Phone Number you could do that:
<%= @contact.happy_phone(:fr) %> #=> "01 23 45 67 89"
Or in uppercase if you prefer:
<%= @contact.happy_phone(:FR) %> #=> "01 23 45 67 89"
Or maybe with a string:
<%= @contact.happy_phone('fr') %> #=> "01 23 45 67 89"
If you want an international format:
<%= @contact.happy_inter_phone(:fr) %> #=> "+33 1 23 45 67 89"
What if you want dots as separator?
<%= @contact.happy_phone(:fr, '.') %> #=> "01.23.45.67.89"
What if your model have several phone attributes, like
mobile, or maybe
<%= @contact.happy_phone1(:fr) %> #=> "11 11 11 11 11" <%= @contact.happy_phone2(:fr) %> #=> "22 22 22 22 22" <%= @contact.happy_mobile(:fr) %> #=> "33 33 33 33 33" ...
Now, imagine you live in Belgium, where phone numbers could have 2 formats:
<%= @contact.happy_phone(:be) %> #=> "03 111 22 33" <%= @contact.happy_phone2(:be) %> #=> "063 11 22 33" <%= @contact.happy_inter_phone(:be) %> #=> "+32 3 111 22 33" <%= @contact.happy_inter_phone2(:be) %> #=> "+32 63 11 22 33"
And if Happy Phone Number don't know your country, you could use a mask formatting:
<%= @contact.happy_mask_phone("#### ###-###") %> #=> "0123 456-789"
Add this line in your Gemfile:
happy_phone_number method in your model. Example:
class Contact < ActiveRecord::Base attr_accessible :email, :name, :phone, :fax happy_phone_number end
Now, in your views, you can use:
happy_phone(:fr) happy_fax(:fr) ...
- ruby >= 1.9.2
- rails >= 3.2
I don't know much about format and convention for every possible country. Feel free to send me documentation for the phone number formatting and convention for your country. Or contribute to the code by adding any missing formatter.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request
How to add a formatter for your country
Name the file as follow:
where "aa" is the ISO code of the country. See [ISO country codes] 1.
Put this file in the lib/happy_phone_number/ folder. Then follow the structure of an existed formatter:
- fr_format.rb is a good starting point
- dk_format.rb is the simplest one
- be_format.rb is a little more complex
Feel free to ask for help if you have any troubles.
There is many other ways to contibute to FIXME
- Report any bug
- Give me ideas
- Correct my poor english syntax
- Blog about Happy Phone Number
- Tell Happy Phone Number to your friends and colleagues