today_ai_weather/app/controllers/cities_controller.rb
songtianlun 6f21fe001e fix: remove trailing whitespace in admin files
- Clean up trailing whitespace in app/admin/state.rb
- Clean up trailing whitespace in app/admin/subregions.rb
- Clean up trailing whitespace in app/admin/users.rb
- Adjust formatting in cities_controller.rb for consistency

These changes improve code readability and maintainability by
removing unnecessary whitespace. It is a small clean-up that prepares
files for better version control parsing and reduces the diff clutter.
2025-02-12 10:02:47 +08:00

67 lines
2.1 KiB
Ruby

class CitiesController < ApplicationController
before_action :authenticate_user!, only: [ :generate_weather_art ]
before_action :require_admin, only: [ :generate_weather_art ]
def index
@regions = Region.includes(:countries).order(:name)
@cities = City.includes(:country, country: :region).order(:name)
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)
end
@cities = @cities.page(params[:page]).per(12)
set_meta_tags(
title: @current_region ? "Cities in #{@current_region.name}" : "Explore Cities",
description: "Discover weather art for cities #{@current_region ? "in #{@current_region.name}" : 'worldwide'}. Real-time AI-generated weather visualization.",
keywords: "#{@current_region&.name}, cities, weather art, AI visualization"
)
end
def show
@city = City.friendly.find(params[:id])
ahoy.track "View City", {
city_id: @city.id,
name: @city.name,
event_type: "city_view"
}
set_meta_tags(
title: @city.name,
description: "Experience #{@city.name}'s weather through AI-generated art. Daily updates of weather conditions visualized through artificial intelligence.",
keywords: "#{@city.name}, #{@city.country.name}, weather art, AI visualization",
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