diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index b2e22e7cc..293e6b46e 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -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] } diff --git a/spec/controllers/stats_controller_spec.rb b/spec/controllers/stats_controller_spec.rb index 584a54cf4..6cbaf91bd 100644 --- a/spec/controllers/stats_controller_spec.rb +++ b/spec/controllers/stats_controller_spec.rb @@ -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