From 208fe781786d6946304df6c8e02b4edbc8a8dd1c Mon Sep 17 00:00:00 2001
From: echarp <manu@echarp.org>
Date: Sun, 2 Jul 2017 13:28:10 +0200
Subject: [PATCH] Stats can be filtered using the regions selector at the top

---
 app/controllers/stats_controller.rb | 30 +++++++++++++++++++++--------
 app/views/stats/index.html.haml     | 10 +++++-----
 2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb
index e3b2d20ad..7b2dc480a 100644
--- a/app/controllers/stats_controller.rb
+++ b/app/controllers/stats_controller.rb
@@ -1,18 +1,32 @@
 # Generate statistics, around events, by date or place
 class StatsController < ApplicationController
-  before_action :set_temporal, :set_local, only: [:index]
+  has_scope :region, :locality, :tag, :daylimit, :year
+  has_scope :near, type: :hash, using: %i[location distance]
+
+  before_action :set_events, :counts, :temporal, :local, only: [:index]
 
   private
 
-  def set_temporal
-    @years = Event.group(year_grouping).count
-    @months = Event.group(year_grouping, month_grouping).count
+  def counts
+    @events_count = @events.count
+    @events_um_count = apply_scopes(Event.unmoderated).count
+    @orgas_count = apply_scopes(Orga).moderated.count
+    @orgas_um_count = apply_scopes(Orga.unmoderated).count
+  end
+
+  def temporal
+    @years = @events.group(year_grouping).count
+    @months = @events.group(year_grouping, month_grouping).count
+  end
+
+  def local
+    @regions = @events.joins(:region).group(:region_id, year_grouping).count
+    @city_events = @events.group(:city).having('count(city) > 3')
+                          .order('count(city) desc').count
   end
 
-  def set_local
-    @regions = Event.joins(:region).group(:region_id, year_grouping).count
-    @city_events = Event.group(:city).having('count(city) > 3')
-                        .order('count(city) desc').count
+  def set_events
+    @events = apply_scopes Event.moderated
   end
 
   def year_grouping
diff --git a/app/views/stats/index.html.haml b/app/views/stats/index.html.haml
index 8ceae1be1..75515bd4d 100644
--- a/app/views/stats/index.html.haml
+++ b/app/views/stats/index.html.haml
@@ -4,16 +4,16 @@
 
 %dl
   %dt= t '.allEvents'
-  %dd.quantity= number_with_delimiter Event.moderated.count
+  %dd.quantity= number_with_delimiter @events_count
 
   %dt= t '.awaitingModeration'
-  %dd.quantity= number_with_delimiter Event.unmoderated.count
+  %dd.quantity= number_with_delimiter @events_um_count
 
   %dt= t '.allOrgas'
-  %dd.quantity= number_with_delimiter Orga.moderated.count
+  %dd.quantity= number_with_delimiter @orgas_count
 
   %dt= t '.awaitingModeration'
-  %dd.quantity= number_with_delimiter Orga.unmoderated.count
+  %dd.quantity= number_with_delimiter @orgas_um_count
 
 %h3
   %em.fa.fa-calendar
@@ -68,7 +68,7 @@
         - @years.each do |year|
           %td.quantity
             - total += @regions[[region.id, year[0]]] || 0
-            = link_to root_path(year: "#{year[0]}", region: region.id) do
+            = link_to root_path(year: year[0].to_s, region: region.id) do
               = number_with_delimiter @regions[[region.id, year[0]]]
         %th.quantity.total= number_with_delimiter total
         %td.sparkline/
-- 
GitLab