feat: add tag formatting helper

This commit is contained in:
songtianlun 2025-03-05 16:20:08 +08:00
parent 2d227d5871
commit 5de3d4306f
6 changed files with 24 additions and 7 deletions

View File

@ -1,4 +1,5 @@
class CitiesController < ApplicationController class CitiesController < ApplicationController
include TagHelper
before_action :authenticate_user!, only: [ :generate_weather_art ] before_action :authenticate_user!, only: [ :generate_weather_art ]
before_action :require_admin, only: [ :generate_weather_art ] before_action :require_admin, only: [ :generate_weather_art ]

View File

@ -1,4 +1,5 @@
class RssController < ApplicationController class RssController < ApplicationController
include TagHelper
def feed def feed
@weather_arts = WeatherArt.order(created_at: :desc).includes(:image_attachment, city: [ :country, :state ]).limit(20) @weather_arts = WeatherArt.order(created_at: :desc).includes(:image_attachment, city: [ :country, :state ]).limit(20)
@ -27,11 +28,11 @@ class RssController < ApplicationController
date_published: art.created_at.iso8601, date_published: art.created_at.iso8601,
image: art.image.attached? ? rails_blob_url(art.webp_image.processed) : nil, image: art.image.attached? ? rails_blob_url(art.webp_image.processed) : nil,
metadata: { metadata: {
country: art.city&.country&.name, country: format_as_tag(art.city&.country&.name),
city: art.city&.name, city: format_as_tag(art.city&.name),
state: art.city&.state&.name, state: format_as_tag(art.city&.state&.name),
region: art.city&.region&.name, region: format_as_tag(art.city&.region&.name),
subregion: art.city&.subregion&.name, subregion: format_as_tag(art.city&.subregion&.name),
description: art.description, description: art.description,
prompt: art.prompt, prompt: art.prompt,
date: art.weather_date&.strftime("%Y-%m-%d"), date: art.weather_date&.strftime("%Y-%m-%d"),

View File

@ -1,4 +1,5 @@
class WeatherArtsController < ApplicationController class WeatherArtsController < ApplicationController
include TagHelper
def show def show
@city = City.friendly.find(params[:city_id]) @city = City.friendly.find(params[:city_id])
@weather_art = @city.weather_arts.friendly.find(params[:slug]) @weather_art = @city.weather_arts.friendly.find(params[:slug])

14
app/helpers/tag_helper.rb Normal file
View File

@ -0,0 +1,14 @@
module TagHelper
def format_as_tag(text)
return nil if text.blank?
# 下划线方式 (选项1)
text.strip.gsub(/\s+/, "_")
# 或者删除空格 (选项2)
# text.strip.gsub(/\s+/, '')
# 或者驼峰式 (选项3)
# text.strip.gsub(/\s+(\w)/) { $1.upcase }.gsub(/\s+/, '')
end
end

View File

@ -87,7 +87,7 @@
<%= render "shared/share_social", <%= render "shared/share_social",
title: share_title, title: share_title,
description: share_description, description: share_description,
tags: "AIWeather,Art,AIart,Weather,#{@city&.name},#{@city&.country&.name}", tags: "AIWeather,Art,AIart,Weather,#{format_as_tag(@city&.name)},#{format_as_tag(@city&.country&.name)},#{format_as_tag(@city&.country&.region&.name)},#{format_as_tag(@city&.country&.subregion&.name)}",
image: url_for(@city&.latest_weather_art&.webp_image&.processed) image: url_for(@city&.latest_weather_art&.webp_image&.processed)
%> %>
</div> </div>

View File

@ -119,7 +119,7 @@
<%= render "shared/share_social", <%= render "shared/share_social",
title: share_title, title: share_title,
description: share_description, description: share_description,
tags: "AIWeather,Art,AIart,Weather,#{@weather_art.city&.name},#{@weather_art&.city&.country&.name}", tags: "AIWeather,Art,AIart,Weather,#{format_as_tag(@weather_art.city&.name)},#{format_as_tag(@weather_art.city&.country&.name)},#{format_as_tag(@weather_art.city&.country&.region&.name)},#{format_as_tag(@weather_art.city&.country&.subregion&.name)}",
image: url_for(@weather_art.webp_image.processed) image: url_for(@weather_art.webp_image.processed)
%> %>
</div> </div>