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.
This commit is contained in:
songtianlun 2025-02-11 16:56:51 +08:00
parent 2511eadd2f
commit 973a0882ed
4 changed files with 50 additions and 2 deletions

View File

@ -21,4 +21,8 @@ module ApplicationHelper
}
}.to_json.html_safe if weather_art.image.attached?
end
def admin?
current_user&.admin?
end
end

View File

@ -0,0 +1,45 @@
<% if admin? %>
<div class="card bg-warning/10 border border-warning/20 shadow-lg p-6">
<div class="flex items-center gap-3 mb-4">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-warning" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
<h3 class="font-display font-bold text-lg">Admin Panel</h3>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- 统计数据 -->
<div class="space-y-4">
<h4 class="font-semibold">Statistics</h4>
<div class="stats stats-vertical lg:stats-horizontal shadow">
<div class="stat">
<div class="stat-title">Total Images</div>
<div class="stat-value"><%= @weather_art.city.weather_arts.count %></div>
</div>
<div class="stat">
<div class="stat-title">Today's Images</div>
<div class="stat-value"><%= @weather_art.city.weather_arts.where("created_at >= ?", Time.zone.now.beginning_of_day).count %></div>
</div>
</div>
</div>
<!-- 操作按钮 -->
<div class="space-y-4">
<h4 class="font-semibold">Actions</h4>
<div class="flex flex-wrap gap-4">
<%= 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" %>
</div>
</div>
</div>
</div>
<% end %>

View File

@ -7,7 +7,6 @@
<div class="relative min-h-screen bg-white"> <!-- 使用更明快的背景颜色 -->
<div class="container mx-auto px-4 pt-12 pb-16">
<div class="max-w-6xl mx-auto space-y-6">
<!-- 返回导航 -->
<div class="flex items-center">
<%= link_to city_path(@weather_art.city),

View File

@ -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".