2025-01-19 12:21:00 +08:00
class CitiesController < ApplicationController
2025-02-11 17:40:13 +08:00
before_action :authenticate_user! , only : [ :generate_weather_art ]
before_action :require_admin , only : [ :generate_weather_art ]
2025-01-19 12:21:00 +08:00
def index
2025-01-22 14:04:58 +08:00
@regions = Region . includes ( :countries ) . order ( :name )
2025-01-26 23:49:35 +08:00
@cities = City . includes ( :country , country : :region ) . order ( :name )
2025-01-22 14:04:58 +08:00
if params [ :region ]
@current_region = Region . friendly . find ( params [ :region ] )
2025-02-08 17:42:50 +08:00
@cities = @cities . by_region ( @current_region . id ) if @current_region
2025-01-22 14:04:58 +08:00
end
if params [ :country ]
@current_country = Country . friendly . find ( params [ :country ] )
@cities = @cities . by_country ( @current_country . id )
end
2025-01-23 17:30:05 +08:00
2025-02-08 17:42:50 +08:00
@cities = @cities . page ( params [ :page ] ) . per ( 12 )
2025-01-23 19:02:52 +08:00
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 "
)
2025-01-19 12:21:00 +08:00
end
def show
2025-01-19 22:08:05 +08:00
@city = City . friendly . find ( params [ :id ] )
2025-01-27 00:43:18 +08:00
ahoy . track " View City " , {
city_id : @city . id ,
name : @city . name ,
2025-01-27 00:43:36 +08:00
event_type : " city_view "
2025-01-27 00:43:18 +08:00
}
2025-01-23 19:02:52 +08:00
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
}
)
2025-01-19 12:21:00 +08:00
end
2025-02-11 17:40:13 +08:00
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
2025-01-19 12:21:00 +08:00
end