Add a date_attribute argument to StatsController#cumulative_hash
This commit is contained in:
parent
dbf891b333
commit
06714b5fc3
2 changed files with 27 additions and 8 deletions
|
@ -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] }
|
||||
|
|
|
@ -50,6 +50,7 @@ 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)
|
||||
|
@ -66,3 +67,21 @@ describe StatsController, type: :controller do
|
|||
}) }
|
||||
end
|
||||
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 }
|
||||
|
||||
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
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue