From df269847773b59604861a9ba86de060874f5ae61 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 27 Dec 2025 23:03:31 +0100 Subject: [PATCH] Allow sharing digest for 1 week or 1 month --- app/models/stat.rb | 4 ++- app/models/users/digest.rb | 6 +++-- app/services/users/digests/calculate_year.rb | 6 ++--- app/views/shared/_sharing_modal.html.erb | 4 ++- app/views/users/digests/index.html.erb | 5 ++-- app/views/users/digests/public_year.html.erb | 2 +- app/views/users/digests/show.html.erb | 8 +++--- .../digests_mailer/year_end_digest.html.erb | 12 +++++++++ .../digests_mailer/year_end_digest.text.erb | 6 ++--- ...93242_change_digests_distance_to_bigint.rb | 16 ------------ db/schema.rb | 4 +-- spec/factories/users/digests.rb | 26 +++++++++---------- .../users/digests/calculate_year_spec.rb | 8 +++--- 13 files changed, 56 insertions(+), 51 deletions(-) delete mode 100644 db/migrate/20251227193242_change_digests_distance_to_bigint.rb diff --git a/app/models/stat.rb b/app/models/stat.rb index 1bcb2cbf..4b0e6394 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -68,12 +68,14 @@ class Stat < ApplicationRecord def enable_sharing!(expiration: '1h') # Default to 24h if an invalid expiration is provided - expiration = '24h' unless %w[1h 12h 24h].include?(expiration) + expiration = '24h' unless %w[1h 12h 24h 1w 1m].include?(expiration) expires_at = case expiration when '1h' then 1.hour.from_now when '12h' then 12.hours.from_now when '24h' then 24.hours.from_now + when '1w' then 1.week.from_now + when '1m' then 1.month.from_now end update!( diff --git a/app/models/users/digest.rb b/app/models/users/digest.rb index 0c65a753..843aa115 100644 --- a/app/models/users/digest.rb +++ b/app/models/users/digest.rb @@ -46,12 +46,14 @@ class Users::Digest < ApplicationRecord end def enable_sharing!(expiration: '24h') - expiration = '24h' unless %w[1h 12h 24h].include?(expiration) + expiration = '24h' unless %w[1h 12h 24h 1w 1m].include?(expiration) expires_at = case expiration when '1h' then 1.hour.from_now when '12h' then 12.hours.from_now when '24h' then 24.hours.from_now + when '1w' then 1.week.from_now + when '1m' then 1.month.from_now end update!( @@ -127,7 +129,7 @@ class Users::Digest < ApplicationRecord end def total_distance_all_time - all_time_stats['total_distance'] || 0 + (all_time_stats['total_distance'] || 0).to_i end def distance_km diff --git a/app/services/users/digests/calculate_year.rb b/app/services/users/digests/calculate_year.rb index f278c6a6..faea7d50 100644 --- a/app/services/users/digests/calculate_year.rb +++ b/app/services/users/digests/calculate_year.rb @@ -76,12 +76,12 @@ module Users result = {} monthly_stats.each do |stat| - result[stat.month.to_s] = stat.distance + result[stat.month.to_s] = stat.distance.to_s end # Fill in missing months with 0 (1..12).each do |month| - result[month.to_s] ||= 0 + result[month.to_s] ||= '0' end result @@ -131,7 +131,7 @@ module Users { 'total_countries' => user.countries_visited.count, 'total_cities' => user.cities_visited.count, - 'total_distance' => user.stats.sum(:distance) + 'total_distance' => user.stats.sum(:distance).to_s } end end diff --git a/app/views/shared/_sharing_modal.html.erb b/app/views/shared/_sharing_modal.html.erb index 926719cb..f994558b 100644 --- a/app/views/shared/_sharing_modal.html.erb +++ b/app/views/shared/_sharing_modal.html.erb @@ -43,7 +43,9 @@ <%= options_for_select([ ['1 hour', '1h'], ['12 hours', '12h'], - ['24 hours', '24h'] + ['24 hours', '24h'], + ['1 week', '1w'], + ['1 month', '1m'] ], @stat&.sharing_settings&.dig('expiration') || '1h') %> diff --git a/app/views/users/digests/index.html.erb b/app/views/users/digests/index.html.erb index 142f4eab..7b695d23 100644 --- a/app/views/users/digests/index.html.erb +++ b/app/views/users/digests/index.html.erb @@ -27,8 +27,9 @@ <% if @digests.empty? %>
-
<%= icon 'earth' %>
-

No Year-End Digests Yet

+

+ <%= icon 'earth' %>No Year-End Digests Yet +

Year-end digests are automatically generated on January 1st each year. <% if @available_years.any? && current_user.active? %> diff --git a/app/views/users/digests/public_year.html.erb b/app/views/users/digests/public_year.html.erb index 2031aef2..a2cfaec7 100644 --- a/app/views/users/digests/public_year.html.erb +++ b/app/views/users/digests/public_year.html.erb @@ -80,7 +80,7 @@

<%= column_chart( @digest.monthly_distances.sort.map { |month, distance_meters| - [Date::ABBR_MONTHNAMES[month.to_i], Users::Digest.convert_distance(distance_meters, @distance_unit).round] + [Date::ABBR_MONTHNAMES[month.to_i], Users::Digest.convert_distance(distance_meters.to_i, @distance_unit).round] }, height: '200px', suffix: " #{@distance_unit}", diff --git a/app/views/users/digests/show.html.erb b/app/views/users/digests/show.html.erb index 509efb1c..3b69657b 100644 --- a/app/views/users/digests/show.html.erb +++ b/app/views/users/digests/show.html.erb @@ -102,7 +102,7 @@
<%= column_chart( @digest.monthly_distances.sort.map { |month, distance_meters| - [Date::ABBR_MONTHNAMES[month.to_i], Users::Digest.convert_distance(distance_meters, @distance_unit).round] + [Date::ABBR_MONTHNAMES[month.to_i], Users::Digest.convert_distance(distance_meters.to_i, @distance_unit).round] }, height: '250px', suffix: " #{@distance_unit}", @@ -262,8 +262,10 @@ <%= options_for_select([ ['1 hour', '1h'], ['12 hours', '12h'], - ['24 hours', '24h'] - ], @digest&.sharing_settings&.dig('expiration') || '1h') %> + ['24 hours', '24h'], + ['1 week', '1w'], + ['1 month', '1m'] + ], @digest&.sharing_settings&.dig('expiration') || '24h') %>
diff --git a/app/views/users/digests_mailer/year_end_digest.html.erb b/app/views/users/digests_mailer/year_end_digest.html.erb index 04163726..65959397 100644 --- a/app/views/users/digests_mailer/year_end_digest.html.erb +++ b/app/views/users/digests_mailer/year_end_digest.html.erb @@ -166,6 +166,12 @@

Your journey, by the numbers

+
+

+ Hi, this is Evgenii from Dawarich! Pretty wild journey last yeah, huh? Let's take a look back at all the places you explored in <%= @digest.year %>. +

+
+
@@ -276,6 +282,12 @@
+
+

+ You can open your digest for sharing on its page on Dawarich: <%= users_digest_url(year: @digest.year) %> +

+
+