Merge pull request #5417 from tomhughes/social-share-helper

Refactor social sharing helper
This commit is contained in:
Andy Allan 2024-12-19 10:28:13 +00:00 committed by GitHub
commit f8b04635a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 72 deletions

View file

@ -12,71 +12,47 @@ module SocialShareButtonHelper
}.freeze }.freeze
# Generates a set of social share buttons based on the specified options. # Generates a set of social share buttons based on the specified options.
def render_social_share_buttons(opts = {}) def social_share_buttons(title:, url:)
sites = opts.fetch(:allow_sites, [])
valid_sites, invalid_sites = filter_allowed_sites(sites)
# Log invalid sites
invalid_sites.each do |invalid_site|
Rails.logger.error("Invalid site or icon not configured: #{invalid_site}")
end
tag.div( tag.div(
:class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3" :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3"
) do ) do
valid_sites.map do |site| safe_join(SOCIAL_SHARE_CONFIG.map do |site, icon|
link_options = { link_options = {
:rel => ["nofollow", opts[:rel]].compact, :rel => "nofollow",
:class => "ssb-icon rounded-circle", :class => "ssb-icon rounded-circle",
:title => I18n.t("application.share.#{site}.title"), :title => I18n.t("application.share.#{site}.title"),
:target => "_blank" :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_path(site), :alt => I18n.t("application.share.#{site}.alt"), :size => 28) image_tag(icon, :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
end end
end.join.html_safe end, "\n")
end end
end end
private private
def filter_allowed_sites(sites) def generate_share_url(site, title, url)
valid_sites = sites.empty? ? SOCIAL_SHARE_CONFIG.keys : sites.select { |site| valid_site?(site) }
invalid_sites = sites - valid_sites
[valid_sites, invalid_sites]
end
def icon_path(site)
SOCIAL_SHARE_CONFIG[site.to_sym] || ""
end
def valid_site?(site)
SOCIAL_SHARE_CONFIG.key?(site.to_sym)
end
def generate_share_url(site, params)
site = site.to_sym site = site.to_sym
title = URI.encode_www_form_component(title)
url = URI.encode_www_form_component(url)
case site case site
when :email when :email
to = params[:to] || "" "mailto:?subject=#{title}&body=#{url}"
subject = CGI.escape(params[:title])
body = CGI.escape(params[:url])
"mailto:#{to}?subject=#{subject}&body=#{body}"
when :x when :x
via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : "" "https://x.com/intent/tweet?url=#{url}&text=#{title}"
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}"
when :linkedin 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 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 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 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 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 else
raise ArgumentError, "Unsupported platform: #{platform}" raise ArgumentError, "Unsupported platform: #{platform}"
end end

View file

@ -15,10 +15,7 @@
<% end %> <% end %>
<%= render @entry %> <%= render @entry %>
<%= render_social_share_buttons({ <%= social_share_buttons(:title => @entry.title, :url => diary_entry_url(@entry.user, @entry)) %>
:title => @entry.title,
:url => diary_entry_url(@entry.user, @entry)
}) %>
<div id="comments" class="comments mb-3 overflow-hidden"> <div id="comments" class="comments mb-3 overflow-hidden">
<div class="row border-bottom border-secondary-subtle"> <div class="row border-bottom border-secondary-subtle">

View file

@ -3,34 +3,15 @@ require "test_helper"
class SocialShareButtonHelperTest < ActionView::TestCase class SocialShareButtonHelperTest < ActionView::TestCase
include SocialShareButtonHelper include SocialShareButtonHelper
def setup def test_social_share_buttons
@options = { buttons = social_share_buttons(:title => "Test Title", :url => "https://example.com")
:allow_sites => %w[x facebook linkedin], buttons_dom = Rails::Dom::Testing.html_document_fragment.parse(buttons)
:title => "Test Title",
:url => "https://example.com",
:desc => "Test Description",
:via => "testuser"
}
end
def test_render_social_share_buttons_with_valid_sites SOCIAL_SHARE_CONFIG.each_value do |icon|
result = render_social_share_buttons(@options) assert_dom buttons_dom, "div:has(a img[src='/images/#{icon}'])", :count => 1 do
assert_includes result, "x" assert_dom "a[href*='Test+Title']"
assert_includes result, "facebook" assert_dom "a[href*='https%3A%2F%2Fexample.com']"
assert_includes result, "linkedin" end
end
def test_render_social_share_buttons_with_invalid_site
@options[:allow_sites] << "invalid_site"
result = render_social_share_buttons(@options)
assert_not_includes result, "invalid_site"
end
def test_render_social_share_buttons_with_no_sites
@options[:allow_sites] = []
result = render_social_share_buttons(@options)
SocialShareButtonHelper::SOCIAL_SHARE_CONFIG.each_key do |site|
assert_includes result, site.to_s # Convert symbol to string
end end
end end
end end