diff --git a/app/helpers/schedules_helper.rb b/app/helpers/schedules_helper.rb index d8f3706c..d2794569 100644 --- a/app/helpers/schedules_helper.rb +++ b/app/helpers/schedules_helper.rb @@ -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) diff --git a/app/models/recipient_schedule.rb b/app/models/recipient_schedule.rb index 040e818a..e9d6f1c6 100644 --- a/app/models/recipient_schedule.rb +++ b/app/models/recipient_schedule.rb @@ -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? diff --git a/app/models/schedule.rb b/app/models/schedule.rb index 63c28276..de181db4 100644 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -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) diff --git a/app/views/schedules/_form.html.haml b/app/views/schedules/_form.html.haml index 8c3e7ba9..2755c775 100644 --- a/app/views/schedules/_form.html.haml +++ b/app/views/schedules/_form.html.haml @@ -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/ diff --git a/db/migrate/20170412204724_add_time_to_schedule.rb b/db/migrate/20170412204724_add_time_to_schedule.rb new file mode 100644 index 00000000..12bff92a --- /dev/null +++ b/db/migrate/20170412204724_add_time_to_schedule.rb @@ -0,0 +1,5 @@ +class AddTimeToSchedule < ActiveRecord::Migration[5.0] + def change + add_column :schedules, :time, :integer, default: 960 + end +end diff --git a/db/schema.rb b/db/schema.rb index 5771af68..751d7e29 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index 5ca46fd9..75b9d95e 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -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 diff --git a/spec/models/recipient_schedule_spec.rb b/spec/models/recipient_schedule_spec.rb index 2703c190..44ab7a26 100644 --- a/spec/models/recipient_schedule_spec.rb +++ b/spec/models/recipient_schedule_spec.rb @@ -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