working on aggregating responses

pull/1/head
Jared Cosulich 9 years ago
parent 48eb55ad94
commit 5d36ab0a62

@ -20,8 +20,31 @@ class SchoolCategory < ApplicationRecord
} }
end end
def aggregate_responses def chained_aggregated_responses
return if ENV['BULK_PROCESS'] _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
end end

@ -11,24 +11,58 @@ RSpec.describe SchoolCategory, type: :model do
let!(:category1) { Category.create!(name: 'Category 1') } let!(:category1) { Category.create!(name: 'Category 1') }
let!(:category2) { Category.create!(name: 'Category 2') } 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!(:attempt1) { Attempt.create!(question: category1questions[0], recipient: school1recipients[0], answer_index: 2)}
let!(:attempt2) { Attempt.create(question: questions[0], recipient: school1recipients[1])} let!(:attempt2) { Attempt.create!(question: category1questions[0], recipient: school1recipients[1])}
let!(:attempt3) { Attempt.create(question: questions[0], recipient: school1recipients[2], answer_index: 3)} let!(:attempt3) { Attempt.create!(question: category1questions[0], recipient: school1recipients[2], answer_index: 3)}
let!(:attempt4) { Attempt.create(question: questions[0], recipient: school2recipients[0], answer_index: 4)} 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 describe 'aggregated_responses' do
it 'should provide the count and sum of all attempts' do it 'should provide the count and sum of all attempts' do
expect(school_category.aggregated_responses).to eq( expect(school_category1.aggregated_responses).to eq(
attempt_count: 3, attempt_count: 5,
response_count: 2, response_count: 4,
answer_index_total: 5 answer_index_total: 15
) )
end end
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 end

Loading…
Cancel
Save