@@ -606,15 +606,24 @@ def current_page?(*args) # :nodoc:
606
606
# If +name+ is not specified, +phone_number+ will be used as the name of
607
607
# the link.
608
608
#
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
+ #
609
614
# Additional HTML attributes for the link can be passed via +html_options+.
610
615
#
611
616
# ==== Options
617
+ # * <tt>:country_code</tt> - Prepend the country code to the phone number.
612
618
# * <tt>:body</tt> - Preset the body of the message.
613
619
#
614
620
# ==== Examples
615
621
# sms_to "5155555785"
616
622
# # => <a href="sms:5155555785;">5155555785</a>
617
623
#
624
+ # sms_to "5155555785", country_code: "01"
625
+ # # => <a href="sms:+015155555785;">5155555785</a>
626
+ #
618
627
# sms_to "5155555785", "Text me"
619
628
# # => <a href="sms:5155555785;">Text me</a>
620
629
#
@@ -633,14 +642,14 @@ def sms_to(phone_number, name = nil, html_options = {}, &block)
633
642
html_options , name = name , nil if name . is_a? ( Hash )
634
643
html_options = ( html_options || { } ) . stringify_keys
635
644
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 ) } " : ""
641
650
642
651
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 } "
644
653
645
654
content_tag ( "a" , name || phone_number , html_options , &block )
646
655
end
0 commit comments