From 3115b735ea46c0f4cc4cfc14584a23fd9dc043b7 Mon Sep 17 00:00:00 2001 From: Jim Jones Date: Tue, 18 Jun 2019 12:10:16 -0500 Subject: Helper method to create an sms link - when clicked it opens the phone/desktop's messaging client with the phone number and optional body value prepopulated. --- actionview/lib/action_view/helpers/url_helper.rb | 48 ++++++++++++++++++ actionview/test/template/url_helper_test.rb | 62 ++++++++++++++++++++++++ 2 files changed, 110 insertions(+) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb index df83dff681..97ebb850d0 100644 --- a/actionview/lib/action_view/helpers/url_helper.rb +++ b/actionview/lib/action_view/helpers/url_helper.rb @@ -571,6 +571,54 @@ module ActionView end end + # Creates an sms anchor link tag to the specified +phone_number+, which is + # also used as the name of the link unless +name+ is specified. Additional + # HTML attributes for the link can be passed in +html_options+. + # + # When clicked, an sms message is prepopulated with the passed phone number + # and optional +body+ value. + # + # +sms_to+ has a +body+ option for customizing the sms message itself by + # passing special keys to +html_options+. + # + # ==== Options + # * :body - Preset the body of the email. + # + # ==== Examples + # sms_to "5155555785" + # # => 5155555785 + # + # sms_to "5155555785", "Text me" + # # => Text me + # + # sms_to "5155555785", "Text me", + # subject: "Hello Jim I have a question about your product." + # # => Text me + # + # You can use a block as well if your link target is hard to fit into the name parameter. ERB example: + # + # <%= sms_to "5155555785" do %> + # Text me: + # <% end %> + # # => + # Text me: + # + def sms_to(phone_number, name = nil, html_options = {}, &block) + html_options, name = name, nil if block_given? + 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("&") + + encoded_phone_number = ERB::Util.url_encode(phone_number) + html_options["href"] = "sms:#{encoded_phone_number};#{extras}" + + content_tag("a", name || phone_number, html_options, &block) + end + private def convert_options_to_data_attributes(options, html_options) if html_options diff --git a/actionview/test/template/url_helper_test.rb b/actionview/test/template/url_helper_test.rb index 632b32f09f..bce6e7f370 100644 --- a/actionview/test/template/url_helper_test.rb +++ b/actionview/test/template/url_helper_test.rb @@ -708,6 +708,68 @@ class UrlHelperTest < ActiveSupport::TestCase assert_equal({ class: "special" }, options) end + def test_sms_to + assert_dom_equal %{15155555785}, sms_to("15155555785") + assert_dom_equal %{Jim Jones}, sms_to("15155555785", "Jim Jones") + assert_dom_equal( + %{Jim Jones}, + sms_to("15155555785", "Jim Jones", "class" => "admin") + ) + assert_equal sms_to("15155555785", "Jim Jones", "class" => "admin"), + sms_to("15155555785", "Jim Jones", class: "admin") + end + + def test_sms_to_with_options + assert_dom_equal( + %{Text me}, + sms_to("15155555785", "Text me", class: "simple-class", body: "Hello from Jim") + ) + + assert_dom_equal( + %{Text me}, + sms_to("15155555785", "Text me", body: "This is the body of the message.") + ) + end + + def test_sms_with_img + assert_dom_equal %{}, + sms_to("15155555785", raw('')) + end + + def test_sms_with_html_safe_string + assert_dom_equal( + %{1+5155555785}, + sms_to(raw("1+5155555785")) + ) + end + + def test_sms_with_nil + assert_dom_equal( + %{}, + sms_to(nil) + ) + end + + def test_sms_returns_html_safe_string + assert_predicate sms_to("15155555785"), :html_safe? + end + + def test_sms_with_block + assert_dom_equal %{Text me}, + sms_to("15155555785") { content_tag(:span, "Text me") } + end + + def test_sms_with_block_and_options + assert_dom_equal %{Text me}, + sms_to("15155555785", body: "Hello from Jim", class: "special") { content_tag(:span, "Text me") } + end + + def test_sms_does_not_modify_html_options_hash + options = { class: "special" } + sms_to "15155555785", "ME!", options + assert_equal({ class: "special" }, options) + end + def protect_against_forgery? request_forgery end -- cgit v1.2.3 From d2d15ad8a540e2f519de2ea905aff2305b038450 Mon Sep 17 00:00:00 2001 From: Jim Jones Date: Tue, 18 Jun 2019 14:01:43 -0500 Subject: Doc changes. --- actionview/lib/action_view/helpers/url_helper.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb index 97ebb850d0..4174e41c64 100644 --- a/actionview/lib/action_view/helpers/url_helper.rb +++ b/actionview/lib/action_view/helpers/url_helper.rb @@ -571,18 +571,18 @@ module ActionView end end - # Creates an sms anchor link tag to the specified +phone_number+, which is + # Creates an SMS anchor link tag to the specified +phone_number+, which is # also used as the name of the link unless +name+ is specified. Additional # HTML attributes for the link can be passed in +html_options+. # - # When clicked, an sms message is prepopulated with the passed phone number + # When clicked, an SMS message is prepopulated with the passed phone number # and optional +body+ value. # - # +sms_to+ has a +body+ option for customizing the sms message itself by + # +sms_to+ has a +body+ option for customizing the SMS message itself by # passing special keys to +html_options+. # # ==== Options - # * :body - Preset the body of the email. + # * :body - Preset the body of the message. # # ==== Examples # sms_to "5155555785" -- cgit v1.2.3