From f310d595689509dfedee7b9a11bf488154798bf1 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Thu, 16 Mar 2017 17:52:01 -0400 Subject: [PATCH] respect opting out --- app/models/recipient_schedule.rb | 1 + app/views/recipients/show.html.haml | 2 +- spec/lib/tasks/survey_rake_spec.rb | 25 +++++++++++ spec/models/recipient_schedule_spec.rb | 59 ++++++++++++++++---------- 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/app/models/recipient_schedule.rb b/app/models/recipient_schedule.rb index 5cdb7ff1..af352c9a 100644 --- a/app/models/recipient_schedule.rb +++ b/app/models/recipient_schedule.rb @@ -22,6 +22,7 @@ class RecipientSchedule < ApplicationRecord end def attempt_question(question: next_question) + return if recipient.opted_out? attempt = recipient.attempts.create( schedule: schedule, recipient_schedule: self, diff --git a/app/views/recipients/show.html.haml b/app/views/recipients/show.html.haml index 2a07346e..ea80feee 100644 --- a/app/views/recipients/show.html.haml +++ b/app/views/recipients/show.html.haml @@ -2,7 +2,7 @@ .col %p %strong School: - = @school.name + = link_to @school.name, school_admin_path(@school) %p %strong Recipient: = @recipient.name diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index ed14bb5a..5ca46fd9 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -104,5 +104,30 @@ describe "survey:attempt_questions" do end end end + + describe 'Opted Out Recipient' do + + before :each do + recipients[1].update_attributes(opted_out: true) + Timecop.freeze + subject.invoke + end + + it 'should create the first attempt for each recipient' do + recipients.each_with_index do |recipient, index| + recipient.reload + if index == 1 + expect(recipient.attempts.count).to eq(0) + expect(recipient.attempts.first).to be_nil + else + expect(recipient.attempts.count).to eq(1) + attempt = recipient.attempts.first + expect(attempt.sent_at).to be_present + expect(attempt.answer_index).to be_nil + end + end + end + end + end end diff --git a/spec/models/recipient_schedule_spec.rb b/spec/models/recipient_schedule_spec.rb index 71ad25b7..2703c190 100644 --- a/spec/models/recipient_schedule_spec.rb +++ b/spec/models/recipient_schedule_spec.rb @@ -57,32 +57,47 @@ RSpec.describe RecipientSchedule, type: :model do Timecop.freeze end - let!(:attempt) { recipient_schedule.attempt_question } - - it 'should make an attempt to ask the next question' do - expect(attempt).to be_persisted - expect(attempt.recipient).to eq(recipient) - expect(attempt.schedule).to eq(schedule) - expect(attempt.recipient_schedule).to eq(recipient_schedule) - expect(attempt.question).to eq(questions.first) - expect(attempt.sent_at.to_i).to eq(Time.new.to_i) - expect(attempt.answer_index).to be_nil - end - - it 'should update the upcoming_questions_ids' do - expect(recipient_schedule.upcoming_question_ids).to eq(questions[1..2].map(&:id).join(',')) - end + describe 'with an opted out recipient' do + before :each do + recipient_schedule.recipient.update_attributes(opted_out: true) + end + + let!(:attempt) { recipient_schedule.attempt_question } - it 'should update the attempted_question_ids' do - expect(recipient_schedule.attempted_question_ids).to eq(questions.first.id.to_s) - end + it 'should not do anything' do + expect(attempt).to be_nil + end - it 'should update last_attempt_at' do - expect(recipient_schedule.last_attempt_at.to_i).to eq(Time.new.to_i) end - it 'should update next_attempt_at' do - expect(recipient_schedule.next_attempt_at.to_i).to eq((Time.new + (60 * 60 * schedule.frequency_hours)).to_i) + describe 'with an opted in recipient' do + let!(:attempt) { recipient_schedule.attempt_question } + + it 'should make an attempt to ask the next question' do + expect(attempt).to be_persisted + expect(attempt.recipient).to eq(recipient) + expect(attempt.schedule).to eq(schedule) + expect(attempt.recipient_schedule).to eq(recipient_schedule) + expect(attempt.question).to eq(questions.first) + expect(attempt.sent_at.to_i).to eq(Time.new.to_i) + expect(attempt.answer_index).to be_nil + end + + it 'should update the upcoming_questions_ids' do + expect(recipient_schedule.upcoming_question_ids).to eq(questions[1..2].map(&:id).join(',')) + end + + it 'should update the attempted_question_ids' do + expect(recipient_schedule.attempted_question_ids).to eq(questions.first.id.to_s) + end + + it 'should update last_attempt_at' do + expect(recipient_schedule.last_attempt_at.to_i).to eq(Time.new.to_i) + end + + it 'should update next_attempt_at' do + expect(recipient_schedule.next_attempt_at.to_i).to eq((Time.new + (60 * 60 * schedule.frequency_hours)).to_i) + end end end end