Skip to content

Commit b9eb9fe

Browse files
Add :country_code option to sms_to
`phone_to` supports a `:country_code` option, so add a `:country_code` option to `sms_to` for consistency.
1 parent 432bc68 commit b9eb9fe

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
* Deprecate `render` locals to be assigned to instance variables.
26

37
*Petrik de Heus*

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,24 @@ def current_page?(*args) # :nodoc:
598598
# If +name+ is not specified, +phone_number+ will be used as the name of
599599
# the link.
600600
#
601+
# A +country_code+ option is supported, which prepends a plus sign and the
602+
# given country code to the linked phone number. For example,
603+
# <tt>country_code: "01"</tt> will prepend <tt>+01</tt> to the linked
604+
# phone number.
605+
#
601606
# Additional HTML attributes for the link can be passed via +html_options+.
602607
#
603608
# ==== Options
609+
# * <tt>:country_code</tt> - Prepend the country code to the phone number.
604610
# * <tt>:body</tt> - Preset the body of the message.
605611
#
606612
# ==== Examples
607613
# sms_to "5155555785"
608614
# # => <a href="sms:5155555785;">5155555785</a>
609615
#
616+
# sms_to "5155555785", country_code: "01"
617+
# # => <a href="sms:+015155555785;">5155555785</a>
618+
#
610619
# sms_to "5155555785", "Text me"
611620
# # => <a href="sms:5155555785;">Text me</a>
612621
#
@@ -625,14 +634,14 @@ def sms_to(phone_number, name = nil, html_options = {}, &block)
625634
html_options, name = name, nil if name.is_a?(Hash)
626635
html_options = (html_options || {}).stringify_keys
627636

628-
extras = %w{ body }.map! { |item|
629-
option = html_options.delete(item).presence || next
630-
"#{item.dasherize}=#{ERB::Util.url_encode(option)}"
631-
}.compact
632-
extras = extras.empty? ? "" : "?&" + extras.join("&")
637+
country_code = html_options.delete("country_code").presence
638+
country_code = country_code ? "+#{ERB::Util.url_encode(country_code)}" : ""
639+
640+
body = html_options.delete("body").presence
641+
body = body ? "?&body=#{ERB::Util.url_encode(body)}" : ""
633642

634643
encoded_phone_number = ERB::Util.url_encode(phone_number)
635-
html_options["href"] = "sms:#{encoded_phone_number};#{extras}"
644+
html_options["href"] = "sms:#{country_code}#{encoded_phone_number};#{body}"
636645

637646
content_tag("a", name || phone_number, html_options, &block)
638647
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)