1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
David Heinemeier Hansson 2005-06-16 06:33:10 +00:00
parent 43c470fae4
commit 665ab93761
3 changed files with 12 additions and 10 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Added :extension option to NumberHelper#number_to_phone #1361 [delynnb]
* Added button_to as a form-based solution to deal with harmful actions that should be hidden behind POSTs. This makes it just as easy as link_to to create a safe trigger for actions like destroy, although it's limited by being a block element, the fixed look, and a no-no inside other forms. #1371 [tom@moertel.com] * Added button_to as a form-based solution to deal with harmful actions that should be hidden behind POSTs. This makes it just as easy as link_to to create a safe trigger for actions like destroy, although it's limited by being a block element, the fixed look, and a no-no inside other forms. #1371 [tom@moertel.com]
* Fixed image_tag so an exception is not thrown just because the image is missing and alt value can't be generated #1395 [Marcel] * Fixed image_tag so an exception is not thrown just because the image is missing and alt value can't be generated #1395 [Marcel]

View file

@ -8,20 +8,18 @@ module ActionView
# The area code can be surrounded by parenthesis by setting +:area_code+ to true; default is false # 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 "-" # The delimiter can be set using +:delimiter+; default is "-"
# Examples: # Examples:
# number_to_phone(1235551234) => 123-555-1234 # number_to_phone(1235551234) => 123-555-1234
# number_to_phone(1235551234, {:area_code => true}) => (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, {:delimiter => " "}) => 123 555 1234
# number_to_phone(1235551234, {:area_code => true, :extension => 555}) => (123) 555-1234 x 555
def number_to_phone(number, options = {}) def number_to_phone(number, options = {})
options = options.stringify_keys options = options.stringify_keys
area_code = options.delete("area_code") { false } area_code = options.delete("area_code") { false }
delimiter = options.delete("delimiter") { "-" } delimiter = options.delete("delimiter") { "-" }
extension = options.delete("extension") { "" }
begin begin
str = number.to_s 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")
if area_code == true extension.to_s.strip.empty? ? str : "#{str} x #{extension.to_s.strip}"
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
rescue rescue
number number
end end

View file

@ -11,6 +11,8 @@ class NumberHelperTest < Test::Unit::TestCase
assert_equal("123-555-1234", number_to_phone(1235551234)) assert_equal("123-555-1234", number_to_phone(1235551234))
assert_equal("(123) 555-1234", number_to_phone(1235551234, {:area_code => true})) assert_equal("(123) 555-1234", number_to_phone(1235551234, {:area_code => true}))
assert_equal("123 555 1234", number_to_phone(1235551234, {:delimiter => " "})) assert_equal("123 555 1234", number_to_phone(1235551234, {:delimiter => " "}))
assert_equal("(123) 555-1234 x 555", number_to_phone(1235551234, {:area_code => true, :extension => 555}))
assert_equal("123-555-1234", number_to_phone(1235551234, :extension => " "))
end end
def test_number_to_currency def test_number_to_currency