Merge pull request #797 from sgmap/fix-stats-if-not-super-admin

Fix stats if not super admin
This commit is contained in:
gregoirenovel 2017-10-17 13:00:53 +02:00 committed by GitHub
commit 0ca4d61803
2 changed files with 38 additions and 12 deletions

View file

@ -36,13 +36,16 @@ class StatsController < ApplicationController
private
def max_date
if administration_signed_in?
Time.now.to_date
else
Time.now.beginning_of_month - 1.second
end
end
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
else
max_date = Time.now.beginning_of_month - 1.second
end
association
.where(date_attribute => min_date..max_date)
@ -56,6 +59,7 @@ class StatsController < ApplicationController
def cumulative_hash(association, date_attribute)
sum = 0
association
.where("#{date_attribute.to_s} < ?", max_date)
.group("DATE_TRUNC('month', #{date_attribute.to_s})")
.count
.to_a

View file

@ -51,20 +51,42 @@ describe StatsController, type: :controller do
describe '#cumulative_hash' do
before do
Timecop.freeze(Time.new(2016, 10, 2))
FactoryGirl.create(:procedure, :created_at => 55.days.ago, :updated_at => 43.days.ago)
FactoryGirl.create(:procedure, :created_at => 45.days.ago, :updated_at => 40.days.ago)
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)
FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 1.hour.ago)
end
let (:association) { Procedure.all }
subject { StatsController.new.send(:cumulative_hash, association, :updated_at) }
context "while a super admin is logged in" do
before { allow(@controller).to receive(:administration_signed_in?).and_return(true) }
it { expect(subject).to eq({
20.days.ago.beginning_of_month => 2,
10.days.ago.beginning_of_month => 3
})
}
subject { @controller.send(:cumulative_hash, association, :updated_at) }
it { expect(subject).to eq({
2.month.ago.beginning_of_month => 2,
1.month.ago.beginning_of_month => 4,
1.hour.ago.beginning_of_month => 5
})
}
end
context "while a super admin is not logged in" do
before { allow(@controller).to receive(:administration_signed_in?).and_return(false) }
subject { @controller.send(:cumulative_hash, association, :updated_at) }
it { expect(subject).to eq({
2.month.ago.beginning_of_month => 2,
1.month.ago.beginning_of_month => 4
})
}
end
after { Timecop.return }
end
describe "#procedures_count_per_administrateur" do