]> git.openstreetmap.org Git - rails.git/commitdiff
Drop unused options from social_share_buttons
authorTom Hughes <tom@compton.nu>
Wed, 18 Dec 2024 16:57:08 +0000 (16:57 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 19 Dec 2024 08:16:58 +0000 (08:16 +0000)
app/helpers/social_share_button_helper.rb
app/views/diary_entries/show.html.erb
test/helpers/social_share_button_helper_test.rb

index 0fdba2799f58a42b77fa803abc90ac08c46ccf1e..90618a9245662a3975fee24f114f06ddd963d170 100644 (file)
@@ -12,19 +12,19 @@ module SocialShareButtonHelper
   }.freeze
 
   # Generates a set of social share buttons based on the specified options.
-  def social_share_buttons(opts = {})
+  def social_share_buttons(title:, url:)
     tag.div(
       :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3"
     ) do
       SOCIAL_SHARE_CONFIG.map do |site, icon|
         link_options = {
-          :rel => ["nofollow", opts[:rel]].compact,
+          :rel => "nofollow",
           :class => "ssb-icon rounded-circle",
           :title => I18n.t("application.share.#{site}.title"),
           :target => "_blank"
         }
 
-        link_to generate_share_url(site, opts), link_options do
+        link_to generate_share_url(site, title, url), link_options do
           image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
         end
       end.join.html_safe
@@ -33,28 +33,26 @@ module SocialShareButtonHelper
 
   private
 
-  def generate_share_url(site, params)
+  def generate_share_url(site, title, url)
     site = site.to_sym
+    title = URI.encode_www_form_component(title)
+    url = URI.encode_www_form_component(url)
+
     case site
     when :email
-      to = params[:to] || ""
-      subject = CGI.escape(params[:title])
-      body = CGI.escape(params[:url])
-      "mailto:#{to}?subject=#{subject}&body=#{body}"
+      "mailto:?subject=#{title}&body=#{url}"
     when :x
-      via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : ""
-      hashtags_str = params[:hashtags] ? "&hashtags=#{URI.encode_www_form_component(params[:hashtags].join(','))}" : ""
-      "https://x.com/intent/tweet?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}#{hashtags_str}#{via_str}"
+      "https://x.com/intent/tweet?url=#{url}&text=#{title}"
     when :linkedin
-      "https://www.linkedin.com/sharing/share-offsite/?url=#{URI.encode_www_form_component(params[:url])}"
+      "https://www.linkedin.com/sharing/share-offsite/?url=#{url}"
     when :facebook
-      "https://www.facebook.com/sharer/sharer.php?u=#{URI.encode_www_form_component(params[:url])}&t=#{URI.encode_www_form_component(params[:title])}"
+      "https://www.facebook.com/sharer/sharer.php?u=#{url}&t=#{title}"
     when :mastodon
-      "https://mastodonshare.com/?text=#{URI.encode_www_form_component(params[:title])}&url=#{URI.encode_www_form_component(params[:url])}"
+      "https://mastodonshare.com/?text=#{title}&url=#{url}"
     when :telegram
-      "https://t.me/share/url?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}"
+      "https://t.me/share/url?url=#{url}&text=#{title}"
     when :bluesky
-      "https://bsky.app/intent/compose?text=#{URI.encode_www_form_component(params[:title])}+#{URI.encode_www_form_component(params[:url])}"
+      "https://bsky.app/intent/compose?text=#{title}+#{url}"
     else
       raise ArgumentError, "Unsupported platform: #{platform}"
     end
index f54ae1ca7e043afd8d114034625e3c669071dfb4..9e3e7da650ea0c7b8475153367056849a34b4765 100644 (file)
 <% end %>
 
 <%= render @entry %>
-<%= social_share_buttons({
-                           :title => @entry.title,
-                           :url => diary_entry_url(@entry.user, @entry)
-                         }) %>
+<%= social_share_buttons(:title => @entry.title, :url => diary_entry_url(@entry.user, @entry)) %>
 
 <div id="comments" class="comments mb-3 overflow-hidden">
   <div class="row border-bottom border-secondary-subtle">
index 4903c3aed8d9a5dd32a5869717fc82a39ffccf5c..89ee3ff08d748211c89c7af47a7cd0853c2f4b04 100644 (file)
@@ -3,17 +3,8 @@ require "test_helper"
 class SocialShareButtonHelperTest < ActionView::TestCase
   include SocialShareButtonHelper
 
-  def setup
-    @options = {
-      :title => "Test Title",
-      :url => "https://example.com",
-      :desc => "Test Description",
-      :via => "testuser"
-    }
-  end
-
   def test_social_share_buttons
-    result = social_share_buttons(@options)
+    result = social_share_buttons(:title => "Test Title", :url => "https://example.com")
     assert_includes result, "email"
     assert_includes result, "bluesky"
     assert_includes result, "facebook"