Add a date_attribute argument to StatsController#cumulative_hash

This commit is contained in:
gregoirenovel 2017-04-26 14:28:39 +02:00
parent dbf891b333
commit 06714b5fc3
2 changed files with 27 additions and 8 deletions

View file

@ -42,10 +42,10 @@ class StatsController < ApplicationController
h
end
def cumulative_hash(association)
def cumulative_hash(association, date_attribute = :created_at)
sum = 0
association
.group("DATE_TRUNC('month', created_at)")
.group("DATE_TRUNC('month', #{date_attribute.to_s})")
.count
.to_a
.sort{ |x, y| x[0] <=> y[0] }

View file

@ -50,19 +50,38 @@ 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
}) }
end
end
context "with 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)
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 }
subject { StatsController.new.send(:cumulative_hash, association) }
subject { StatsController.new.send(:cumulative_hash, association, :updated_at) }
it { expect(subject).to eq({
45.days.ago.beginning_of_month => 1,
15.days.ago.beginning_of_month => 3
20.days.ago.beginning_of_month => 2,
10.days.ago.beginning_of_month => 3
}) }
end
end