Skip to content

Commit

Permalink
feat: Add meta tags and auto update page title
Browse files Browse the repository at this point in the history
  • Loading branch information
chonla committed Oct 23, 2024
1 parent 654c4b3 commit b0389d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cdmm/app/controllers/cdmm_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CdmmController < ApplicationController
include ApplicationHelper
# Viewing
# if form_key is empty, it will create a draft version and show the default data
# if form_key is in the database, it will populate data and show them
Expand Down Expand Up @@ -53,6 +54,7 @@ def save()
ev.form_status = :published
if ev.save
@table = evaluation_table(ev, form_key)
new_page_title = "<title id='page_title'>#{custom_title(ev[:title])}</title>"
respond_to do |format|
format.turbo_stream {
render turbo_stream: [
Expand All @@ -64,12 +66,14 @@ def save()
.replace("evaluation_form_title",
partial: "form_title",
locals: { text: @table[:title] }),
turbo_stream
.replace("page_title", new_page_title)
]
}
format.html {
redirect_to evaluation_show_path(ev.form_key), notice: 'Evaluation was successfully created.'
}
end
end
else
# Handle errors (e.g., re-render the form with errors)
render_internal_server_error
Expand Down
7 changes: 5 additions & 2 deletions cdmm/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module ApplicationHelper
def custom_title(title)
"#{title} #{default_meta_tags[:separator]} #{default_meta_tags[:site]}"
end

def default_meta_tags
{
site: 'CDMM',
Expand All @@ -10,7 +14,7 @@ def default_meta_tags
canonical: request.original_url,
noindex: !Rails.env.production?,
icon: [
{ href: image_url('box.svg'), type: 'image/svg+xml' },
# { href: image_url('box.svg'), type: 'image/svg+xml' },
# { href: image_url('icon.jpg'), rel: 'apple-touch-icon', sizes: '180x180', type: 'image/jpg' },
],
og: {
Expand All @@ -19,7 +23,6 @@ def default_meta_tags
description: 'The Continuous Delivery Maturity Assessment Form',
type: 'website',
url: request.original_url,
# image: image_url('login-page.png')
}
}
end
Expand Down
11 changes: 11 additions & 0 deletions cdmm/app/views/cdmm/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<%
set_meta_tags(
title: @table[:title],
description: @table[:title],
og: {
title: @table[:title],
description: @table[:title]
}
)
%>

<div class="w-full">
<%= render "cdmm_form", table: @table %>
</div>
2 changes: 1 addition & 1 deletion cdmm/config/initializers/meta_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# config.truncate_site_title_first = false

# Add HTML attributes to the <title> HTML tag. Default is {}.
# config.title_tag_attributes = {}
config.title_tag_attributes = { id: "page_title" }

# Natural separator when truncating. Default is " " (space character).
# Set to nil to disable natural separator.
Expand Down

0 comments on commit b0389d5

Please sign in to comment.