From 3a6d247451a62d3924b6cb3467713031bc1f8fe8 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Thu, 23 Jan 2025 14:10:13 +0800 Subject: [PATCH] feat: add pagination to weather arts gallery - Introduce ArtsController with index action - Create index view for displaying weather arts - Implement Kaminari for pagination functionality - Add necessary routes for accessing arts - Update Gemfile to include Kaminari gem - Create views for pagination controls This implementation enhances the Weather Arts Gallery, allowing users to view and navigate through a collection of AI-generated weather arts easily. Pagination controls have been added to improve usability. --- Gemfile | 2 + Gemfile.lock | 1 + app/controllers/arts_controller.rb | 8 +++ app/helpers/arts_helper.rb | 2 + app/models/city.rb | 2 + app/views/arts/index.html.erb | 80 ++++++++++++++++++++++++ app/views/home/index.html.erb | 8 +++ app/views/kaminari/_first_page.html.erb | 11 ++++ app/views/kaminari/_gap.html.erb | 8 +++ app/views/kaminari/_last_page.html.erb | 11 ++++ app/views/kaminari/_next_page.html.erb | 11 ++++ app/views/kaminari/_page.html.erb | 12 ++++ app/views/kaminari/_paginator.html.erb | 25 ++++++++ app/views/kaminari/_prev_page.html.erb | 11 ++++ app/views/layouts/application.html.erb | 1 + config/initializers/kaminari_config.rb | 14 +++++ config/routes.rb | 1 + test/controllers/arts_controller_test.rb | 7 +++ 18 files changed, 215 insertions(+) create mode 100644 app/controllers/arts_controller.rb create mode 100644 app/helpers/arts_helper.rb create mode 100644 app/views/arts/index.html.erb create mode 100644 app/views/kaminari/_first_page.html.erb create mode 100644 app/views/kaminari/_gap.html.erb create mode 100644 app/views/kaminari/_last_page.html.erb create mode 100644 app/views/kaminari/_next_page.html.erb create mode 100644 app/views/kaminari/_page.html.erb create mode 100644 app/views/kaminari/_paginator.html.erb create mode 100644 app/views/kaminari/_prev_page.html.erb create mode 100644 config/initializers/kaminari_config.rb create mode 100644 test/controllers/arts_controller_test.rb diff --git a/Gemfile b/Gemfile index 2d351f5..c36ff7d 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,8 @@ gem "devise", "~> 4.9" gem "activeadmin", "~> 3.2" gem "friendly_id", "~> 5.5" +gem 'kaminari', '~> 1.2' + # gem "whenever", "~> 1.0" gem "ruby-openai", "~> 7.3" gem "httparty", "~> 0.22.0" diff --git a/Gemfile.lock b/Gemfile.lock index b391945..52401fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -494,6 +494,7 @@ DEPENDENCIES jbuilder jsbundling-rails kamal + kaminari (~> 1.2) pg (~> 1.5) propshaft puma (>= 5.0) diff --git a/app/controllers/arts_controller.rb b/app/controllers/arts_controller.rb new file mode 100644 index 0000000..07fabb9 --- /dev/null +++ b/app/controllers/arts_controller.rb @@ -0,0 +1,8 @@ +class ArtsController < ApplicationController + def index + @weather_arts = WeatherArt.includes(:city, city: [ :country, { country: :region } ]) + .order(created_at: :desc) + .page(params[:page]) + .per(2) + end +end diff --git a/app/helpers/arts_helper.rb b/app/helpers/arts_helper.rb new file mode 100644 index 0000000..d80f33d --- /dev/null +++ b/app/helpers/arts_helper.rb @@ -0,0 +1,2 @@ +module ArtsHelper +end diff --git a/app/models/city.rb b/app/models/city.rb index 86d8bf6..23862ea 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -5,6 +5,8 @@ class City < ApplicationRecord has_many :weather_arts, dependent: :destroy + delegate :region, to: :country + validates :name, presence: true validates :latitude, presence: true validates :longitude, presence: true diff --git a/app/views/arts/index.html.erb b/app/views/arts/index.html.erb new file mode 100644 index 0000000..c9eb43b --- /dev/null +++ b/app/views/arts/index.html.erb @@ -0,0 +1,80 @@ + +
+ +
+
+

+ Weather Arts Gallery +

+

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

+
+
+ +
+
+ <% @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 %> +

+
+ <% end %> +
+ + +
+
+
+

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

