From 5d36ab0a628ca66c27629ceaf77e6ddc4aea032b Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Mon, 13 Mar 2017 11:49:49 -0400 Subject: [PATCH] working on aggregating responses --- app/models/school_category.rb | 27 +++++++++++++-- spec/models/school_category_spec.rb | 54 +++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/app/models/school_category.rb b/app/models/school_category.rb index 177c4933..461c7510 100644 --- a/app/models/school_category.rb +++ b/app/models/school_category.rb @@ -20,8 +20,31 @@ class SchoolCategory < ApplicationRecord } end - def aggregate_responses - return if ENV['BULK_PROCESS'] + def chained_aggregated_responses + _aggregated_responses = aggregated_responses + + child_school_categories = category.child_categories.collect do |cc| + SchoolCategory.for(school, cc) + end.flatten + return { + attempt_count: + _aggregated_responses[:attempt_count] + + child_school_categories.inject(0) { |total, csc| total + csc.attempt_count }, + response_count: + _aggregated_responses[:response_count] + + child_school_categories.inject(0) { |total, csc| total + csc.response_count }, + answer_index_total: + _aggregated_responses[:answer_index_total] + + child_school_categories.inject(0) { |total, csc| total + csc.answer_index_total } + } + end + + def sync_aggregated_responses + return if ENV['BULK_PROCESS'] + update_attributes(chained_aggregated_responses) + if category.parent_category.present? + SchoolCategory.for(school, category.parent_category).each { |sc| sc.sync_aggregated_responses } + end end end diff --git a/spec/models/school_category_spec.rb b/spec/models/school_category_spec.rb index bc6d3a2d..e84a4e71 100644 --- a/spec/models/school_category_spec.rb +++ b/spec/models/school_category_spec.rb @@ -11,24 +11,58 @@ RSpec.describe SchoolCategory, type: :model do let!(:category1) { Category.create!(name: 'Category 1') } let!(:category2) { Category.create!(name: 'Category 2') } - let!(:questions) { create_questions(3, category1) } + let!(:category1questions) { create_questions(3, category1) } + let!(:category2questions) { create_questions(3, category2) } - let!(:attempt1) { Attempt.create(question: questions[0], recipient: school1recipients[0], answer_index: 2)} - let!(:attempt2) { Attempt.create(question: questions[0], recipient: school1recipients[1])} - let!(:attempt3) { Attempt.create(question: questions[0], recipient: school1recipients[2], answer_index: 3)} - let!(:attempt4) { Attempt.create(question: questions[0], recipient: school2recipients[0], answer_index: 4)} + let!(:attempt1) { Attempt.create!(question: category1questions[0], recipient: school1recipients[0], answer_index: 2)} + let!(:attempt2) { Attempt.create!(question: category1questions[0], recipient: school1recipients[1])} + let!(:attempt3) { Attempt.create!(question: category1questions[0], recipient: school1recipients[2], answer_index: 3)} + let!(:attempt4) { Attempt.create!(question: category1questions[0], recipient: school2recipients[0], answer_index: 4)} + let!(:attempt5) { Attempt.create!(question: category1questions[1], recipient: school1recipients[0], answer_index: 5)} + let!(:attempt6) { Attempt.create!(question: category1questions[2], recipient: school1recipients[0], answer_index: 5)} + let!(:attempt7) { Attempt.create!(question: category2questions[0], recipient: school1recipients[0], answer_index: 3)} + let!(:attempt8) { Attempt.create!(question: category2questions[1], recipient: school1recipients[1], answer_index: 1)} - let!(:school_category) { SchoolCategory.for(school1, category1).first } + let!(:school_category1) { SchoolCategory.for(school1, category1).first } describe 'aggregated_responses' do it 'should provide the count and sum of all attempts' do - expect(school_category.aggregated_responses).to eq( - attempt_count: 3, - response_count: 2, - answer_index_total: 5 + expect(school_category1.aggregated_responses).to eq( + attempt_count: 5, + response_count: 4, + answer_index_total: 15 ) end end + describe 'sync_responses' do + + let!(:category3) { Category.create!(name: 'Category 3', parent_category: category1) } + + let!(:category3questions) { create_questions(3, category3) } + + let!(:attempt7) { Attempt.create!(question: category3questions[0], recipient: school1recipients[0], answer_index: 4)} + let!(:attempt8) { Attempt.create!(question: category3questions[0], recipient: school1recipients[1], answer_index: 1)} + let!(:attempt9) { Attempt.create!(question: category3questions[0], recipient: school1recipients[2])} + let!(:attempt10) { Attempt.create!(question: category3questions[1], recipient: school1recipients[1], answer_index: 5)} + let!(:attempt11) { Attempt.create!(question: category3questions[1], recipient: school2recipients[0], answer_index: 5)} + + let!(:school_category3) { SchoolCategory.for(school1, category3).first } + + + it 'should update attributes and parent_category school_category attributes' do + school_category3.sync_aggregated_responses + + school_category3.reload + expect(school_category3.attempt_count).to eq(4) + expect(school_category3.response_count).to eq(3) + expect(school_category3.answer_index_total).to eq(10) + + school_category1.reload + expect(school_category1.attempt_count).to eq(9) + expect(school_category1.response_count).to eq(7) + expect(school_category1.answer_index_total).to eq(25) + end + end end