Skip to content

Commit 0a1b952

Browse files
committed
test: add meta description helper spec
1 parent 1e32636 commit 0a1b952

File tree

10 files changed

+83
-0
lines changed

10 files changed

+83
-0
lines changed

app/helpers/better_together/application_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ def host_community_logo_url
8080
rails_storage_proxy_url(attachment)
8181
end
8282

83+
# Sets a translated meta description for the current view. Provide the
84+
# translation scope without the `meta.descriptions` prefix.
85+
#
86+
# set_meta_description('communities.show', community_name: @resource.name)
87+
#
88+
# @param scope [String] translation scope under meta.descriptions
89+
# @param options [Hash] interpolation values for the translation
90+
def set_meta_description(scope, **options)
91+
content_for(:meta_description, t("meta.descriptions.#{scope}", **options))
92+
end
93+
8394
# Builds SEO-friendly meta tags for the current view. Defaults are derived
8495
# from translations and fall back to the Open Graph description when set.
8596
# rubocop:todo Metrics/MethodLength

app/views/better_together/communities/index.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<%= resource_class.model_name.human.pluralize %>
33
<% end %>
44

5+
<% set_meta_description('communities.index', platform_name: host_platform.name) %>
6+
57
<div class="container my-3">
68
<div class="d-flex justify-content-between align-items-center">
79
<h1><%= resource_class.model_name.human.pluralize %></h1>

app/views/better_together/communities/show.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<%= @resource.name %> | <%= resource_class.model_name.human.pluralize %>
33
<% end %>
44

5+
<% set_meta_description('communities.show', community_name: @resource.name, platform_name: host_platform.name) %>
6+
57
<div class="container-fluid mb-3 px-0">
68

79
<div class="profile-header position-relative">

app/views/better_together/conversations/index.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<%= BetterTogether::Conversation.model_name.human.pluralize %>
33
<% end %>
44

5+
<% set_meta_description('conversations.index', platform_name: host_platform.name) %>
6+
57
<div id="conversations_index">
68
<div class="p-2 bg-secondary rounded-0 text-white d-flex flex-row justify-content-between align-items-center">
79
<h5 class="mb-0">

app/views/better_together/conversations/show.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<%= "#{@conversation} | " if @conversation.to_s.present? %><%= @conversation.class.model_name.human.pluralize %>
55
<% end %>
66

7+
<% set_meta_description('conversations.show', conversation_subject: @conversation.to_s, platform_name: host_platform.name) %>
8+
79
<%= render 'communicator' do %>
810
<%= render partial: 'better_together/conversations/conversation_content', locals: { conversation: @conversation, messages: @messages, message: @message } %>
911
<% end %>

config/locales/en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,13 @@ en:
14861486
default_description: "Welcome to %{platform_name}"
14871487
page:
14881488
description_fallback: "Read %{title} on %{platform_name}"
1489+
descriptions:
1490+
communities:
1491+
index: "Discover communities on %{platform_name}."
1492+
show: "Learn about %{community_name} on %{platform_name}."
1493+
conversations:
1494+
index: "Browse conversations on %{platform_name}."
1495+
show: "View conversation %{conversation_subject} on %{platform_name}."
14891496
time:
14901497
am: am
14911498
formats:

config/locales/es.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,13 @@ es:
14781478
default_description: "Bienvenido a %{platform_name}"
14791479
page:
14801480
description_fallback: "Lee %{title} en %{platform_name}"
1481+
descriptions:
1482+
communities:
1483+
index: "Descubre comunidades en %{platform_name}."
1484+
show: "Conoce %{community_name} en %{platform_name}."
1485+
conversations:
1486+
index: "Explora tus conversaciones en %{platform_name}."
1487+
show: "Ver conversación %{conversation_subject} en %{platform_name}."
14811488
errors:
14821489
not_found:
14831490
title: "404 - Página no encontrada"

config/locales/fr.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,13 @@ fr:
15001500
default_description: "Bienvenue sur %{platform_name}"
15011501
page:
15021502
description_fallback: "Lire %{title} sur %{platform_name}"
1503+
descriptions:
1504+
communities:
1505+
index: "Découvrir les communautés sur %{platform_name}."
1506+
show: "En savoir plus sur %{community_name} sur %{platform_name}."
1507+
conversations:
1508+
index: "Parcourez vos conversations sur %{platform_name}."
1509+
show: "Voir la conversation %{conversation_subject} sur %{platform_name}."
15031510
errors:
15041511
not_found:
15051512
title: "404 - Page non trouvée"

docs/seo.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SEO
2+
3+
## Meta Descriptions
4+
5+
- Each high-traffic page should set a concise, unique `content_for :meta_description`.
6+
- Use the `set_meta_description` helper with translation keys under `meta.descriptions`.
7+
- Keep descriptions under 160 characters and include relevant keywords.
8+
- Example:
9+
10+
```erb
11+
<% set_meta_description('communities.show', community_name: @community.name, platform_name: host_platform.name) %>
12+
```
13+
14+
## Best Practices
15+
16+
- Translate descriptions using `config/locales/*` to support internationalization.
17+
- Prefer dynamic values (e.g., community or conversation names) to ensure uniqueness.
18+
- Review descriptions regularly to avoid duplication and improve click-through rates.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
require 'nokogiri'
5+
6+
module BetterTogether
7+
RSpec.describe ApplicationHelper, type: :helper do
8+
describe '#set_meta_description' do
9+
it 'stores translated description in content_for and renders meta tag' do
10+
allow(helper).to receive(:host_platform).and_return(double(name: 'MyPlatform'))
11+
allow(helper).to receive(:request).and_return(double(original_url: 'http://example.com'))
12+
allow(helper).to receive(:host_community_logo_url).and_return(nil)
13+
14+
helper.set_meta_description('communities.index', platform_name: 'MyPlatform')
15+
16+
expected = I18n.t('meta.descriptions.communities.index', platform_name: 'MyPlatform')
17+
expect(view.content_for(:meta_description)).to eq(expected)
18+
19+
html = Nokogiri::HTML.fragment(helper.seo_meta_tags)
20+
meta = html.at('meta[name="description"]')
21+
expect(meta['content']).to eq(expected)
22+
end
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)