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