Skip to content

Commit 91593af

Browse files
committed
Merge pull request rails#41559 from jonathanhefner/sms_to-country_code
Add :country_code option to sms_to
2 parents 56d8ff8 + b9eb9fe commit 91593af

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

actionview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add `:country_code` option to `sms_to` for consistency with `phone_to`.
2+
3+
*Jonathan Hefner*
4+
15
* OpenSSL constants are now used for Digest computations.
26

37
*Dirkjan Bussink*

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,15 +606,24 @@ def current_page?(*args) # :nodoc:
606606
# If +name+ is not specified, +phone_number+ will be used as the name of
607607
# the link.
608608
#
609+
# A +country_code+ option is supported, which prepends a plus sign and the
610+
# given country code to the linked phone number. For example,
611+
# <tt>country_code: "01"</tt> will prepend <tt>+01</tt> to the linked
612+
# phone number.
613+
#
609614
# Additional HTML attributes for the link can be passed via +html_options+.
610615
#
611616
# ==== Options
617+
# * <tt>:country_code</tt> - Prepend the country code to the phone number.
612618
# * <tt>:body</tt> - Preset the body of the message.
613619
#
614620
# ==== Examples
615621
# sms_to "5155555785"
616622
# # => <a href="sms:5155555785;">5155555785</a>
617623
#
624+
# sms_to "5155555785", country_code: "01"
625+
# # => <a href="sms:+015155555785;">5155555785</a>
626+
#
618627
# sms_to "5155555785", "Text me"
619628
# # => <a href="sms:5155555785;">Text me</a>
620629
#
@@ -633,14 +642,14 @@ def sms_to(phone_number, name = nil, html_options = {}, &block)
633642
html_options, name = name, nil if name.is_a?(Hash)
634643
html_options = (html_options || {}).stringify_keys
635644

636-
extras = %w{ body }.map! { |item|
637-
option = html_options.delete(item).presence || next
638-
"#{item.dasherize}=#{ERB::Util.url_encode(option)}"
639-
}.compact
640-
extras = extras.empty? ? "" : "?&" + extras.join("&")
645+
country_code = html_options.delete("country_code").presence
646+
country_code = country_code ? "+#{ERB::Util.url_encode(country_code)}" : ""
647+
648+
body = html_options.delete("body").presence
649+
body = body ? "?&body=#{ERB::Util.url_encode(body)}" : ""
641650

642651
encoded_phone_number = ERB::Util.url_encode(phone_number)
643-
html_options["href"] = "sms:#{encoded_phone_number};#{extras}"
652+
html_options["href"] = "sms:#{country_code}#{encoded_phone_number};#{body}"
644653

645654
content_tag("a", name || phone_number, html_options, &block)
646655
end

actionview/test/template/url_helper_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,18 @@ def test_sms_to
768768

769769
def test_sms_to_with_options
770770
assert_dom_equal(
771-
%{<a class="simple-class" href="sms:15155555785;?&body=Hello%20from%20Jim">Text me</a>},
772-
sms_to("15155555785", "Text me", class: "simple-class", body: "Hello from Jim")
771+
%{<a class="simple-class" href="sms:+015155555785;?&body=Hello%20from%20Jim">Text me</a>},
772+
sms_to("5155555785", "Text me", class: "simple-class", country_code: "01", body: "Hello from Jim")
773773
)
774774

775775
assert_dom_equal(
776-
%{<a class="simple-class" href="sms:15155555785;?&body=Hello%20from%20Jim">15155555785</a>},
777-
sms_to("15155555785", class: "simple-class", body: "Hello from Jim")
776+
%{<a class="simple-class" href="sms:+015155555785;?&body=Hello%20from%20Jim">5155555785</a>},
777+
sms_to("5155555785", class: "simple-class", country_code: "01", body: "Hello from Jim")
778778
)
779779

780780
assert_dom_equal(
781-
%{<a href="sms:15155555785;?&body=This%20is%20the%20body%20of%20the%20message.">Text me</a>},
782-
sms_to("15155555785", "Text me", body: "This is the body of the message.")
781+
%{<a href="sms:5155555785;?&body=This%20is%20the%20body%20of%20the%20message.">Text me</a>},
782+
sms_to("5155555785", "Text me", body: "This is the body of the message.")
783783
)
784784
end
785785

0 commit comments

Comments
 (0)