From 519d85dc528d75463f12fcbff642d23836475645 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Tue, 21 Oct 2008 22:03:06 -0500 Subject: [PATCH] Add 'groupings' option to number_to_phone helper. --- .../lib/action_view/helpers/number_helper.rb | 14 +++++++++++--- actionpack/test/template/number_helper_test.rb | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 77f19b3..ba1cdff 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -4,7 +4,7 @@ module ActionView # Methods are provided for phone numbers, currency, percentage, # precision, positional notation, and file size. module NumberHelper - # Formats a +number+ into a US phone number (e.g., (555) 123-9876). You can customize the format + # Formats a +number+ into a phone number (e.g., (555) 123-9876). You can customize the format # in the +options+ hash. # # ==== Options @@ -13,6 +13,7 @@ module ActionView # * :extension - Specifies an extension to add to the end of the # generated number. # * :country_code - Sets the country code for the phone number. + # * :groupings - Specifies alternate groupings (must specify 3-element array; defaults to [3, 3, 4]) # # ==== Examples # number_to_phone(1235551234) # => 123-555-1234 @@ -20,6 +21,10 @@ module ActionView # number_to_phone(1235551234, :delimiter => " ") # => 123 555 1234 # number_to_phone(1235551234, :area_code => true, :extension => 555) # => (123) 555-1234 x 555 # number_to_phone(1235551234, :country_code => 1) # => +1-123-555-1234 + # number_to_phone('0212345678', :groupings => [2, 4, 4], + # :area_code => true, :delimiter => " ") # => (02) 1234 5678 + # number_to_phone('0412345678', :groupings => [4, 3, 3], + # :delimiter => " ") # => 0412 345 678 # # number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".") # => +1.123.555.1234 x 1343 @@ -30,14 +35,17 @@ module ActionView delimiter = options[:delimiter] || "-" extension = options[:extension].to_s.strip || nil country_code = options[:country_code] || nil + g1, g2, g3 = options[:groupings] || [3, 3, 4] + + re = Regexp.new("([0-9]{1,#{g1}})([0-9]{#{g2}})([0-9]{#{g3}})$") begin str = "" str << "+#{country_code}#{delimiter}" unless country_code.blank? str << if area_code - number.gsub!(/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/,"(\\1) \\2#{delimiter}\\3") + number.gsub!(re, "(\\1) \\2#{delimiter}\\3") else - number.gsub!(/([0-9]{1,3})([0-9]{3})([0-9]{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3") + number.gsub!(re, "\\1#{delimiter}\\2#{delimiter}\\3") end str << " x #{extension}" unless extension.blank? str diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 9c9f549..05e1b56 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -14,6 +14,8 @@ class NumberHelperTest < ActionView::TestCase assert_equal("+18005551212", number_to_phone(8005551212, :country_code => 1, :delimiter => '')) assert_equal("22-555-1212", number_to_phone(225551212)) assert_equal("+45-22-555-1212", number_to_phone(225551212, :country_code => 45)) + assert_equal("(02) 1234 5678", number_to_phone('0212345678', :area_code => true, :delimiter => ' ', :groupings => [2, 4, 4])) + assert_equal("0412 345 678", number_to_phone('0412345678', :delimiter => ' ', :groupings => [4, 3, 3])) assert_equal("x", number_to_phone("x")) assert_nil number_to_phone(nil) end -- 1.5.3.7