Add StatsController#thirty_days_flow_hash

This commit is contained in:
gregoirenovel 2017-04-03 16:26:05 +02:00
parent e192038045
commit 2074ac93ba
2 changed files with 39 additions and 4 deletions

View file

@ -1,15 +1,24 @@
class StatsController < ApplicationController
def index
procedures = Procedure.where(:created_at => 30.days.ago..Time.now).group("date_trunc('day', created_at)").count
dossiers = Dossier.where(:created_at => 30.days.ago..Time.now).group("date_trunc('day', created_at)").count
procedures = Procedure
dossiers = Dossier
@procedures_30_days_flow = clean_hash(procedures)
@dossiers_30_days_flow = clean_hash(dossiers)
@procedures_30_days_flow = thirty_days_flow_hash(procedures)
@dossiers_30_days_flow = thirty_days_flow_hash(dossiers)
end
private
def thirty_days_flow_hash(association)
thirty_days_flow_hash = association
.where(:created_at => 30.days.ago..Time.now)
.group("date_trunc('day', created_at)")
.count
clean_hash(thirty_days_flow_hash)
end
def clean_hash h
h.keys.each{ |key| h[key.to_date] = h[key]; h.delete(key) }
min_date = h.keys.min

View file

@ -0,0 +1,26 @@
require 'spec_helper'
describe StatsController, type: :controller do
describe '#thirty_days_flow_hash' do
before do
FactoryGirl.create(:procedure, :created_at => 45.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
FactoryGirl.create(:procedure, :created_at => 1.day.ago)
@expected_hash = {}
(15.days.ago.to_date..1.day.ago.to_date).each do |day|
if [15.days.ago.to_date, 1.day.ago.to_date].include?(day)
@expected_hash[day] = 1
else
@expected_hash[day] = 0
end
end
end
let (:association) { Procedure.all }
subject { StatsController.new.send(:thirty_days_flow_hash, association) }
it { expect(subject).to eq(@expected_hash) }
end
end