From: Andy Allan Date: Thu, 17 Nov 2022 12:51:55 +0000 (+0000) Subject: Use the mail_to method to construct a mailto link for browse tag values X-Git-Tag: live~1475^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/97b7bac95669e838b2a507e21cb7d05416e52c0a?hp=ccc898b8a04f14537c17c924deb477d156967089 Use the mail_to method to construct a mailto link for browse tag values This is easier than building a mailto link ourselves. Also implement the missing title tag and write a test for it. --- diff --git a/app/helpers/browse_tags_helper.rb b/app/helpers/browse_tags_helper.rb index e7562081d..0784d03ca 100644 --- a/app/helpers/browse_tags_helper.rb +++ b/app/helpers/browse_tags_helper.rb @@ -22,7 +22,7 @@ module BrowseTagsHelper elsif url = wiki_link("tag", "#{key}=#{value}") link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value) elsif email = email_link(key, value) - link_to(h(email[:email]), email[:url], :title => t("browse.tag_details.email_link", :email => email[:email])) + mail_to(email, :title => t("browse.tag_details.email_link", :email => email)) elsif phones = telephone_links(key, value) # similarly, telephone_links() returns an array of phone numbers phones = phones.map do |p| @@ -136,10 +136,7 @@ module BrowseTagsHelper # remove any leading and trailing whitespace email = value.strip - if email.match?(URI::MailTo::EMAIL_REGEXP) - # add 'mailto:'' prefix - return { :email => email, :url => "mailto:#{email}" } - end + return email if email.match?(URI::MailTo::EMAIL_REGEXP) nil end diff --git a/config/locales/en.yml b/config/locales/en.yml index c560ded1c..b2d3065cb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -405,6 +405,7 @@ en: wikimedia_commons_link: "The %{page} item on Wikimedia Commons" telephone_link: "Call %{phone_number}" colour_preview: "Colour %{colour_value} preview" + email_link: "Email %{email}" note: title: "Note: %{id}" new_note: "New Note" diff --git a/test/helpers/browse_tags_helper_test.rb b/test/helpers/browse_tags_helper_test.rb index ddd92d5a2..3f36b9210 100644 --- a/test/helpers/browse_tags_helper_test.rb +++ b/test/helpers/browse_tags_helper_test.rb @@ -46,6 +46,9 @@ class BrowseTagsHelperTest < ActionView::TestCase html = format_value("colour", "#f00") assert_dom_equal %(#f00), html + + html = format_value("contact", "foo@example.com") + assert_dom_equal "foo@example.com", html end def test_wiki_link @@ -240,29 +243,23 @@ class BrowseTagsHelperTest < ActionView::TestCase assert_nil email email = email_link("email", "x@example.com") - assert_equal "x@example.com", email[:email] - assert_equal "mailto:x@example.com", email[:url] + assert_equal "x@example.com", email email = email_link("email", "other.email-with-hyphen@example.com") - assert_equal "other.email-with-hyphen@example.com", email[:email] - assert_equal "mailto:other.email-with-hyphen@example.com", email[:url] + assert_equal "other.email-with-hyphen@example.com", email email = email_link("email", "user.name+tag+sorting@example.com") - assert_equal "user.name+tag+sorting@example.com", email[:email] - assert_equal "mailto:user.name+tag+sorting@example.com", email[:url] + assert_equal "user.name+tag+sorting@example.com", email email = email_link("email", "dash-in@both-parts.com") - assert_equal "dash-in@both-parts.com", email[:email] - assert_equal "mailto:dash-in@both-parts.com", email[:url] + assert_equal "dash-in@both-parts.com", email email = email_link("email", "example@s.example") - assert_equal "example@s.example", email[:email] - assert_equal "mailto:example@s.example", email[:url] + assert_equal "example@s.example", email # Strips whitespace at ends email = email_link("email", " test@email.com ") - assert_equal "test@email.com", email[:email] - assert_equal "mailto:test@email.com", email[:url] + assert_equal "test@email.com", email end def test_telephone_links