+

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

+
+
+
<%= art.temperature %>°C
+
+ <%= art.weather_date.strftime("%b %d") %> +
+
+
+ + <%= link_to city_weather_art_path(art.city, art), + class: "btn btn-ghost btn-sm w-full mt-3" do %> + View Details + <% end %> +
+
+ <% end %> +
+ + +
+
+ <%= 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}" %> +
+
+
+
\ No newline at end of file diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 7d97af9..5072bb7 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -57,4 +57,12 @@ <% end %> + +
+ <%= link_to arts_path, class: "btn btn-primary btn-lg gap-2" do %> + View All Weather Arts + + + + <% end %>
\ No newline at end of file diff --git a/app/views/kaminari/_first_page.html.erb b/app/views/kaminari/_first_page.html.erb new file mode 100644 index 0000000..0cc83b9 --- /dev/null +++ b/app/views/kaminari/_first_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "First" page + - available local variables + url: url to the first page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.first?, t('views.pagination.first').html_safe, url, remote: remote %> + diff --git a/app/views/kaminari/_gap.html.erb b/app/views/kaminari/_gap.html.erb new file mode 100644 index 0000000..bbb0f98 --- /dev/null +++ b/app/views/kaminari/_gap.html.erb @@ -0,0 +1,8 @@ +<%# Non-link tag that stands for skipped pages... + - available local variables + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> +<%= t('views.pagination.truncate').html_safe %> diff --git a/app/views/kaminari/_last_page.html.erb b/app/views/kaminari/_last_page.html.erb new file mode 100644 index 0000000..bc777b4 --- /dev/null +++ b/app/views/kaminari/_last_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "Last" page + - available local variables + url: url to the last page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.last?, t('views.pagination.last').html_safe, url, remote: remote %> + diff --git a/app/views/kaminari/_next_page.html.erb b/app/views/kaminari/_next_page.html.erb new file mode 100644 index 0000000..3b0d054 --- /dev/null +++ b/app/views/kaminari/_next_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "Next" page + - available local variables + url: url to the next page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.last?, t('views.pagination.next').html_safe, url, rel: 'next', remote: remote %> + diff --git a/app/views/kaminari/_page.html.erb b/app/views/kaminari/_page.html.erb new file mode 100644 index 0000000..393bfc4 --- /dev/null +++ b/app/views/kaminari/_page.html.erb @@ -0,0 +1,12 @@ +<%# Link showing page number + - available local variables + page: a page object for "this" page + url: url to this page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless page.current?, page, url, {remote: remote, rel: page.rel} %> + diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb new file mode 100644 index 0000000..5525f48 --- /dev/null +++ b/app/views/kaminari/_paginator.html.erb @@ -0,0 +1,25 @@ +<%# The container tag + - available local variables + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote + paginator: the paginator that renders the pagination tags inside +-%> +<%= paginator.render do -%> + +<% end -%> diff --git a/app/views/kaminari/_prev_page.html.erb b/app/views/kaminari/_prev_page.html.erb new file mode 100644 index 0000000..0f32af4 --- /dev/null +++ b/app/views/kaminari/_prev_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "Previous" page + - available local variables + url: url to the previous page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.first?, t('views.pagination.previous').html_safe, url, rel: 'prev', remote: remote %> + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a527769..fe02d99 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -33,6 +33,7 @@
<%= link_to "Cities", cities_path, class: "btn btn-ghost font-sans" %> + <%= link_to "Arts", arts_path, class: "btn btn-ghost font-sans" %>
diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 0000000..4ba6ee3 --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +Kaminari.configure do |config| + # config.default_per_page = 25 + # config.max_per_page = nil + # config.window = 4 + # config.outer_window = 0 + # config.left = 0 + # config.right = 0 + # config.page_method_name = :page + # config.param_name = :page + # config.max_pages = nil + # config.params_on_first_page = false +end diff --git a/config/routes.rb b/config/routes.rb index 677e9dc..0bdf657 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ Rails.application.routes.draw do resources :cities, only: [ :index, :show ] do resources :weather_arts, path: "weather", only: [ :show ], param: :slug end + resources :arts, only: [:index] # namespace :admin do # resources :cities diff --git a/test/controllers/arts_controller_test.rb b/test/controllers/arts_controller_test.rb new file mode 100644 index 0000000..d83c287 --- /dev/null +++ b/test/controllers/arts_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ArtsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end