parent
a0646ba70f
commit
0cdf38fcc0
@ -0,0 +1,8 @@
|
||||
class Attempt < ApplicationRecord
|
||||
|
||||
belongs_to :schedule
|
||||
belongs_to :recipient
|
||||
belongs_to :recipient_schedule
|
||||
belongs_to :question
|
||||
|
||||
end
|
||||
@ -0,0 +1,29 @@
|
||||
class RecipientSchedule < ApplicationRecord
|
||||
|
||||
belongs_to :recipient
|
||||
belongs_to :schedule
|
||||
has_many :attempts
|
||||
|
||||
def next_question
|
||||
upcoming = upcoming_question_ids.split(/,/)
|
||||
Question.where(id: upcoming.first).first
|
||||
end
|
||||
|
||||
def make_attempt(question: next_question)
|
||||
sent_at = Time.new
|
||||
recipient.attempts.create(
|
||||
schedule: schedule,
|
||||
recipient_schedule: self,
|
||||
question: question,
|
||||
sent_at: sent_at
|
||||
)
|
||||
|
||||
upcoming = upcoming_question_ids.split(/,/)[1..-1].join(',')
|
||||
attempted = (attempted_question_ids.split(/,/) + [question.id]).join(',')
|
||||
update_attributes(
|
||||
upcoming_question_ids: upcoming,
|
||||
attempted_question_ids: attempted,
|
||||
last_attempt_at: sent_at
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,13 @@
|
||||
class CreateRecipientSchedules < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
create_table :recipient_schedules do |t|
|
||||
t.integer :recipient_id
|
||||
t.integer :schedule_id
|
||||
t.text :upcoming_question_ids
|
||||
t.text :attempted_question_ids
|
||||
t.timestamp :last_attempt_at
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,17 @@
|
||||
class CreateAttempts < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
create_table :attempts do |t|
|
||||
t.integer :recipient_id
|
||||
t.integer :schedule_id
|
||||
t.integer :recipient_schedule_id
|
||||
t.timestamp :sent_at
|
||||
t.timestamp :responded_at
|
||||
t.integer :question_id
|
||||
t.integer :translation_id
|
||||
t.integer :answer_index
|
||||
t.integer :open_response_id
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Attempt, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
@ -0,0 +1,61 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe RecipientSchedule, type: :model do
|
||||
|
||||
let(:question) { Question.create!(text: 'What is the question?', option1: 'A', option2: 'B', option3: 'C', option4: 'D', option5: 'E')}
|
||||
let(:question_list) { QuestionList.create(name: 'Parent Questions', question_ids: "#{question.id},2,3")}
|
||||
|
||||
let(:recipient) { Recipient.create!(name: 'Parent') }
|
||||
let(:recipient_list) { RecipientList.create(name: 'Parent List', recipient_ids: recipient.id.to_s)}
|
||||
|
||||
let(:schedule) { Schedule.create!(name: 'Parent Schedule', recipient_list_id: recipient_list.id, question_list: question_list) }
|
||||
|
||||
let!(:recipient_schedule) do
|
||||
RecipientSchedule.create!(
|
||||
recipient: recipient,
|
||||
schedule: schedule,
|
||||
upcoming_question_ids: "#{question.id},3",
|
||||
attempted_question_ids: '2',
|
||||
last_attempt_at: 2.weeks.ago
|
||||
)
|
||||
end
|
||||
|
||||
describe 'next_question' do
|
||||
it 'should provide the next question from the upcoming_question_ids list' do
|
||||
expect(recipient_schedule.next_question).to eq(question)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'make_attempt' do
|
||||
before :each do
|
||||
Timecop.freeze
|
||||
recipient_schedule.make_attempt
|
||||
end
|
||||
|
||||
it 'should contact the twillio API'
|
||||
|
||||
it 'should make an attempt to ask the next question' do
|
||||
expect(Attempt.count).to eq(1)
|
||||
attempt = Attempt.first
|
||||
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(question)
|
||||
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('3')
|
||||
end
|
||||
|
||||
it 'should update the attempted_question_ids' do
|
||||
expect(recipient_schedule.attempted_question_ids).to eq("2,#{question.id}")
|
||||
end
|
||||
|
||||
it 'should update last_attempt_at' do
|
||||
expect(recipient_schedule.last_attempt_at.to_i).to eq(Time.new.to_i)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue