From 469f0aed4bb8730bfdb78bc5718c8b93fdd51ab1 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 22 Jun 2017 12:10:03 +0200 Subject: [PATCH] [Fix #440] Show the current month only if logged in as superadmin --- app/controllers/stats_controller.rb | 6 ++- spec/controllers/stats_controller_spec.rb | 54 ++++++++++++++++++----- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index e41dc8c74..a4ac3cbb1 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -38,7 +38,11 @@ class StatsController < ApplicationController def last_four_months_hash(association, date_attribute = :created_at) min_date = 3.months.ago.beginning_of_month.to_date - max_date = Time.now.to_date + if administration_signed_in? + max_date = Time.now.to_date + else + max_date = Time.now.beginning_of_month - 1.second + end association .where(date_attribute => min_date..max_date) diff --git a/spec/controllers/stats_controller_spec.rb b/spec/controllers/stats_controller_spec.rb index 1c28f0d01..09ce7f38c 100644 --- a/spec/controllers/stats_controller_spec.rb +++ b/spec/controllers/stats_controller_spec.rb @@ -5,18 +5,22 @@ describe StatsController, type: :controller do context "without a date attribute" do before do FactoryGirl.create(:procedure, :created_at => 6.months.ago) - FactoryGirl.create(:procedure, :created_at => 45.days.ago) - FactoryGirl.create(:procedure, :created_at => 1.days.ago) - FactoryGirl.create(:procedure, :created_at => 1.days.ago) + FactoryGirl.create(:procedure, :created_at => 62.days.ago) + FactoryGirl.create(:procedure, :created_at => 62.days.ago) + FactoryGirl.create(:procedure, :created_at => 31.days.ago) + + @controller = StatsController.new + + allow(@controller).to receive(:administration_signed_in?).and_return(false) end let (:association) { Procedure.all } - subject { StatsController.new.send(:last_four_months_hash, association) } + subject { @controller.send(:last_four_months_hash, association) } it { expect(subject).to eq([ - [I18n.l(45.days.ago.beginning_of_month, format: "%B %Y"), 1], - [I18n.l(1.days.ago.beginning_of_month, format: "%B %Y"), 2] + [I18n.l(62.days.ago.beginning_of_month, format: "%B %Y"), 2], + [I18n.l(31.days.ago.beginning_of_month, format: "%B %Y"), 1] ]) } end @@ -24,18 +28,44 @@ describe StatsController, type: :controller do context "with a date attribute" do before do FactoryGirl.create(:procedure, :created_at => 6.months.ago, :updated_at => 6.months.ago) - FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 45.days.ago) - FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 45.days.ago) - FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 1.days.ago) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 62.days.ago) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 62.days.ago) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 31.days.ago) + @controller = StatsController.new + + allow(@controller).to receive(:administration_signed_in?).and_return(false) end let (:association) { Procedure.all } - subject { StatsController.new.send(:last_four_months_hash, association, :updated_at) } + subject { @controller.send(:last_four_months_hash, association, :updated_at) } it { expect(subject).to eq([ - [I18n.l(45.days.ago.beginning_of_month, format: "%B %Y"), 2], - [I18n.l(1.days.ago.beginning_of_month, format: "%B %Y"), 1] + [I18n.l(62.days.ago.beginning_of_month, format: "%B %Y"), 2], + [I18n.l(31.days.ago.beginning_of_month, format: "%B %Y"), 1] + ]) + } + end + + context "while a super admin is logged in" do + before do + FactoryGirl.create(:procedure, :created_at => 6.months.ago) + FactoryGirl.create(:procedure, :created_at => 45.days.ago) + FactoryGirl.create(:procedure, :created_at => 1.day.ago) + FactoryGirl.create(:procedure, :created_at => 1.day.ago) + + @controller = StatsController.new + + allow(@controller).to receive(:administration_signed_in?).and_return(true) + end + + let (:association) { Procedure.all } + + subject { @controller.send(:last_four_months_hash, association) } + + it { expect(subject).to eq([ + [I18n.l(45.days.ago.beginning_of_month, format: "%B %Y"), 1], + [I18n.l(1.days.ago.beginning_of_month, format: "%B %Y"), 2] ]) } end