respect opting out

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

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

@ -2,7 +2,7 @@
.col
%p
%strong School:
= @school.name
= link_to @school.name, school_admin_path(@school)
%p
%strong Recipient:
= @recipient.name

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

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

Loading…
Cancel
Save