From 973a0882ed0004a8ee75ce4e2fe3ca7831f86b80 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 11 Feb 2025 16:56:51 +0800 Subject: [PATCH] feat: add admin panel and update database URL - Implemented a new admin panel that allows administrators to view statistics and manage weather art. The panel includes: - A card displaying total images and today's images - Buttons to generate new art and edit the city - Added `admin?` helper method to check if the current user is an administrator. - Updated database configuration to use safer credentials lookup method `dig` for development database URL instead of the previous method, improving reliability in accessing nested credentials. --- app/helpers/application_helper.rb | 4 +++ app/views/cities/_admin_panel.html.erb | 45 ++++++++++++++++++++++++++ app/views/weather_arts/show.html.erb | 1 - config/database.yml | 2 +- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 app/views/cities/_admin_panel.html.erb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2b5c010..045b7e0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -21,4 +21,8 @@ module ApplicationHelper } }.to_json.html_safe if weather_art.image.attached? end + + def admin? + current_user&.admin? + end end diff --git a/app/views/cities/_admin_panel.html.erb b/app/views/cities/_admin_panel.html.erb new file mode 100644 index 0000000..bcc918c --- /dev/null +++ b/app/views/cities/_admin_panel.html.erb @@ -0,0 +1,45 @@ +<% if admin? %> +
+
+ + + +

Admin Panel

+
+ +
+ +
+

Statistics

+
+
+
Total Images
+
<%= @weather_art.city.weather_arts.count %>
+
+ +
+
Today's Images
+
<%= @weather_art.city.weather_arts.where("created_at >= ?", Time.zone.now.beginning_of_day).count %>
+
+
+
+ + +
+

Actions

+
+ <%= button_to "Generate New Art", "#", + method: :post, + data: { + controller: "generate-art", + action: "generate-art#generate", + city_id: @weather_art.city.id + }, + class: "btn btn-primary" %> + + <%= link_to "Edit City", edit_city_path(@weather_art.city), class: "btn btn-secondary" %> +
+
+
+
+<% end %> \ No newline at end of file diff --git a/app/views/weather_arts/show.html.erb b/app/views/weather_arts/show.html.erb index 78990ad..b3261df 100644 --- a/app/views/weather_arts/show.html.erb +++ b/app/views/weather_arts/show.html.erb @@ -7,7 +7,6 @@
-
<%= link_to city_path(@weather_art.city), diff --git a/config/database.yml b/config/database.yml index 5dd2ac0..8031255 100644 --- a/config/database.yml +++ b/config/database.yml @@ -12,7 +12,7 @@ default: &default development: <<: *default # database: storage/development.sqlite3 - url: <%= Rails.application.credentials.db_dev.url %> + url: <%= Rails.application.credentials.dig(:db_dev, :url) %> # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake".