diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index f50905bf..9e0292d5 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -5,6 +5,11 @@ class StatsController < ApplicationController @stats = current_user.stats.group_by(&:year).sort_by { _1 }.reverse end + def show + @year = params[:year].to_i + @stats = current_user.stats.where(year: @year) + end + def update StatCreatingJob.perform_later(current_user.id) diff --git a/app/models/stat.rb b/app/models/stat.rb index 81a2f504..e7b618f1 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -30,4 +30,10 @@ class Stat < ApplicationRecord [data[:day], data[:distance].round(2)] end end + + def self.year_distance(year) + stats = where(year: year).order(:month) + + stats.map { |stat| [Date::MONTHNAMES[stat.month], stat.distance] } + end end diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index 0a1bdc20..7ea5ab99 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -10,13 +10,13 @@
<%= stat.toponyms.count %> countries, <%= stat.toponyms.sum { _1['cities'].count } %> cities
- <%= column_chart( - stat.daily_distance, - height: '100px', - suffix: ' km', - xtitle: 'Days', - ytitle: 'Distance' - ) %> <% end %> + <%= column_chart( + stat.daily_distance, + height: '100px', + suffix: ' km', + xtitle: 'Days', + ytitle: 'Distance' + ) %> diff --git a/app/views/stats/_year.html.erb b/app/views/stats/_year.html.erb new file mode 100644 index 00000000..79657bb9 --- /dev/null +++ b/app/views/stats/_year.html.erb @@ -0,0 +1,18 @@ +

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

+
+ <%= column_chart( + Stat.year_distance(year), + height: '200px', + suffix: ' km', + xtitle: 'Days', + ytitle: 'Distance' + ) %> +
+
+ <% stats.each do |stat| %> + <%= render stat %> + <% end %> +
diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index 9dc41fbf..ae4b78c3 100644 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -41,15 +41,6 @@ <%= link_to 'Update stats', stats_path, method: :post, class: 'btn btn-primary mt-5' %> <% @stats.each do |year, stats| %> -

- <%= link_to points_url(year_timespan(year)), class: 'underline hover:no-underline' do %> - <%= year %> - <% end %> -

-
- <% stats.each do |stat| %> - <%= render stat %> - <% end %> -
+ <%= render partial: 'stats/year', locals: { year: year, stats: stats } %> <% end %> diff --git a/app/views/stats/show.html.erb b/app/views/stats/show.html.erb new file mode 100644 index 00000000..d7c98de5 --- /dev/null +++ b/app/views/stats/show.html.erb @@ -0,0 +1,3 @@ +
+ <%= render partial: 'stats/year', locals: { year: @year, stats: @stats } %> +
diff --git a/config/routes.rb b/config/routes.rb index b7c94779..66a9380b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ Rails.application.routes.draw do post :update end end + get 'stats/:year', to: 'stats#show', constraints: { year: /\d{4}/ } root to: 'home#index' devise_for :users