diff --git a/app/admin/weather_arts.rb b/app/admin/weather_arts.rb index 9fd96c3..5e44e0b 100644 --- a/app/admin/weather_arts.rb +++ b/app/admin/weather_arts.rb @@ -1,4 +1,9 @@ ActiveAdmin.register WeatherArt do + controller do + def find_resource + scoped_collection.friendly.find(params[:id]) + end + end # See permitted parameters documentation: # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters # diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index 6a6367b..1fb8aa8 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -1,6 +1,18 @@ class CitiesController < ApplicationController def index @cities = City.all.order(:name) + @regions = Region.includes(:countries).order(:name) + @cities = City.includes(:country, country: :region).active.order(:name) + + if params[:region] + @current_region = Region.friendly.find(params[:region]) + @cities = @cities.by_region(@current_region.id) + end + + if params[:country] + @current_country = Country.friendly.find(params[:country]) + @cities = @cities.by_country(@current_country.id) + end end def show diff --git a/app/controllers/weather_arts_controller.rb b/app/controllers/weather_arts_controller.rb index 1f5a178..5ae79ec 100644 --- a/app/controllers/weather_arts_controller.rb +++ b/app/controllers/weather_arts_controller.rb @@ -1,6 +1,6 @@ class WeatherArtsController < ApplicationController def show @city = City.friendly.find(params[:city_id]) - @weather_art = @city.weather_arts.find(params[:id]) + @weather_art = @city.weather_arts.friendly.find(params[:slug]) end end diff --git a/app/models/city.rb b/app/models/city.rb index 28b42b9..86d8bf6 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -1,6 +1,6 @@ class City < ApplicationRecord extend FriendlyId - friendly_id :name, use: :slugged + friendly_id :slug_candidates, use: :slugged belongs_to :country has_many :weather_arts, dependent: :destroy @@ -11,10 +11,21 @@ class City < ApplicationRecord delegate :region, to: :country + scope :by_region, ->(region_id) { joins(:country).where(countries: { region_id: region_id }) } + scope :by_country, ->(country_id) { where(country_id: country_id) } + scope :active, -> { where(active: true) } + def to_s name end + def slug_candidates + [ + :name, + [ :country, :name ] + ] + end + def localized_name I18n.t("cities.#{name.parameterize.underscore}") end diff --git a/app/models/weather_art.rb b/app/models/weather_art.rb index fafa5ca..ca9c1e2 100644 --- a/app/models/weather_art.rb +++ b/app/models/weather_art.rb @@ -1,11 +1,21 @@ class WeatherArt < ApplicationRecord - belongs_to :city + extend FriendlyId + friendly_id :weather_date, use: :slugged + belongs_to :city has_one_attached :image validates :weather_date, presence: true validates :city_id, presence: true + def should_generate_new_friendly_id? + weather_date_changed? || city_id_changed? || super + end + + def to_s + "#{city.name} - #{weather_date.strftime('%Y-%m-%d')}" + end + def self.ransackable_associations(auth_object = nil) [ "city", "image_attachment", "image_blob" ] end diff --git a/app/views/cities/index.html.erb b/app/views/cities/index.html.erb index c72a81a..c995f4b 100644 --- a/app/views/cities/index.html.erb +++ b/app/views/cities/index.html.erb @@ -1,35 +1,119 @@ -
- Discover AI-generated weather art from cities around the world -
++ Discover AI-generated weather art from cities around the world +
+Lat: <%= city.latitude %>
-Long: <%= city.longitude %>
-+ <%= city.country.name %>, <%= city.region.name %> +
++ <%= city.country.name %>, <%= city.region.name %> +
+