mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-07 13:38:18 -08:00
adding attempt counters
This commit is contained in:
parent
f310d59568
commit
d4b3a83681
7 changed files with 61 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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?'})
|
||||
|
|
|
|||
7
db/migrate/20170317141348_add_email_to_recipient.rb
Normal file
7
db/migrate/20170317141348_add_email_to_recipient.rb
Normal file
|
|
@ -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
|
||||
6
db/migrate/20170317150205_add_counters_to_recipient.rb
Normal file
6
db/migrate/20170317150205_add_counters_to_recipient.rb
Normal file
|
|
@ -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
|
||||
13
db/schema.rb
13
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|
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue