working on aggregating results

pull/1/head
Jared Cosulich 9 years ago
parent 5d36ab0a62
commit 8c389742ee

@ -33,7 +33,7 @@ class Attempt < ApplicationRecord
if school_category.nil? if school_category.nil?
school_category = SchoolCategory.create(school: recipient.school, category: question.category) school_category = SchoolCategory.create(school: recipient.school, category: question.category)
end end
# school_category.aggregate_responses school_category.sync_aggregated_responses
end end
end end

@ -3,6 +3,9 @@ class SchoolCategory < ApplicationRecord
belongs_to :school belongs_to :school
belongs_to :category belongs_to :category
validates_associated :school
validates_associated :category
scope :for, -> (school, category) { where(school: school).where(category: category) } scope :for, -> (school, category) { where(school: school).where(category: category) }
def aggregated_responses def aggregated_responses
@ -14,9 +17,9 @@ class SchoolCategory < ApplicationRecord
select('sum(attempts.answer_index) as answer_index_total')[0] select('sum(attempts.answer_index) as answer_index_total')[0]
return { return {
attempt_count: attempt_data.attempt_count, attempt_count: attempt_data.attempt_count || 0,
response_count: attempt_data.response_count, response_count: attempt_data.response_count || 0,
answer_index_total: attempt_data.answer_index_total answer_index_total: attempt_data.answer_index_total || 0
} }
end end

@ -3,9 +3,9 @@ class CreateSchoolCategories < ActiveRecord::Migration[5.0]
create_table :school_categories do |t| create_table :school_categories do |t|
t.references :school, foreign_key: true t.references :school, foreign_key: true
t.references :category, foreign_key: true t.references :category, foreign_key: true
t.integer :attempt_count t.integer :attempt_count, default: 0
t.integer :response_count t.integer :response_count, default: 0
t.integer :answer_index_total t.integer :answer_index_total, default: 0
t.timestamps t.timestamps
end end

@ -125,11 +125,11 @@ ActiveRecord::Schema.define(version: 20170312202259) do
create_table "school_categories", force: :cascade do |t| create_table "school_categories", force: :cascade do |t|
t.integer "school_id" t.integer "school_id"
t.integer "category_id" t.integer "category_id"
t.integer "attempt_count" t.integer "attempt_count", default: 0
t.integer "response_count" t.integer "response_count", default: 0
t.integer "answer_index_total" t.integer "answer_index_total", default: 0
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["category_id"], name: "index_school_categories_on_category_id", using: :btree t.index ["category_id"], name: "index_school_categories_on_category_id", using: :btree
t.index ["school_id"], name: "index_school_categories_on_school_id", using: :btree t.index ["school_id"], name: "index_school_categories_on_school_id", using: :btree
end end

@ -202,6 +202,6 @@ namespace :data do
end end
ENV.delete('BULK_PROCESS') ENV.delete('BULK_PROCESS')
SchoolCategory.all.each { |sc| sc.aggregate_responses } SchoolCategory.all.each { |sc| sc.sync_aggregated_responses }
end end
end end

@ -9,7 +9,8 @@ RSpec.describe Attempt, type: :model do
school.recipient_lists.create!(name: 'Parents', recipient_ids: "#{recipient.id}") school.recipient_lists.create!(name: 'Parents', recipient_ids: "#{recipient.id}")
end end
let!(:question) { create_questions(1).first } let!(:category) { Category.create(name: 'Category') }
let!(:question) { create_questions(1, category).first }
let!(:question_list) do let!(:question_list) do
QuestionList.create!(name: 'Parent Questions', question_ids: "#{question.id}") QuestionList.create!(name: 'Parent Questions', question_ids: "#{question.id}")
end end
@ -35,6 +36,30 @@ RSpec.describe Attempt, type: :model do
) )
end end
describe 'after_save' do
let!(:school_categories) { SchoolCategory.for(attempt.recipient.school, attempt.question.category) }
it 'creates the associated school_category' do
expect(school_categories.count).to eq(1)
expect(school_categories.first.attempt_count).to eq(1)
expect(school_categories.first.response_count).to eq(0)
expect(school_categories.first.answer_index_total).to eq(0)
end
describe 'after_update' do
before :each do
attempt.update_attributes(answer_index: 4)
end
it 'updates associated school_categories' do
expect(school_categories.count).to eq(1)
expect(school_categories.first.attempt_count).to eq(1)
expect(school_categories.first.response_count).to eq(1)
expect(school_categories.first.answer_index_total).to eq(4)
end
end
end
describe 'send_message' do describe 'send_message' do
before :each do before :each do
Timecop.freeze Timecop.freeze

@ -9,7 +9,8 @@ RSpec.describe RecipientSchedule, type: :model do
school.recipient_lists.create!(name: 'Parents', recipient_ids: "#{recipient.id}") school.recipient_lists.create!(name: 'Parents', recipient_ids: "#{recipient.id}")
end end
let!(:questions) { create_questions(3) } let(:category) { Category.create(name: 'Category') }
let!(:questions) { create_questions(3, category) }
let!(:question_list) do let!(:question_list) do
QuestionList.create!(name: 'Parent Questions', question_ids: questions.map(&:id).join(',')) QuestionList.create!(name: 'Parent Questions', question_ids: questions.map(&:id).join(','))
end end

Loading…
Cancel
Save