revising attempt response to describe how many people have responded

pull/1/head
Jared Cosulich 9 years ago
parent affc5aae53
commit 60a2982724

@ -3,7 +3,8 @@ class AttemptsController < ApplicationController
protect_from_forgery :except => [:twilio] protect_from_forgery :except => [:twilio]
def 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') if (twilio_params[:Body].downcase == 'stop')
attempt.recipient.update_attributes(opted_out: true) attempt.recipient.update_attributes(opted_out: true)
@ -17,9 +18,18 @@ class AttemptsController < ApplicationController
responded_at: Time.new, responded_at: Time.new,
twilio_details: twilio_params.to_h.to_yaml 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 response_message = ["We've registered your response of \"#{attempt.response}\"."]
#{school_category_url(attempt.recipient.school, attempt.question.category)}"""
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 end
# # GET /attempts/1/edit # # GET /attempts/1/edit

@ -10,6 +10,7 @@ class Attempt < ApplicationRecord
after_save :update_school_categories after_save :update_school_categories
after_commit :update_counts 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_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_response, -> { where('answer_index is not null or open_response_id is not null')} scope :with_response, -> { where('answer_index is not null or open_response_id is not null')}

@ -6,8 +6,12 @@ RSpec.describe AttemptsController, type: :controller do
let(:schedule) { Schedule.new } let(:schedule) { Schedule.new }
let(:school) { School.create!(name: 'School') } let(:school) { School.create!(name: 'School') }
let(:recipient) { school.recipients.create!(name: 'Recipient', phone: '+11231231234') } let(:recipient) { school.recipients.create!(name: 'Recipient', phone: '+11231231234') }
let(:recipient_schedule) { RecipientSchedule.new } 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(:category) { Category.create!(name: 'Category') }
let(:question) { create_questions(1, category).first } let(:question) { create_questions(1, category).first }
let!(:first_attempt) { let!(:first_attempt) {
@ -26,6 +30,14 @@ RSpec.describe AttemptsController, type: :controller do
question: question question: question
) )
} }
let!(:attempt2) {
Attempt.create(
schedule: schedule,
recipient: recipient2,
recipient_schedule: recipient_schedule2,
question: question
)
}
describe "POST #twilio" do 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'} {'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 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 attempt.reload
expect(attempt.answer_index).to eq(3) expect(attempt.answer_index).to eq(3)
expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml) expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml)
@ -47,10 +66,32 @@ RSpec.describe AttemptsController, type: :controller do
end end
it "sends back a message" do 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\". 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"
expect(response.body).to eq """We\'ve registered your response of \"Option 0:1 C\". end
To see how others responded to the same question please visit
http://test.host/schools/school/categories/category""" 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
end end

Loading…
Cancel
Save