2025-01-19 12:21:00 +08:00
class CitiesController < ApplicationController
2025-02-11 17:59:44 +08:00
before_action :authenticate_user! , only : [ :generate_weather_art ]
before_action :require_admin , only : [ :generate_weather_art ]
2025-02-11 17:40:13 +08:00
2025-01-19 12:21:00 +08:00
def index
2025-02-14 10:01:24 +08:00
@regions = Region . order ( :name )
2025-01-26 23:49:35 +08:00
@cities = City . includes ( :country , country : :region ) . order ( :name )
2025-02-17 00:15:32 +08:00
@latest_arts = WeatherArt . includes ( :city , :image_attachment ) . latest ( 1 )
2025-01-22 14:04:58 +08:00
2025-02-12 14:00:03 +08:00
if params [ :query ] . present?
@cities = @cities . search_by_name ( params [ :query ] )
end
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
2025-02-12 15:12:23 +08:00
if params [ :country ]
@current_country = Country . friendly . find ( params [ :country ] )
@cities = @cities . by_country ( @current_country . id ) if @current_country
end
2025-02-08 17:42:50 +08:00
@cities = @cities . page ( params [ :page ] ) . per ( 12 )
2025-01-23 19:02:52 +08:00
2025-02-12 14:47:30 +08:00
respond_to do | format |
format . html
format . turbo_stream {
render turbo_stream : turbo_stream . update ( " cities_results " ,
partial : " cities/results " ,
locals : { cities : @cities }
)
}
end
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-02-14 18:05:03 +08:00
@arts = @city . weather_arts . order ( weather_date : :desc ) . includes ( [ :image_attachment ] )
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. " ,
2025-02-24 14:08:25 +08:00
keywords : " #{ @city . name } , #{ @city . country . name } , ai art, weather art, AI visualization " ,
2025-01-23 19:02:52 +08:00
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