From 36f0bf00c0fe78ee3176aa34ae360996959d8c93 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Mon, 3 Aug 2026 21:22:15 +0100 Subject: [PATCH] Adjust OG/twitter meta tags based on current space --- app/views/common/_default_og.html.erb | 20 +++++++----- app/views/layouts/_head.html.erb | 2 +- test/config/test_tess.yml | 2 +- test/controllers/static_controller_test.rb | 36 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/app/views/common/_default_og.html.erb b/app/views/common/_default_og.html.erb index 557c13c60..e988bcb29 100644 --- a/app/views/common/_default_og.html.erb +++ b/app/views/common/_default_og.html.erb @@ -1,12 +1,18 @@ +<% title = Space.current_space.default? ? TeSS::Config.site['title'] : Space.current_space.title %> +<% image = Space.current_space.default? ? TeSS::Config.site['logo_open_graph'] : Space.current_space.image&.url %> +<% image ||= TeSS::Config.site['logo'] %> +<% description = strip_tags(Space.current_space.default? ? t('home.subtitle') : render_sanitized_markdown(Space.current_space.description)).strip %> +<% twitter = TeSS::Config.twitter_handle if Space.current_space.default? %> + <% # default twitter meta tags -%> <%= tag(:meta, name: "twitter:card", content: 'summary_large_image') %> -<%= tag(:meta, name: "twitter:site", content: TeSS::Config.twitter_handle) unless TeSS::Config.twitter_handle.blank? %> -<%= tag(:meta, name: "twitter:image", content: asset_url(TeSS::Config.site['logo'])) %> -<%= tag(:meta, name: "twitter:title", content: TeSS::Config.site['title']) %> -<%= tag(:meta, name: "twitter:description", content: strip_tags(t('home.subtitle'))) %> +<%= tag(:meta, name: "twitter:site", content: twitter) if twitter.present? %> +<%= tag(:meta, name: "twitter:image", content: asset_url(image)) if image.present? %> +<%= tag(:meta, name: "twitter:title", content: title) %> +<%= tag(:meta, name: "twitter:description", content: description) if description.present? %> <% # default OpenGraph meta tags -%> <%= tag(:meta, property: "og:url", content: request.original_url) %> -<%= tag(:meta, property: "og:image", content: asset_url(TeSS::Config.site['logo_open_graph'])) %> -<%= tag(:meta, property: "og:title", content: TeSS::Config.site['title']) %> -<%= tag(:meta, property: "og:description", content: strip_tags(t('home.subtitle'))) %> +<%= tag(:meta, property: "og:image", content: asset_url(image)) if image.present? %> +<%= tag(:meta, property: "og:title", content: title) %> +<%= tag(:meta, property: "og:description", content: description) if description.present? %> diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb index 5b83760d8..340c0b03d 100644 --- a/app/views/layouts/_head.html.erb +++ b/app/views/layouts/_head.html.erb @@ -22,7 +22,7 @@ <% if content_for? :og_header %> <%= yield :og_header %> <% else %> - <%= render :partial => 'common/default_og' %> + <%= render partial: 'common/default_og' %> <% end %> <% if @bioschemas.present? %> diff --git a/test/config/test_tess.yml b/test/config/test_tess.yml index b2511a0fe..03a874906 100644 --- a/test/config/test_tess.yml +++ b/test/config/test_tess.yml @@ -4,7 +4,7 @@ default: &default sender_email: no-reply@example.com solr_enabled: false default_role: registered_user - twitter_handle: '@handle' + twitter_handle: '@test_handle' announcement_message: prevent_robot_indexing: false require_cookie_consent: true diff --git a/test/controllers/static_controller_test.rb b/test/controllers/static_controller_test.rb index 8e57815c1..3238f7275 100644 --- a/test/controllers/static_controller_test.rb +++ b/test/controllers/static_controller_test.rb @@ -813,4 +813,40 @@ class StaticControllerTest < ActionController::TestCase get :home assert_select 'head title', text: 'TeSS Test Instance' end + + test 'includes og and twitter meta tags' do + get :home + assert_response :success + + assert_select 'meta[name="twitter:card"][content=?]', 'summary_large_image' + assert_select 'meta[name="twitter:site"][content=?]', '@test_handle' + assert_select 'meta[name="twitter:title"][content=?]', 'TeSS Test Instance' + assert_select 'meta[name="twitter:description"][content*=?]', "Search the portal" + + assert_select 'meta[property="og:url"][content=?]', 'http://test.host/' + assert_select 'meta[property="og:title"][content=?]', 'TeSS Test Instance' + assert_select 'meta[property="og:description"][content*=?]', "Search the portal" + end + + test 'includes og and twitter meta tags for space' do + mock_images + space = spaces(:plants) + space.update!(image_url: 'http://example.com/goblet.png', description: 'Plants in space') + + with_host(space.host) do + get :home + assert_response :success + + assert_select 'meta[name="twitter:card"][content=?]', 'summary_large_image' + assert_select 'meta[name="twitter:site"]', count: 0 + assert_select 'meta[name="twitter:image"][content*=?]', 'goblet.png' + assert_select 'meta[name="twitter:title"][content=?]', 'TeSS Plants Community' + assert_select 'meta[name="twitter:description"][content*=?]', "Plants in space" + + assert_select 'meta[property="og:url"][content=?]','http://plants.mytess.training/' + assert_select 'meta[property="og:image"][content*=?]', 'goblet.png' + assert_select 'meta[property="og:title"][content=?]', 'TeSS Plants Community' + assert_select 'meta[property="og:description"][content*=?]', "Plants in space" + end + end end