From 6c18fbe41f3e326b1aff44911c5039df88631579 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 20 Dec 2024 15:26:25 +0100 Subject: [PATCH] Add buttons to update stats for a year or a month --- .app_version | 2 +- CHANGELOG.md | 8 ++++++++ app/controllers/stats_controller.rb | 16 ++++++++++++++++ app/jobs/stats/calculating_job.rb | 4 ++-- app/services/stats/calculate_month.rb | 4 ++-- app/views/notifications/index.html.erb | 2 +- app/views/stats/_stat.html.erb | 16 +++++++++++----- app/views/stats/index.html.erb | 13 ++++++++----- config/routes.rb | 6 +++++- 9 files changed, 54 insertions(+), 17 deletions(-) diff --git a/.app_version b/.app_version index 727d97b9..144996ed 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.20.2 +0.20.3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 99c1a9d8..bdd8fea7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.20.3 - 2024-12-20 + +### Added + +- A button on a year stats card to update stats for the whole year. +- A button on a month stats card to update stats for a specific month. +- A confirmation alert on the Notifications page before deleting all notifications. + # 0.20.2 - 2024-12-17 ### Added diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 2305a44e..b7e68f41 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -16,6 +16,22 @@ class StatsController < ApplicationController end def update + if params[:month] == 'all' + (1..12).each do |month| + Stats::CalculatingJob.perform_later(current_user.id, params[:year], month) + end + + target = "the whole #{params[:year]}" + else + Stats::CalculatingJob.perform_later(current_user.id, params[:year], params[:month]) + + target = "#{Date::MONTHNAMES[params[:month].to_i]} of #{params[:year]}" + end + + redirect_to stats_path, notice: "Stats for #{target} are being updated", status: :see_other + end + + def update_all current_user.years_tracked.each do |year| year[:months].each do |month| Stats::CalculatingJob.perform_later( diff --git a/app/jobs/stats/calculating_job.rb b/app/jobs/stats/calculating_job.rb index 02da4d5e..ac28ccf6 100644 --- a/app/jobs/stats/calculating_job.rb +++ b/app/jobs/stats/calculating_job.rb @@ -19,8 +19,8 @@ class Stats::CalculatingJob < ApplicationJob Notifications::Create.new( user:, kind: :info, - title: "Stats updated: #{year}-#{month}", - content: "Stats updated for #{year}-#{month}" + title: "Stats updated for #{Date::MONTHNAMES[month.to_i]} of #{year}", + content: "Stats updated for #{Date::MONTHNAMES[month.to_i]} of #{year}" ).call end diff --git a/app/services/stats/calculate_month.rb b/app/services/stats/calculate_month.rb index b99b2603..324cc3a7 100644 --- a/app/services/stats/calculate_month.rb +++ b/app/services/stats/calculate_month.rb @@ -3,8 +3,8 @@ class Stats::CalculateMonth def initialize(user_id, year, month) @user = User.find(user_id) - @year = year - @month = month + @year = year.to_i + @month = month.to_i end def call diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 09e760fb..43b2ff51 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -7,7 +7,7 @@ <%= link_to "Mark all as read", mark_notifications_as_read_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-primary" %>  <% end %> <% if @notifications.any? %> - <%= link_to "Delete all", delete_all_notifications_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-warning" %> + <%= link_to "Delete all", delete_all_notifications_path, method: :post, data: { turbo_method: :post, turbo_confirm: 'Are you sure you want to delete all notifications?' }, class: "btn btn-sm btn-warning" %> <% end %>
diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index 4309c3b0..f8e59e04 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -1,10 +1,16 @@
-

- <%= link_to map_url(timespan(stat.month, stat.year)), class: "underline hover:no-underline text-#{header_colors.sample}" do %> - <%= "#{Date::MONTHNAMES[stat.month]} of #{stat.year}" %> - <% end %> -

+
+

+ <%= link_to map_url(timespan(stat.month, stat.year)), class: "underline hover:no-underline text-#{header_colors.sample}" do %> + <%= Date::MONTHNAMES[stat.month] %> + <% end %> +

+ +
+ <%= link_to '[Update]', update_year_month_stats_path(stat.year, stat.month), data: { turbo_method: :put }, class: 'text-sm text-gray-500 hover:underline' %> +
+

<%= stat.distance %><%= DISTANCE_UNIT %>

<% if REVERSE_GEOCODING_ENABLED %>
diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index ee3b33cd..d67037de 100644 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -21,15 +21,18 @@ <% end %>
- <%= link_to 'Update stats', stats_path, data: { 'turbo-method' => :post }, class: 'btn btn-primary mt-5' %> + <%= link_to 'Update stats', update_all_stats_path, data: { turbo_method: :put }, class: 'btn btn-primary mt-5' %> -
+
<% @stats.each do |year, stats| %>
-

- <%= link_to year, "/stats/#{year}", class: 'underline hover:no-underline' %> - <%= link_to '[Map]', map_url(year_timespan(year)), class: 'underline hover:no-underline' %> +

+
+ <%= link_to year, "/stats/#{year}", class: 'underline hover:no-underline' %> + <%= link_to '[Map]', map_url(year_timespan(year)), class: 'underline hover:no-underline' %> +
+ <%= link_to '[Update]', update_year_month_stats_path(year, :all), data: { turbo_method: :put }, class: 'text-sm text-gray-500 hover:underline' %>

<% cache [current_user, 'year_distance_stat', year], skip_digest: true do %> diff --git a/config/routes.rb b/config/routes.rb index 4478d7db..d8926bd0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,10 +41,14 @@ Rails.application.routes.draw do post 'notifications/destroy_all', to: 'notifications#destroy_all', as: :delete_all_notifications resources :stats, only: :index do collection do - post :update + put :update_all end end get 'stats/:year', to: 'stats#show', constraints: { year: /\d{4}/ } + put 'stats/:year/:month/update', + to: 'stats#update', + as: :update_year_month_stats, + constraints: { year: /\d{4}/, month: /\d{1,2}|all/ } root to: 'home#index' devise_for :users, skip: [:registrations]