working on adding time to schedule

pull/1/head
Jared Cosulich 9 years ago
parent 284fc73f78
commit 061a3b9fde

@ -9,6 +9,18 @@ module SchedulesHelper
]
end
def options_for_time
words = ['AM', 'PM'].map do |time|
[12, *(1..11)].map do |hour|
['00', '30'].map do |minute|
"#{hour}:#{minute} #{time}"
end
end
end.flatten
words.each_with_index.map { |word, index| [word, index * 30] }
end
def frequency_description(hours)
case hours
when (24 * 7)

@ -37,13 +37,17 @@ class RecipientSchedule < ApplicationRecord
upcoming_question_ids: upcoming,
attempted_question_ids: attempted,
last_attempt_at: attempt.sent_at,
next_attempt_at: attempt.sent_at + (60 * 60 * schedule.frequency_hours)
next_attempt_at: next_attempt_at + (60 * 60 * schedule.frequency_hours)
)
end
return attempt
end
def self.create_for_recipient(recipient_or_recipient_id, schedule, next_attempt_at=Time.new)
def self.create_for_recipient(recipient_or_recipient_id, schedule, next_attempt_at=nil)
if next_attempt_at.nil?
next_attempt_at = Time.at(schedule.start_date.to_time.to_i + (60 * schedule.time))
end
question_ids = schedule.question_list.question_ids.split(/,/)
question_ids = question_ids.shuffle if schedule.random?

@ -8,6 +8,7 @@ class Schedule < ApplicationRecord
validates :recipient_list, presence: true
validates :question_list, presence: true
before_validation :set_start_date
after_create :create_recipient_schedules
scope :active, -> {
@ -16,6 +17,11 @@ class Schedule < ApplicationRecord
private
def set_start_date
return if start_date.present?
self.start_date = Date.today
end
def create_recipient_schedules
recipient_list.recipients.each do |recipient|
RecipientSchedule.create_for_recipient(recipient, self)

@ -27,6 +27,10 @@
= f.label :end_date
%br/
= f.date_select :end_date, class: 'form-control'
.form-group
= f.label :time, 'Time of Day (what time should people receive the text)'
%br/
= f.select :time, options_for_time, class: 'form-control'
.form-group
= f.label :active
%br/

@ -0,0 +1,5 @@
class AddTimeToSchedule < ActiveRecord::Migration[5.0]
def change
add_column :schedules, :time, :integer, default: 960
end
end

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170405183356) do
ActiveRecord::Schema.define(version: 20170412204724) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -127,6 +127,7 @@ ActiveRecord::Schema.define(version: 20170405183356) do
t.integer "question_list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "time", default: 960
t.index ["school_id"], name: "index_schedules_on_school_id", using: :btree
end

@ -42,15 +42,30 @@ describe "survey:attempt_questions" do
question_list: question_list,
frequency_hours: 24 * 7,
start_date: Time.new,
end_date: 1.year.from_now
end_date: 1.year.from_now,
time: 1200
)
end
describe 'First Attempt' do
describe 'First attempt not at specified time' do
before :each do
now = DateTime.now
date = ActiveSupport::TimeZone["America/New_York"].parse(now.strftime("%Y-%m-%dT19:00:00%z"))
Timecop.freeze(date) { subject.invoke }
end
it 'should not create any attempts' do
expect(Attempt.count).to eq(0)
end
end
describe 'First attempt at specified time' do
before :each do
Timecop.freeze
subject.invoke
now = DateTime.now
date = ActiveSupport::TimeZone["America/New_York"].parse(now.strftime("%Y-%m-%dT20:00:00%z"))
Timecop.freeze(date) { subject.invoke }
end
it 'should create the first attempt for each recipient' do

@ -61,7 +61,7 @@ RSpec.describe RecipientSchedule, type: :model do
before :each do
recipient_schedule.recipient.update_attributes(opted_out: true)
end
let!(:attempt) { recipient_schedule.attempt_question }
it 'should not do anything' do
@ -96,7 +96,7 @@ RSpec.describe RecipientSchedule, type: :model do
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)
expect(recipient_schedule.next_attempt_at.to_i).to eq(Date.today.to_time.to_i + (960 * 60))
end
end
end

Loading…
Cancel
Save