Merge pull request #482 from sgmap/fix-440

[Fix #440] Show the current month only if logged in as superadmin
This commit is contained in:
gregoirenovel 2017-06-26 10:51:00 +02:00 committed by GitHub
commit a0e0cf4e40
2 changed files with 47 additions and 13 deletions

View file

@ -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)

View file

@ -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