aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-06-16 06:33:10 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-06-16 06:33:10 +0000
commit665ab93761f3a42ccce12b10c1e9a958ae53e691 (patch)
treeca243d3688a9a2176b44acb73d680ba0b582ff55 /actionpack/lib/action_view/helpers
parent43c470fae468ef63e0d5c3dc1e202925685fd47b (diff)
downloadrails-665ab93761f3a42ccce12b10c1e9a958ae53e691.tar.gz
rails-665ab93761f3a42ccce12b10c1e9a958ae53e691.tar.bz2
rails-665ab93761f3a42ccce12b10c1e9a958ae53e691.zip
Added :extension option to NumberHelper#number_to_phone #1361 [delynnb]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1438 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/number_helper.rb18
1 files changed, 8 insertions, 10 deletions
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb
index b494bef59a..b1757c864c 100644
--- a/actionpack/lib/action_view/helpers/number_helper.rb
+++ b/actionpack/lib/action_view/helpers/number_helper.rb
@@ -8,20 +8,18 @@ module ActionView
# The area code can be surrounded by parenthesis by setting +:area_code+ to true; default is false
# The delimiter can be set using +:delimiter+; default is "-"
# Examples:
- # number_to_phone(1235551234) => 123-555-1234
- # number_to_phone(1235551234, {:area_code => true}) => (123) 555-1234
- # number_to_phone(1235551234, {:delimiter => " "}) => 123 555 1234
+ # number_to_phone(1235551234) => 123-555-1234
+ # number_to_phone(1235551234, {:area_code => true}) => (123) 555-1234
+ # number_to_phone(1235551234, {:delimiter => " "}) => 123 555 1234
+ # number_to_phone(1235551234, {:area_code => true, :extension => 555}) => (123) 555-1234 x 555
def number_to_phone(number, options = {})
- options = options.stringify_keys
+ options = options.stringify_keys
area_code = options.delete("area_code") { false }
delimiter = options.delete("delimiter") { "-" }
+ extension = options.delete("extension") { "" }
begin
- str = number.to_s
- if area_code == true
- str.gsub!(/([0-9]{3})([0-9]{3})([0-9]{4})/,"(\\1) \\2#{delimiter}\\3")
- else
- str.gsub!(/([0-9]{3})([0-9]{3})([0-9]{4})/,"\\1#{delimiter}\\2#{delimiter}\\3")
- end
+ str = area_code == true ? number.to_s.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})/,"(\\1) \\2#{delimiter}\\3") : number.to_s.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})/,"\\1#{delimiter}\\2#{delimiter}\\3")
+ extension.to_s.strip.empty? ? str : "#{str} x #{extension.to_s.strip}"
rescue
number
end