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?
school_category = SchoolCategory.create(school: recipient.school, category: question.category)
end
# school_category.aggregate_responses
school_category.sync_aggregated_responses
end
end

@ -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

@ -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

@ -125,9 +125,9 @@ 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.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

@ -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

@ -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

@ -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

Loading…
Cancel
Save