today_ai_weather/app/controllers/cities_controller.rb
songtianlun 6f01174ea9 feat: enhance city description generation
- Modify get_description method to include an additional
  parameter for user context
- Update usage in views to reflect the new parameter
- Adjust temporary description message based on user context

This change enhances the city description generation logic by
allowing differentiation in how the description is provided
depending on whether the calling context is a user. It improves
the user experience while ensuring that non-user contexts
handle descriptions differently. This minor enhancement also
included updating the view to support this new logic.
2025-04-12 14:46:44 +08:00

97 lines
3.2 KiB
Ruby

class CitiesController < ApplicationController
include TagHelper
before_action :authenticate_user!, only: [ :generate_weather_art ]
before_action :require_admin, only: [ :generate_weather_art ]
def index
@regions = Region.order(:name)
@cities = City.includes(:country, :state).order(:name)
@latest_arts = WeatherArt.includes(:city, :image_attachment).latest(1)
if params[:query].present?
@cities = @cities.search_by_name(params[:query])
end
if params[:region]
@current_region = Region.friendly.find(params[:region])
@cities = @cities.by_region(@current_region.id) if @current_region
end
if params[:country]
@current_country = Country.friendly.find(params[:country])
@cities = @cities.by_country(@current_country.id) if @current_country
end
if params[:state]
@current_state = State.friendly.find(params[:state])
@cities = @cities.by_state(@current_state.id) if @current_state
end
@cities = @cities.page(params[:page]).per(12)
respond_to do |format|
format.html
format.turbo_stream {
render turbo_stream: turbo_stream.update("cities_results",
partial: "cities/results",
locals: {
cities: @cities,
pagination_params: {
region: params[:region],
country: params[:country],
state: params[:state],
query: params[:query]
}
}
)
}
end
set_meta_tags(
title: t("meta.cities.index.title"),
description: t("meta.cities.index.description"),
keywords: t("meta.cities.index.keywords")
)
end
def show
@city = City.friendly.find(params[:id])
@arts = @city.weather_arts.order(weather_date: :desc).includes([ :image_attachment ])
ahoy.track "View City", {
city_id: @city.id,
name: @city.name,
event_type: "city_view"
}
set_meta_tags(
title: t("meta.cities.show.title", city_name: @city.name, country_name: @city&.country&.name.to_s),
description: t("meta.cities.show.description", city_name: @city.get_description("en", false, true, false)),
keywords: t("meta.cities.show.keywords", city_name: @city.name, country_name: @city.country&.name.to_s),
og: {
image: @city.latest_weather_art&.image&.attached? ? url_for(@city.latest_weather_art.image) : nil
}
)
end
def generate_weather_art
@city = City.friendly.find(params[:id])
GenerateWeatherArtWorker.perform_async(@city.id)
respond_to do |format|
format.html do
flash[:notice] = "Weather art generation has been queued"
redirect_to @city
end
end
end
private
def require_admin
unless current_user&.admin?
flash[:error] = "You are not authorized to perform this action"
redirect_to root_path
end
end
end