From 0f9b75b137290f144a70a72c788df380c4f1700c Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Sun, 19 Oct 2008 17:38:03 -0500 Subject: [PATCH] Add 'grouping' option to number_to_phone to facilitate other country grouping patterns. --- .../lib/action_view/helpers/number_helper.rb | 26 +++++++++++++------- actionpack/test/template/number_helper_test.rb | 2 + 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 77f19b3..39be824 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,24 +21,31 @@ 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 def number_to_phone(number, options = {}) number = number.to_s.strip unless number.nil? - options = options.symbolize_keys - area_code = options[:area_code] || nil - delimiter = options[:delimiter] || "-" - extension = options[:extension].to_s.strip || nil - country_code = options[:country_code] || nil + options = options.stringify_keys + area_code = options["area_code"] || nil + 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 @@ -45,7 +53,7 @@ module ActionView number end end - + # Formats a +number+ into a currency string (e.g., $13.65). You can customize the format # in the +options+ hash. # 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