aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib
diff options
context:
space:
mode:
authorPietro Moro <33845712+pietro-moro@users.noreply.github.com>2019-07-26 20:54:57 +0200
committerRafael França <rafael@franca.dev>2019-07-26 14:54:57 -0400
commit0eff6956a55882817f67c8d471d32ed3e4b109ee (patch)
treed690b7e65e14a9e5bb22ac2cb5969c0f01c4722d /actionview/lib
parent7f16fedad32f01664ad82829f244319bc752fcd7 (diff)
downloadrails-0eff6956a55882817f67c8d471d32ed3e4b109ee.tar.gz
rails-0eff6956a55882817f67c8d471d32ed3e4b109ee.tar.bz2
rails-0eff6956a55882817f67c8d471d32ed3e4b109ee.zip
Added a phone_to helper method, on the style of mail_to and sms_to. (#36775)
* Added a phone_to helper method, on the style of mail_to and sms_to. It creates an anchor tag with the href set to tel: *here your number* which, when clicked on a mobile phone, or on a desktop with a supported application, lets the phone app kick in, and it prepopulates it with the phone number specified. [Pietro Moro + Rafael Mendonça França]
Diffstat (limited to 'actionview/lib')
-rw-r--r--actionview/lib/action_view/helpers/url_helper.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb
index 85fd549177..9b4116834e 100644
--- a/actionview/lib/action_view/helpers/url_helper.rb
+++ b/actionview/lib/action_view/helpers/url_helper.rb
@@ -619,6 +619,53 @@ module ActionView
content_tag("a", name || phone_number, html_options, &block)
end
+ # Creates a TEL 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, the default app to make calls is opened, and it
+ # is prepopulated with the passed phone number and optional
+ # +country_code+ value.
+ #
+ # +phone_to+ has a optional +country_code+ option which automatically adds the country
+ # code as well as the + sign in the phone numer that gets prepopulated,
+ # for example if +country_code: "01"+ +\+01+ will be prepended to the
+ # phone numer, by passing special keys to +html_options+.
+ #
+ # ==== Options
+ # * <tt>:country_code</tt> - Prepends the country code to the number
+ #
+ # ==== Examples
+ # phone_to "1234567890"
+ # # => <a href="tel:1234567890">1234567890</a>
+ #
+ # phone_to "1234567890", "Phone me"
+ # # => <a href="tel:134567890">Phone me</a>
+ #
+ # phone_to "1234567890", "Phone me", country_code: "01"
+ # # => <a href="tel:+015155555785">Phone me</a>
+ #
+ # You can use a block as well if your link target is hard to fit into the name parameter. \ERB example:
+ #
+ # <%= phone_to "1234567890" do %>
+ # <strong>Phone me:</strong>
+ # <% end %>
+ # # => <a href="tel:1234567890">
+ # <strong>Phone me:</strong>
+ # </a>
+ def phone_to(phone_number, name = nil, html_options = {}, &block)
+ html_options, name = name, nil if block_given?
+ html_options = (html_options || {}).stringify_keys
+
+ country_code = html_options.delete("country_code").presence
+ country_code = country_code.nil? ? "" : "+#{ERB::Util.url_encode(country_code)}"
+
+ encoded_phone_number = ERB::Util.url_encode(phone_number)
+ html_options["href"] = "tel:#{country_code}#{encoded_phone_number}"
+
+ content_tag("a", name || phone_number, html_options, &block)
+ end
+
private
def convert_options_to_data_attributes(options, html_options)
if html_options