From 60a298272402f1b27024bff0fe765afe7314200d Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Thu, 13 Apr 2017 10:31:04 -0400 Subject: [PATCH] revising attempt response to describe how many people have responded --- app/controllers/attempts_controller.rb | 18 +++++-- app/models/attempt.rb | 1 + spec/controllers/attempts_controller_spec.rb | 51 ++++++++++++++++++-- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/app/controllers/attempts_controller.rb b/app/controllers/attempts_controller.rb index 41c0c642..e61c7a53 100644 --- a/app/controllers/attempts_controller.rb +++ b/app/controllers/attempts_controller.rb @@ -3,7 +3,8 @@ class AttemptsController < ApplicationController protect_from_forgery :except => [:twilio] def twilio - attempt = Recipient.where(phone: twilio_params['From']).first.attempts.last + recipient = Recipient.where(phone: twilio_params['From']).first + attempt = recipient.attempts.last if (twilio_params[:Body].downcase == 'stop') attempt.recipient.update_attributes(opted_out: true) @@ -17,9 +18,18 @@ class AttemptsController < ApplicationController responded_at: Time.new, twilio_details: twilio_params.to_h.to_yaml ) - render plain: """We've registered your response of \"#{attempt.response}\". -To see how others responded to the same question please visit -#{school_category_url(attempt.recipient.school, attempt.question.category)}""" + + response_message = ["We've registered your response of \"#{attempt.response}\"."] + + response_count = Attempt.for_question(attempt.question).for_school(recipient.school).with_response.count + if response_count > 1 + response_message << "#{response_count} people have responded to this question so far. To see all responses visit" + else + response_message << 'You are the first person to respond to this question. Once more people have responded you will be able to see all responses at' + end + response_message << school_category_url(attempt.recipient.school, attempt.question.category) + + render plain: response_message.join(' ') end # # GET /attempts/1/edit diff --git a/app/models/attempt.rb b/app/models/attempt.rb index fa1da197..a122c2d6 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -10,6 +10,7 @@ class Attempt < ApplicationRecord after_save :update_school_categories after_commit :update_counts + scope :for_question, -> (question) { where(question_id: question.id) } scope :for_category, -> (category) { joins(:question).merge(Question.for_category(category)) } scope :for_school, -> (school) { joins(:recipient).merge(Recipient.for_school(school)) } scope :with_response, -> { where('answer_index is not null or open_response_id is not null')} diff --git a/spec/controllers/attempts_controller_spec.rb b/spec/controllers/attempts_controller_spec.rb index f58d1f3c..d26db241 100644 --- a/spec/controllers/attempts_controller_spec.rb +++ b/spec/controllers/attempts_controller_spec.rb @@ -6,8 +6,12 @@ RSpec.describe AttemptsController, type: :controller do let(:schedule) { Schedule.new } let(:school) { School.create!(name: 'School') } + let(:recipient) { school.recipients.create!(name: 'Recipient', phone: '+11231231234') } let(:recipient_schedule) { RecipientSchedule.new } + let(:recipient2) { school.recipients.create!(name: 'Recipient2', phone: '+12342342345') } + let(:recipient_schedule2) { RecipientSchedule.new } + let(:category) { Category.create!(name: 'Category') } let(:question) { create_questions(1, category).first } let!(:first_attempt) { @@ -26,6 +30,14 @@ RSpec.describe AttemptsController, type: :controller do question: question ) } + let!(:attempt2) { + Attempt.create( + schedule: schedule, + recipient: recipient2, + recipient_schedule: recipient_schedule2, + question: question + ) + } describe "POST #twilio" do @@ -34,8 +46,15 @@ RSpec.describe AttemptsController, type: :controller do {'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+11231231234','To' => '2223334444','Body' => '3','NumMedia' => '0'} } - it "updates the last attempt by recipient phone number" do + before :each do post :twilio, params: twilio_attributes + end + + it 'creates the first attempt with response for the question' do + expect(attempt.question.attempts.for_school(school).with_response.count).to eq(1) + end + + it "updates the last attempt by recipient phone number" do attempt.reload expect(attempt.answer_index).to eq(3) expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml) @@ -47,10 +66,32 @@ RSpec.describe AttemptsController, type: :controller do end it "sends back a message" do - post :twilio, params: twilio_attributes - expect(response.body).to eq """We\'ve registered your response of \"Option 0:1 C\". -To see how others responded to the same question please visit -http://test.host/schools/school/categories/category""" + expect(response.body).to eq "We've registered your response of \"Option 0:1 C\". You are the first person to respond to this question. Once more people have responded you will be able to see all responses at http://test.host/schools/school/categories/category" + end + + context "with second response" do + let(:twilio_attributes2) { + {'MessageSid' => 'fwefwefewfewfasfsdfdf','AccountSid' => 'wefwegdbvcbrtnrn','MessagingServiceSid' => 'dfvdfvegbdfb','From' => '+12342342345','To' => '2223334444','Body' => '4','NumMedia' => '0'} + } + + before :each do + post :twilio, params: twilio_attributes2 + end + + it 'creates the second attempt with response for the question' do + expect(attempt.question.attempts.for_school(school).with_response.count).to eq(2) + end + + it "updates the attempt from the second recipient" do + attempt2.reload + expect(attempt2.answer_index).to eq(4) + expect(attempt2.twilio_details).to eq(twilio_attributes2.with_indifferent_access.to_yaml) + expect(attempt2.responded_at).to be_present + end + + it "sends back a message" do + expect(response.body).to eq "We've registered your response of \"Option 0:1 D\". 2 people have responded to this question so far. To see all responses visit http://test.host/schools/school/categories/category" + end end end