From a4de04874d7fefff2bd1e3b7e569ea7a10e1a48b Mon Sep 17 00:00:00 2001 From: songtianlun Date: Thu, 23 Jan 2025 14:13:32 +0800 Subject: [PATCH] feat: implement filtering and pagination for weather arts - Add region selection for filtering weather arts by region - Implement sorting options for newest and oldest entries - Update pagination to show links for each page when applicable - Adjust the number of items displayed per page to 10 This commit enhances the user experience by allowing users to filter and sort weather arts based on their preferences. It also improves the pagination logic to provide more manageable navigation through large datasets, making it easier for users to find the artworks they are interested in. --- app/controllers/arts_controller.rb | 21 +++- app/views/arts/index.html.erb | 185 +++++++++++++++++++++++------ 2 files changed, 163 insertions(+), 43 deletions(-) diff --git a/app/controllers/arts_controller.rb b/app/controllers/arts_controller.rb index 07fabb9..b70273a 100644 --- a/app/controllers/arts_controller.rb +++ b/app/controllers/arts_controller.rb @@ -1,8 +1,21 @@ class ArtsController < ApplicationController def index - @weather_arts = WeatherArt.includes(:city, city: [ :country, { country: :region } ]) - .order(created_at: :desc) - .page(params[:page]) - .per(2) + @regions = Region.all + @current_region = Region.find(params[:region]) if params[:region].present? + + @weather_arts = WeatherArt.includes(city: [:country, { country: :region }]) + + if @current_region + @weather_arts = @weather_arts.joins(city: :country) + .where(countries: { region_id: @current_region.id }) + end + + @weather_arts = if params[:sort] == 'oldest' + @weather_arts.order(created_at: :asc) + else + @weather_arts.order(created_at: :desc) + end + + @weather_arts = @weather_arts.page(params[:page]).per(10) end end diff --git a/app/views/arts/index.html.erb b/app/views/arts/index.html.erb index c9eb43b..ddb77f4 100644 --- a/app/views/arts/index.html.erb +++ b/app/views/arts/index.html.erb @@ -1,66 +1,167 @@
- -
-
-

- Weather Arts Gallery -

-

- Explore our collection of AI-generated weather art from cities around the world -

+ + <% featured_art = @weather_arts.first %> +
+ + <% if featured_art&.image&.attached? %> +
+ <%= image_tag featured_art.image, + class: "w-full h-full object-cover" %> +
+
+ <% end %> + + +
+
+
+

+ Weather Arts Gallery +

+

+ Discover AI-generated weather art from cities around the world +

+ + + <% if featured_art %> +
+ Latest from <%= featured_art.city.name %>, <%= featured_art.city.country.name %> + + <%= featured_art.weather_date.strftime("%B %d, %Y") %> +
+ <% end %> +
+
-
+ +
+
+ +
+ + + + + +
+ + +
+ Showing <%= @weather_arts.total_count %> weather arts + <% if @current_region %> + from <%= @current_region.name %> + <% end %> +
+
+
+ + +
<% @weather_arts.each do |art| %> -
- +
+
<% if art.image.attached? %> <%= image_tag art.image, class: "w-full h-full object-cover transform group-hover:scale-105 transition-transform duration-500" %> - -
- -
-

- <%= art.city.name %> -

-

- <%= art.weather_date.strftime("%B %d, %Y") %> -

-

- <%= art.temperature %>°C, <%= art.description %> -

+ +
+ + +
+
+

+ <%= art.city.name %> +

+

+ <%= art.city.country.name %> +

+
+ + + + <%= art.description %> +
+
<% end %>
- +
-
+
-

+

<%= art.city.name %>

- <%= art.city.country.name %> + <%= art.weather_date.strftime("%B %d, %Y") %>

-
<%= art.temperature %>°C
+
+ <%= art.temperature %>°C +
- <%= art.weather_date.strftime("%b %d") %> + <%= art.humidity %>% humidity
<%= link_to city_weather_art_path(art.city, art), - class: "btn btn-ghost btn-sm w-full mt-3" do %> + class: "btn btn-primary btn-sm w-full" do %> View Details + + + <% end %>
@@ -68,13 +169,19 @@
-
-
- <%= link_to_prev_page @weather_arts, 'Previous', - class: "btn btn-outline #{'btn-disabled' unless @weather_arts.prev_page}" %> - <%= link_to_next_page @weather_arts, 'Next', - class: "btn btn-outline #{'btn-disabled' unless @weather_arts.next_page}" %> + <% if @weather_arts.total_pages > 1 %> +
+
+ <%= link_to_prev_page @weather_arts, "Previous", + class: "btn btn-outline #{'btn-disabled' unless @weather_arts.prev_page}" %> + <% @weather_arts.total_pages.times do |i| %> + <%= link_to i + 1, arts_path(page: i + 1, region: params[:region], sort: params[:sort]), + class: "btn btn-outline #{'btn-active' if @weather_arts.current_page == i + 1}" %> + <% end %> + <%= link_to_next_page @weather_arts, "Next", + class: "btn btn-outline #{'btn-disabled' unless @weather_arts.next_page}" %> +
-
+ <% end %>
\ No newline at end of file