parent
f5b473ed28
commit
48eb55ad94
@ -0,0 +1,27 @@
|
|||||||
|
class SchoolCategory < ApplicationRecord
|
||||||
|
|
||||||
|
belongs_to :school
|
||||||
|
belongs_to :category
|
||||||
|
|
||||||
|
scope :for, -> (school, category) { where(school: school).where(category: category) }
|
||||||
|
|
||||||
|
def aggregated_responses
|
||||||
|
attempt_data = Attempt.
|
||||||
|
for_category(category).
|
||||||
|
for_school(school).
|
||||||
|
select('count(attempts.id) as attempt_count').
|
||||||
|
select('count(attempts.answer_index) as response_count').
|
||||||
|
select('sum(attempts.answer_index) as answer_index_total')[0]
|
||||||
|
|
||||||
|
return {
|
||||||
|
attempt_count: attempt_data.attempt_count,
|
||||||
|
response_count: attempt_data.response_count,
|
||||||
|
answer_index_total: attempt_data.answer_index_total
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def aggregate_responses
|
||||||
|
return if ENV['BULK_PROCESS']
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
class CreateSchoolCategories < ActiveRecord::Migration[5.0]
|
||||||
|
def change
|
||||||
|
create_table :school_categories do |t|
|
||||||
|
t.references :school, foreign_key: true
|
||||||
|
t.references :category, foreign_key: true
|
||||||
|
t.integer :attempt_count
|
||||||
|
t.integer :response_count
|
||||||
|
t.integer :answer_index_total
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe SchoolCategory, type: :model do
|
||||||
|
|
||||||
|
let!(:school1) { School.create!(name: 'School 1') }
|
||||||
|
let!(:school2) { School.create!(name: 'School 2') }
|
||||||
|
|
||||||
|
let!(:school1recipients) { create_recipients(school1, 4) }
|
||||||
|
let!(:school2recipients) { create_recipients(school2, 4) }
|
||||||
|
|
||||||
|
let!(:category1) { Category.create!(name: 'Category 1') }
|
||||||
|
let!(:category2) { Category.create!(name: 'Category 2') }
|
||||||
|
|
||||||
|
let!(:questions) { create_questions(3, category1) }
|
||||||
|
|
||||||
|
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!(:school_category) { 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
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
Loading…
Reference in new issue