From e236af85294dfdcf5cfebc328ad336455a9f5c3d Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Mon, 26 Oct 2020 00:53:09 -0700 Subject: Implement recurring task feature --- app/controllers/tasks_controller.rb | 7 +++++-- app/jobs/task_reminder_job.rb | 10 ++++++++++ app/mailers/task_reminder_mailer.rb | 4 ++++ app/views/task_lists/show.html.erb | 1 + db/migrate/20201026074144_add_recurs_at_to_task.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 app/jobs/task_reminder_job.rb create mode 100644 app/mailers/task_reminder_mailer.rb create mode 100644 db/migrate/20201026074144_add_recurs_at_to_task.rb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index f072770..8510c0e 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -6,14 +6,17 @@ class TasksController < ApplicationController end def create - @task_list.tasks.create!(task_params) + @task = @task_list.tasks.create!(task_params) + if task_params[:recurs_at] + TaskReminderJob.set(wait: task_params[:recurs_at]).perform_later(@task.id, task_params[:recurs_at]) + end redirect_to @task_list end private def task_params - params.require(:task).permit(:name) + params.require(:task).permit(:name, :recurs_at) end def set_task_list diff --git a/app/jobs/task_reminder_job.rb b/app/jobs/task_reminder_job.rb new file mode 100644 index 0000000..64e9e88 --- /dev/null +++ b/app/jobs/task_reminder_job.rb @@ -0,0 +1,10 @@ +class TaskReminderJob < ApplicationJob + queue_as :default + + def perform(task_id, recurs_at) + task = Task.find(task_id) + if task.recurs_at == recurs_at + TaskReminderMailer.remind.deliver_later + end + end +end diff --git a/app/mailers/task_reminder_mailer.rb b/app/mailers/task_reminder_mailer.rb new file mode 100644 index 0000000..539436d --- /dev/null +++ b/app/mailers/task_reminder_mailer.rb @@ -0,0 +1,4 @@ +class TaskReminderMailer < ApplicationMailer + def remind + end +end diff --git a/app/views/task_lists/show.html.erb b/app/views/task_lists/show.html.erb index 3d0a784..4f5350b 100644 --- a/app/views/task_lists/show.html.erb +++ b/app/views/task_lists/show.html.erb @@ -3,6 +3,7 @@ <%= form_for @task, url: [@task_list, @task] do |f| %> <%= f.label :name %> <%= f.text_field :name %> + <%= time_field(:task, :recurs_at) %> <%= f.submit "Create task" %> <% end %> diff --git a/db/migrate/20201026074144_add_recurs_at_to_task.rb b/db/migrate/20201026074144_add_recurs_at_to_task.rb new file mode 100644 index 0000000..2142c62 --- /dev/null +++ b/db/migrate/20201026074144_add_recurs_at_to_task.rb @@ -0,0 +1,5 @@ +class AddRecursAtToTask < ActiveRecord::Migration[6.0] + def change + add_column :tasks, :recurs_at, :time + end +end diff --git a/db/schema.rb b/db/schema.rb index 337b38d..78045f0 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: 2020_10_26_073940) do +ActiveRecord::Schema.define(version: 2020_10_26_074144) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,6 +28,7 @@ ActiveRecord::Schema.define(version: 2020_10_26_073940) do t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.datetime "completed_at" + t.time "recurs_at" t.index ["task_list_id"], name: "index_tasks_on_task_list_id" end -- cgit v1.2.3