From 6f2c63c0944dafa2c3f035c6f2d357a18cf3f247 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 17 Jul 2017 15:12:05 +0200 Subject: [PATCH] =?UTF-8?q?Remove=20default=20values=20for=20some=20stat?= =?UTF-8?q?=20methods=E2=80=99=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/stats_controller.rb | 4 +- spec/controllers/stats_controller_spec.rb | 78 ++++++----------------- 2 files changed, 20 insertions(+), 62 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 8760d0f82..109163dc9 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -36,7 +36,7 @@ class StatsController < ApplicationController private - def last_four_months_hash(association, date_attribute = :created_at) + def last_four_months_hash(association, date_attribute) min_date = 3.months.ago.beginning_of_month.to_date if administration_signed_in? max_date = Time.now.to_date @@ -53,7 +53,7 @@ class StatsController < ApplicationController .map { |e| [I18n.l(e.first, format: "%B %Y"), e.last] } end - def cumulative_hash(association, date_attribute = :created_at) + def cumulative_hash(association, date_attribute) sum = 0 association .group("DATE_TRUNC('month', #{date_attribute.to_s})") diff --git a/spec/controllers/stats_controller_spec.rb b/spec/controllers/stats_controller_spec.rb index 4891e9ef1..f2fe3c36b 100644 --- a/spec/controllers/stats_controller_spec.rb +++ b/spec/controllers/stats_controller_spec.rb @@ -2,35 +2,13 @@ require 'spec_helper' describe StatsController, type: :controller do describe "#last_four_months_hash" do - context "without a date attribute" do - before do - FactoryGirl.create(:procedure, :created_at => 6.months.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 { @controller.send(:last_four_months_hash, association) } - - it { expect(subject).to eq([ - [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 "with a date attribute" do + context "while a regular user is logged in" 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 => 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) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 1.day.ago) @controller = StatsController.new allow(@controller).to receive(:administration_signed_in?).and_return(false) @@ -49,10 +27,10 @@ describe StatsController, type: :controller do 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) + FactoryGirl.create(:procedure, :updated_at => 6.months.ago) + FactoryGirl.create(:procedure, :updated_at => 45.days.ago) + FactoryGirl.create(:procedure, :updated_at => 1.day.ago) + FactoryGirl.create(:procedure, :updated_at => 1.day.ago) @controller = StatsController.new @@ -61,7 +39,7 @@ describe StatsController, type: :controller do let (:association) { Procedure.all } - subject { @controller.send(:last_four_months_hash, association) } + 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"), 1], @@ -72,41 +50,21 @@ describe StatsController, type: :controller do end describe '#cumulative_hash' do - context "without a date attribute" do - before do - FactoryGirl.create(:procedure, :created_at => 45.days.ago) - FactoryGirl.create(:procedure, :created_at => 15.days.ago) - FactoryGirl.create(:procedure, :created_at => 15.days.ago) - end - - let (:association) { Procedure.all } - - subject { StatsController.new.send(:cumulative_hash, association) } - - it { expect(subject).to eq({ - 45.days.ago.beginning_of_month => 1, - 15.days.ago.beginning_of_month => 3 - }) - } + before do + FactoryGirl.create(:procedure, :created_at => 45.days.ago, :updated_at => 20.days.ago) + FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 20.days.ago) + FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 10.days.ago) end - context "with a date attribute" do - before do - FactoryGirl.create(:procedure, :created_at => 45.days.ago, :updated_at => 20.days.ago) - FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 20.days.ago) - FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 10.days.ago) - end + let (:association) { Procedure.all } - let (:association) { Procedure.all } + subject { StatsController.new.send(:cumulative_hash, association, :updated_at) } - subject { StatsController.new.send(:cumulative_hash, association, :updated_at) } - - it { expect(subject).to eq({ - 20.days.ago.beginning_of_month => 2, - 10.days.ago.beginning_of_month => 3 - }) - } - end + it { expect(subject).to eq({ + 20.days.ago.beginning_of_month => 2, + 10.days.ago.beginning_of_month => 3 + }) + } end describe "#procedures_count_per_administrateur" do