diff --git a/app/models/attempt.rb b/app/models/attempt.rb index 50819d4d..6cd53878 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -33,7 +33,7 @@ class Attempt < ApplicationRecord if school_category.nil? school_category = SchoolCategory.create(school: recipient.school, category: question.category) end - # school_category.aggregate_responses + school_category.sync_aggregated_responses end end diff --git a/app/models/school_category.rb b/app/models/school_category.rb index 461c7510..eb2af60f 100644 --- a/app/models/school_category.rb +++ b/app/models/school_category.rb @@ -3,6 +3,9 @@ class SchoolCategory < ApplicationRecord belongs_to :school belongs_to :category + validates_associated :school + validates_associated :category + scope :for, -> (school, category) { where(school: school).where(category: category) } def aggregated_responses @@ -14,9 +17,9 @@ class SchoolCategory < ApplicationRecord 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 + attempt_count: attempt_data.attempt_count || 0, + response_count: attempt_data.response_count || 0, + answer_index_total: attempt_data.answer_index_total || 0 } end diff --git a/db/migrate/20170312202259_create_school_categories.rb b/db/migrate/20170312202259_create_school_categories.rb index f311dc38..3873d546 100644 --- a/db/migrate/20170312202259_create_school_categories.rb +++ b/db/migrate/20170312202259_create_school_categories.rb @@ -3,9 +3,9 @@ class CreateSchoolCategories < ActiveRecord::Migration[5.0] 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.integer :attempt_count, default: 0 + t.integer :response_count, default: 0 + t.integer :answer_index_total, default: 0 t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index fa14b561..03f74196 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -125,11 +125,11 @@ ActiveRecord::Schema.define(version: 20170312202259) do create_table "school_categories", force: :cascade do |t| t.integer "school_id" t.integer "category_id" - t.integer "attempt_count" - t.integer "response_count" - t.integer "answer_index_total" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.integer "attempt_count", default: 0 + t.integer "response_count", default: 0 + t.integer "answer_index_total", default: 0 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false 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 end diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index f842b129..04d5d882 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -202,6 +202,6 @@ namespace :data do end ENV.delete('BULK_PROCESS') - SchoolCategory.all.each { |sc| sc.aggregate_responses } + SchoolCategory.all.each { |sc| sc.sync_aggregated_responses } end end diff --git a/spec/models/attempt_spec.rb b/spec/models/attempt_spec.rb index 76256fda..28d5bef2 100644 --- a/spec/models/attempt_spec.rb +++ b/spec/models/attempt_spec.rb @@ -9,7 +9,8 @@ RSpec.describe Attempt, type: :model do school.recipient_lists.create!(name: 'Parents', recipient_ids: "#{recipient.id}") end - let!(:question) { create_questions(1).first } + let!(:category) { Category.create(name: 'Category') } + let!(:question) { create_questions(1, category).first } let!(:question_list) do QuestionList.create!(name: 'Parent Questions', question_ids: "#{question.id}") end @@ -35,6 +36,30 @@ RSpec.describe Attempt, type: :model do ) 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 before :each do Timecop.freeze diff --git a/spec/models/recipient_schedule_spec.rb b/spec/models/recipient_schedule_spec.rb index 4b22efef..71ad25b7 100644 --- a/spec/models/recipient_schedule_spec.rb +++ b/spec/models/recipient_schedule_spec.rb @@ -9,7 +9,8 @@ RSpec.describe RecipientSchedule, type: :model do school.recipient_lists.create!(name: 'Parents', recipient_ids: "#{recipient.id}") end - let!(:questions) { create_questions(3) } + let(:category) { Category.create(name: 'Category') } + let!(:questions) { create_questions(3, category) } let!(:question_list) do QuestionList.create!(name: 'Parent Questions', question_ids: questions.map(&:id).join(',')) end