diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index d6339b8cb3..dcb9ab114d 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `:country_code` option to `sms_to` for consistency with `phone_to`. + + *Jonathan Hefner* + * Deprecate `render` locals to be assigned to instance variables. *Petrik de Heus* diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb index 0faf51b100..e94cdd3c0f 100644 --- a/actionview/lib/action_view/helpers/url_helper.rb +++ b/actionview/lib/action_view/helpers/url_helper.rb @@ -598,15 +598,24 @@ module ActionView # If +name+ is not specified, +phone_number+ will be used as the name of # the link. # + # A +country_code+ option is supported, which prepends a plus sign and the + # given country code to the linked phone number. For example, + # country_code: "01" will prepend +01 to the linked + # phone number. + # # Additional HTML attributes for the link can be passed via +html_options+. # # ==== Options + # * :country_code - Prepend the country code to the phone number. # * :body - Preset the body of the message. # # ==== Examples # sms_to "5155555785" # # => 5155555785 # + # sms_to "5155555785", country_code: "01" + # # => 5155555785 + # # sms_to "5155555785", "Text me" # # => Text me # @@ -625,14 +634,14 @@ module ActionView html_options, name = name, nil if name.is_a?(Hash) html_options = (html_options || {}).stringify_keys - extras = %w{ body }.map! { |item| - option = html_options.delete(item).presence || next - "#{item.dasherize}=#{ERB::Util.url_encode(option)}" - }.compact - extras = extras.empty? ? "" : "?&" + extras.join("&") + country_code = html_options.delete("country_code").presence + country_code = country_code ? "+#{ERB::Util.url_encode(country_code)}" : "" + + body = html_options.delete("body").presence + body = body ? "?&body=#{ERB::Util.url_encode(body)}" : "" encoded_phone_number = ERB::Util.url_encode(phone_number) - html_options["href"] = "sms:#{encoded_phone_number};#{extras}" + html_options["href"] = "sms:#{country_code}#{encoded_phone_number};#{body}" content_tag("a", name || phone_number, html_options, &block) end diff --git a/actionview/test/template/url_helper_test.rb b/actionview/test/template/url_helper_test.rb index 5f7866490b..fca03cc056 100644 --- a/actionview/test/template/url_helper_test.rb +++ b/actionview/test/template/url_helper_test.rb @@ -768,18 +768,18 @@ class UrlHelperTest < ActiveSupport::TestCase def test_sms_to_with_options assert_dom_equal( - %{Text me}, - sms_to("15155555785", "Text me", class: "simple-class", body: "Hello from Jim") + %{Text me}, + sms_to("5155555785", "Text me", class: "simple-class", country_code: "01", body: "Hello from Jim") ) assert_dom_equal( - %{15155555785}, - sms_to("15155555785", class: "simple-class", body: "Hello from Jim") + %{5155555785}, + sms_to("5155555785", class: "simple-class", country_code: "01", body: "Hello from Jim") ) assert_dom_equal( - %{Text me}, - sms_to("15155555785", "Text me", body: "This is the body of the message.") + %{Text me}, + sms_to("5155555785", "Text me", body: "This is the body of the message.") ) end