Skip to content
Browse files

working in sending reminder emails

  • Loading branch information...
1 parent 22febea commit 421d61ec46193aa418ecff1874f2c1c981f9d467 Francis committed Apr 12, 2012
View
8 app/mailers/reminder_email.rb
@@ -0,0 +1,8 @@
+class ReminderEmail < ActionMailer::Base
+ default from: "reminders@yata.com"
+
+ def reminder_email(task)
+ @task = task
+ mail(:to => task.user.email, :subject => "Reminder for task #{task.name.inspect}")
+ end
+end
View
11 app/models/task.rb
@@ -10,9 +10,20 @@ class Task < ActiveRecord::Base
scope :today , lambda { todo.where("due_at < ?", Time.current.end_of_day) }
scope :tomorrow, lambda { todo.where("due_at between ? and ?", Time.current.tomorrow.beginning_of_day, Time.current.tomorrow.end_of_day) }
+ scope :pending_to_send_reminder, lambda { todo.where(:reminder_sent => false).where("due_at < ?", Time.current + 1.hour) }
+
+ belongs_to :user
+
validates :name, :due_at, :presence => true
validate :chronic_parsed_humanized_due_at
+ def self.send_reminders
+ pending_to_send_reminder.includes(:user).find_each do |task|
+ ReminderEmail.reminder_email(task).deliver
+ task.update_attribute(:reminder_sent, true)
+ end
+ end
+
def humanized_due_at
@humanized_due_at ||= due_at.to_s
end
View
12 app/views/reminder_email/reminder_email.html.erb
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
+ </head>
+ <body>
+ <h1>Reminder for task <%= @task.name.inspect %></h1>
+ <p>
+ This task shoud be done in <%= time_ago_in_words @task.due_at %>
+ </p>
+ </body>
+</html>
View
3 app/views/reminder_email/reminder_email.text.erb
@@ -0,0 +1,3 @@
+Reminder for task <%= @task.name.inspect %>
+
+This task shoud be done in <%= time_ago_in_words @task.due_at %>
View
5 db/migrate/20120412113743_add_reminder_sent_to_tasks.rb
@@ -0,0 +1,5 @@
+class AddReminderSentToTasks < ActiveRecord::Migration
+ def change
+ add_column :tasks, :reminder_sent, :boolean, :default => false
+ end
+end
View
9 db/schema.rb
@@ -11,15 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120411220140) do
+ActiveRecord::Schema.define(:version => 20120412113743) do
create_table "tasks", :force => true do |t|
t.string "name"
t.datetime "due_at"
- t.boolean "done", :default => false
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.boolean "done", :default => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
t.integer "user_id"
+ t.boolean "reminder_sent", :default => false
end
add_index "tasks", ["user_id"], :name => "index_tasks_on_user_id"
View
3 lib/tasks/send_reminders.rake
@@ -0,0 +1,3 @@
+task :send_reminders => :environment do
+ Task.send_reminders
+end

0 comments on commit 421d61e

Please sign in to comment.
Something went wrong with that request. Please try again.