From e1a1bebd956a3334785ab52d28e27e4e73f63b41 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 18 Dec 2024 16:42:56 +0000 Subject: [PATCH 1/5] Drop render prefix from social_share_buttons function --- app/helpers/social_share_button_helper.rb | 2 +- app/views/diary_entries/show.html.erb | 8 ++++---- test/helpers/social_share_button_helper_test.rb | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/helpers/social_share_button_helper.rb b/app/helpers/social_share_button_helper.rb index edbd056a2e..2980e2ddaf 100644 --- a/app/helpers/social_share_button_helper.rb +++ b/app/helpers/social_share_button_helper.rb @@ -12,7 +12,7 @@ module SocialShareButtonHelper }.freeze # Generates a set of social share buttons based on the specified options. - def render_social_share_buttons(opts = {}) + def social_share_buttons(opts = {}) sites = opts.fetch(:allow_sites, []) valid_sites, invalid_sites = filter_allowed_sites(sites) diff --git a/app/views/diary_entries/show.html.erb b/app/views/diary_entries/show.html.erb index 14b1576f0f..f54ae1ca7e 100644 --- a/app/views/diary_entries/show.html.erb +++ b/app/views/diary_entries/show.html.erb @@ -15,10 +15,10 @@ <% end %> <%= render @entry %> -<%= render_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) + }) %>
diff --git a/test/helpers/social_share_button_helper_test.rb b/test/helpers/social_share_button_helper_test.rb index 397cbd1123..970e154455 100644 --- a/test/helpers/social_share_button_helper_test.rb +++ b/test/helpers/social_share_button_helper_test.rb @@ -13,8 +13,8 @@ def setup } end - def test_render_social_share_buttons_with_valid_sites - result = render_social_share_buttons(@options) + def test_social_share_buttons_with_valid_sites + result = social_share_buttons(@options) assert_includes result, "x" assert_includes result, "facebook" assert_includes result, "linkedin" @@ -22,13 +22,13 @@ def test_render_social_share_buttons_with_valid_sites def test_render_social_share_buttons_with_invalid_site @options[:allow_sites] << "invalid_site" - result = render_social_share_buttons(@options) + result = social_share_buttons(@options) assert_not_includes result, "invalid_site" end - def test_render_social_share_buttons_with_no_sites + def test_social_share_buttons_with_no_sites @options[:allow_sites] = [] - result = render_social_share_buttons(@options) + result = social_share_buttons(@options) SocialShareButtonHelper::SOCIAL_SHARE_CONFIG.each_key do |site| assert_includes result, site.to_s # Convert symbol to string end From dee02bccd3de5198f144bed24b658908eed0daa0 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 18 Dec 2024 16:45:42 +0000 Subject: [PATCH 2/5] Drop unused ability to filter social sharing sites --- app/helpers/social_share_button_helper.rb | 26 ++----------------- .../social_share_button_helper_test.rb | 23 +++++----------- 2 files changed, 8 insertions(+), 41 deletions(-) diff --git a/app/helpers/social_share_button_helper.rb b/app/helpers/social_share_button_helper.rb index 2980e2ddaf..0fdba2799f 100644 --- a/app/helpers/social_share_button_helper.rb +++ b/app/helpers/social_share_button_helper.rb @@ -13,18 +13,10 @@ module SocialShareButtonHelper # Generates a set of social share buttons based on the specified options. def social_share_buttons(opts = {}) - 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( :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3" ) do - valid_sites.map do |site| + SOCIAL_SHARE_CONFIG.map do |site, icon| link_options = { :rel => ["nofollow", opts[:rel]].compact, :class => "ssb-icon rounded-circle", @@ -33,7 +25,7 @@ def social_share_buttons(opts = {}) } link_to generate_share_url(site, opts), 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.join.html_safe end @@ -41,20 +33,6 @@ def social_share_buttons(opts = {}) private - def filter_allowed_sites(sites) - 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 case site diff --git a/test/helpers/social_share_button_helper_test.rb b/test/helpers/social_share_button_helper_test.rb index 970e154455..4903c3aed8 100644 --- a/test/helpers/social_share_button_helper_test.rb +++ b/test/helpers/social_share_button_helper_test.rb @@ -5,7 +5,6 @@ class SocialShareButtonHelperTest < ActionView::TestCase def setup @options = { - :allow_sites => %w[x facebook linkedin], :title => "Test Title", :url => "https://example.com", :desc => "Test Description", @@ -13,24 +12,14 @@ def setup } end - def test_social_share_buttons_with_valid_sites + def test_social_share_buttons result = social_share_buttons(@options) - assert_includes result, "x" + assert_includes result, "email" + assert_includes result, "bluesky" assert_includes result, "facebook" assert_includes result, "linkedin" - end - - def test_render_social_share_buttons_with_invalid_site - @options[:allow_sites] << "invalid_site" - result = social_share_buttons(@options) - assert_not_includes result, "invalid_site" - end - - def test_social_share_buttons_with_no_sites - @options[:allow_sites] = [] - result = social_share_buttons(@options) - SocialShareButtonHelper::SOCIAL_SHARE_CONFIG.each_key do |site| - assert_includes result, site.to_s # Convert symbol to string - end + assert_includes result, "mastodon" + assert_includes result, "telegram" + assert_includes result, "x" end end From 88946c3e8b3298cf07e9baadbf36cdc06cf789f9 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 18 Dec 2024 16:57:08 +0000 Subject: [PATCH 3/5] Drop unused options from social_share_buttons --- app/helpers/social_share_button_helper.rb | 30 +++++++++---------- app/views/diary_entries/show.html.erb | 5 +--- .../social_share_button_helper_test.rb | 11 +------ 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/app/helpers/social_share_button_helper.rb b/app/helpers/social_share_button_helper.rb index 0fdba2799f..90618a9245 100644 --- a/app/helpers/social_share_button_helper.rb +++ b/app/helpers/social_share_button_helper.rb @@ -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 @@ def social_share_buttons(opts = {}) 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 diff --git a/app/views/diary_entries/show.html.erb b/app/views/diary_entries/show.html.erb index f54ae1ca7e..9e3e7da650 100644 --- a/app/views/diary_entries/show.html.erb +++ b/app/views/diary_entries/show.html.erb @@ -15,10 +15,7 @@ <% 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)) %>
diff --git a/test/helpers/social_share_button_helper_test.rb b/test/helpers/social_share_button_helper_test.rb index 4903c3aed8..89ee3ff08d 100644 --- a/test/helpers/social_share_button_helper_test.rb +++ b/test/helpers/social_share_button_helper_test.rb @@ -3,17 +3,8 @@ 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" From 07fc605923eb6f64318b9559d3eecf979789ae3e Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 18 Dec 2024 17:08:04 +0000 Subject: [PATCH 4/5] Use safe_join to join social sharing buttons --- app/helpers/social_share_button_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/social_share_button_helper.rb b/app/helpers/social_share_button_helper.rb index 90618a9245..da49347c30 100644 --- a/app/helpers/social_share_button_helper.rb +++ b/app/helpers/social_share_button_helper.rb @@ -16,7 +16,7 @@ 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| + safe_join(SOCIAL_SHARE_CONFIG.map do |site, icon| link_options = { :rel => "nofollow", :class => "ssb-icon rounded-circle", @@ -27,7 +27,7 @@ def social_share_buttons(title:, url:) 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 + end, "\n") end end From fcb2b4459d72947219c8c6658b333b63b6e8ded8 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 18 Dec 2024 17:24:18 +0000 Subject: [PATCH 5/5] Improve testing of social sharing buttons --- test/helpers/social_share_button_helper_test.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test/helpers/social_share_button_helper_test.rb b/test/helpers/social_share_button_helper_test.rb index 89ee3ff08d..136298381a 100644 --- a/test/helpers/social_share_button_helper_test.rb +++ b/test/helpers/social_share_button_helper_test.rb @@ -4,13 +4,14 @@ class SocialShareButtonHelperTest < ActionView::TestCase include SocialShareButtonHelper def test_social_share_buttons - result = social_share_buttons(:title => "Test Title", :url => "https://example.com") - assert_includes result, "email" - assert_includes result, "bluesky" - assert_includes result, "facebook" - assert_includes result, "linkedin" - assert_includes result, "mastodon" - assert_includes result, "telegram" - assert_includes result, "x" + buttons = social_share_buttons(:title => "Test Title", :url => "https://example.com") + buttons_dom = Rails::Dom::Testing.html_document_fragment.parse(buttons) + + SOCIAL_SHARE_CONFIG.each_value do |icon| + assert_dom buttons_dom, "div:has(a img[src='/images/#{icon}'])", :count => 1 do + assert_dom "a[href*='Test+Title']" + assert_dom "a[href*='https%3A%2F%2Fexample.com']" + end + end end end