adding attempt counters

pull/1/head
Jared Cosulich 9 years ago
parent f310d59568
commit d4b3a83681

@ -8,10 +8,11 @@ class Attempt < ApplicationRecord
belongs_to :question belongs_to :question
after_save :update_school_categories after_save :update_school_categories
after_commit :update_counts
scope :for_category, -> (category) { joins(:question).merge(Question.for_category(category)) } scope :for_category, -> (category) { joins(:question).merge(Question.for_category(category)) }
scope :for_school, -> (school) { joins(:recipient).merge(Recipient.for_school(school)) } 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 def send_message
twilio_number = ENV['TWILIO_NUMBER'] twilio_number = ENV['TWILIO_NUMBER']
@ -42,4 +43,11 @@ class Attempt < ApplicationRecord
school_category.sync_aggregated_responses school_category.sync_aggregated_responses
end end
def update_counts
recipient.update_attributes(
attempts_count: recipient.attempts.count,
responses_count: recipient.attempts.with_response.count
)
end
end end

@ -37,7 +37,7 @@ class Question < ApplicationRecord
end end
def aggregated_responses_for_school(school) 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? return unless school_responses.present?
response_answer_total = school_responses.inject(0) { |total, response| total + response.answer_index } response_answer_total = school_responses.inject(0) { |total, response| total + response.answer_index }

@ -68,10 +68,14 @@
%thead{style: 'font-weight: bold;'} %thead{style: 'font-weight: bold;'}
%th Name %th Name
%th Phone %th Phone
%th Attempts
%th Responses
%th{colspan: 2} Actions %th{colspan: 2} Actions
- @school.recipients.each do |recipient| - @school.recipients.each do |recipient|
%tr.recipient %tr.recipient
%td= link_to recipient.name, [@school, recipient] %td= link_to recipient.name, [@school, recipient]
%td= recipient.phone %td= recipient.phone
%td= recipient.attempts_count
%td= recipient.responses_count
%td= link_to('Edit', edit_school_recipient_path(@school, recipient)) %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?'}) %td= link_to('Delete', school_recipient_path(@school, recipient), method: :delete, data: {confirm: 'Are you sure you want to delete this recipient?'})

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

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

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -102,11 +102,16 @@ ActiveRecord::Schema.define(version: 20170316194122) do
t.string "ethnicity" t.string "ethnicity"
t.integer "home_language_id" t.integer "home_language_id"
t.string "income" t.string "income"
t.boolean "opted_out" t.boolean "opted_out", default: false
t.integer "school_id" t.integer "school_id"
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.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 ["phone"], name: "index_recipients_on_phone", using: :btree
t.index ["slug"], name: "index_recipients_on_slug", unique: true, using: :btree
end end
create_table "schedules", force: :cascade do |t| create_table "schedules", force: :cascade do |t|

@ -60,6 +60,31 @@ RSpec.describe Attempt, type: :model do
end end
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 describe 'send_message' do
before :each do before :each do
Timecop.freeze Timecop.freeze

Loading…
Cancel
Save