diff --git a/app/models/attempt.rb b/app/models/attempt.rb index 37c78ed7..cfc80f8a 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -8,10 +8,11 @@ class Attempt < ApplicationRecord belongs_to :question after_save :update_school_categories + after_commit :update_counts scope :for_category, -> (category) { joins(:question).merge(Question.for_category(category)) } scope :for_school, -> (school) { joins(:recipient).merge(Recipient.for_school(school)) } - scope :with_responses, -> { where('answer_index is not null')} + scope :with_response, -> { where('answer_index is not null or open_response_id is not null')} def send_message twilio_number = ENV['TWILIO_NUMBER'] @@ -42,4 +43,11 @@ class Attempt < ApplicationRecord school_category.sync_aggregated_responses end + def update_counts + recipient.update_attributes( + attempts_count: recipient.attempts.count, + responses_count: recipient.attempts.with_response.count + ) + end + end diff --git a/app/models/question.rb b/app/models/question.rb index 2578c61b..d3a1db17 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -37,7 +37,7 @@ class Question < ApplicationRecord end def aggregated_responses_for_school(school) - school_responses = attempts.for_school(school).with_responses + school_responses = attempts.for_school(school).with_response.order(id: :asc) return unless school_responses.present? response_answer_total = school_responses.inject(0) { |total, response| total + response.answer_index } diff --git a/app/views/schools/admin.html.haml b/app/views/schools/admin.html.haml index 4f7313ad..e9d2aae4 100644 --- a/app/views/schools/admin.html.haml +++ b/app/views/schools/admin.html.haml @@ -68,10 +68,14 @@ %thead{style: 'font-weight: bold;'} %th Name %th Phone + %th Attempts + %th Responses %th{colspan: 2} Actions - @school.recipients.each do |recipient| %tr.recipient %td= link_to recipient.name, [@school, recipient] %td= recipient.phone + %td= recipient.attempts_count + %td= recipient.responses_count %td= link_to('Edit', edit_school_recipient_path(@school, recipient)) %td= link_to('Delete', school_recipient_path(@school, recipient), method: :delete, data: {confirm: 'Are you sure you want to delete this recipient?'}) diff --git a/db/migrate/20170317141348_add_email_to_recipient.rb b/db/migrate/20170317141348_add_email_to_recipient.rb new file mode 100644 index 00000000..0424d322 --- /dev/null +++ b/db/migrate/20170317141348_add_email_to_recipient.rb @@ -0,0 +1,7 @@ +class AddEmailToRecipient < ActiveRecord::Migration[5.0] + def change + add_column :recipients, :email, :string + add_column :recipients, :slug, :string + add_index :recipients, :slug, unique: true + end +end diff --git a/db/migrate/20170317150205_add_counters_to_recipient.rb b/db/migrate/20170317150205_add_counters_to_recipient.rb new file mode 100644 index 00000000..9f53c53f --- /dev/null +++ b/db/migrate/20170317150205_add_counters_to_recipient.rb @@ -0,0 +1,6 @@ +class AddCountersToRecipient < ActiveRecord::Migration[5.0] + def change + add_column :recipients, :attempts_count, :integer, default: 0 + add_column :recipients, :responses_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 3975d75f..6f4d4ca8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170316194122) do +ActiveRecord::Schema.define(version: 20170317150205) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -102,11 +102,16 @@ ActiveRecord::Schema.define(version: 20170316194122) do t.string "ethnicity" t.integer "home_language_id" t.string "income" - t.boolean "opted_out" + t.boolean "opted_out", default: false t.integer "school_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "email" + t.string "slug" + t.integer "attempts_count", default: 0 + t.integer "responses_count", default: 0 t.index ["phone"], name: "index_recipients_on_phone", using: :btree + t.index ["slug"], name: "index_recipients_on_slug", unique: true, using: :btree end create_table "schedules", force: :cascade do |t| diff --git a/spec/models/attempt_spec.rb b/spec/models/attempt_spec.rb index 28d5bef2..f902ff58 100644 --- a/spec/models/attempt_spec.rb +++ b/spec/models/attempt_spec.rb @@ -60,6 +60,31 @@ RSpec.describe Attempt, type: :model do end end + describe 'counters' do + it 'are updated when an attempt is created' do + expect(recipient.attempts_count).to eq(1) + expect(recipient.responses_count).to eq(0) + end + + it 'are updated when an attempt is destroyed' do + attempt.destroy + expect(recipient.attempts_count).to eq(0) + expect(recipient.responses_count).to eq(0) + end + + it 'are updated when an attempt is responded to' do + attempt.update_attributes(answer_index: 2) + expect(recipient.attempts_count).to eq(1) + expect(recipient.responses_count).to eq(1) + end + + it 'are updated when an attempt is responded to with an open-ended response' do + attempt.update_attributes(open_response_id: 1) + expect(recipient.attempts_count).to eq(1) + expect(recipient.responses_count).to eq(1) + end + end + describe 'send_message' do before :each do Timecop.freeze