Merge branch 'dev'

This commit is contained in:
songtianlun 2025-02-13 18:01:26 +08:00
commit 4844c8b983
6 changed files with 53 additions and 35 deletions

View File

@ -1,7 +1,8 @@
class HomeController < ApplicationController
def index
@popular_shuffle_arts = WeatherArt.by_popularity(10).shuffle.last(6)
@latest_arts = WeatherArt.latest(12)
@popular_arts = WeatherArt.by_popularity(3)
@random_arts = WeatherArt.random(3)
@latest_arts = WeatherArt.latest(6)
@featured_arts = WeatherArt.includes(:city).order(created_at: :desc).limit(5)
set_meta_tags(
title: "AI-Generated Weather Art",

View File

@ -31,6 +31,19 @@ class WeatherArt < ApplicationRecord
end
}
scope :random, ->(limit = 3) {
if ActiveRecord::Base.connection.adapter_name.downcase == "postgresql"
# PostgreSQL 优化版本
order(Arel.sql("RANDOM()")).limit(limit)
elsif ActiveRecord::Base.connection.adapter_name.downcase == "mysql2"
# MySQL 优化版本
order(Arel.sql("RAND()")).limit(limit)
else
# SQLite 或其他数据库的通用版本
order(Arel.sql("RANDOM()")).limit(limit)
end
}
def should_generate_new_friendly_id?
weather_date_changed? || city_id_changed? || super
end

View File

@ -93,7 +93,7 @@
<div class="divider my-1"></div>
<% @current_region.countries.order(:name).each do |country| %>
<li>
<%= link_to country.name,
<%= link_to "#{country&.emoji + " " || ""}#{country.name}",
cities_path(region: @current_region.slug, country: country.slug),
class: "#{@current_country == country ? 'active' : ''}" %>
</li>

View File

@ -32,7 +32,7 @@
<div class="flex flex-wrap justify-center items-center gap-3">
<div class="badge badge-lg badge-primary gap-2">
<%= @city.country.name %>, <%= @city.region %>
<%= "#{@city&.country&.emoji + " " || ""}#{@city&.country&.name}" %>, <%= @city.region %>
</div>
<div class="badge badge-lg badge-secondary gap-2">
<%= @city.timezone.present? ? Time.current.in_time_zone(@city.timezone).strftime("%Y-%m-%d %H:%M") : "Timezone undefined" %>

View File

@ -0,0 +1,29 @@
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
<% arts.each do |art| %>
<div class="card bg-base-100 shadow-xl hover:shadow-2xl transition-shadow duration-300">
<figure class="relative aspect-[4/3] overflow-hidden">
<% if art.image.attached? %>
<%= image_tag art.image, class: "w-full h-full object-cover transform hover:scale-105 transition-transform duration-500" %>
<% end %>
</figure>
<div class="card-body">
<div class="flex justify-between items-start">
<div>
<h3 class="card-title font-display"><%= art.city.name %></h3>
<p class="text-base-content/70">
<%= art.weather_date.strftime("%B %d, %Y") %>
</p>
</div>
<div class="text-right">
<div class="text-2xl font-bold"><%= art.temperature %>°C</div>
<div class="text-sm text-base-content/70"><%= art.description %></div>
</div>
</div>
<div class="card-actions justify-end mt-4">
<%= link_to "View Details", city_weather_art_path(art.city, art),
class: "btn btn-primary btn-outline" %>
</div>
</div>
</div>
<% end %>
</div>

View File

@ -25,37 +25,12 @@
<!-- 最新天气艺术 -->
<section class="container mx-auto px-4 py-16 space-y-12">
<h2 class="text-3xl font-display font-bold text-center">Shuffle Latest Weather Art</h2>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
<% @latest_arts.each do |art| %>
<div class="card bg-base-100 shadow-xl hover:shadow-2xl transition-shadow duration-300">
<figure class="relative aspect-[4/3] overflow-hidden">
<% if art.image.attached? %>
<%= image_tag art.image, class: "w-full h-full object-cover transform hover:scale-105 transition-transform duration-500" %>
<% end %>
</figure>
<div class="card-body">
<div class="flex justify-between items-start">
<div>
<h3 class="card-title font-display"><%= art.city.name %></h3>
<p class="text-base-content/70">
<%= art.weather_date.strftime("%B %d, %Y") %>
</p>
</div>
<div class="text-right">
<div class="text-2xl font-bold"><%= art.temperature %>°C</div>
<div class="text-sm text-base-content/70"><%= art.description %></div>
</div>
</div>
<div class="card-actions justify-end mt-4">
<%= link_to "View Details", city_weather_art_path(art.city, art),
class: "btn btn-primary btn-outline" %>
</div>
</div>
</div>
<% end %>
</div>
<h2 class="text-3xl font-display font-bold text-center">Latest Weather Art</h2>
<%= render 'home/arts', arts: @latest_arts %>
<h2 class="text-3xl font-display font-bold text-center">Popular Weather Art</h2>
<%= render 'home/arts', arts: @popular_arts %>
<h2 class="text-3xl font-display font-bold text-center">Random Weather Art</h2>
<%= render 'home/arts', arts: @random_arts %>
</section>
</div>
<div class="text-center mt-12 mb-12">