Created a mailer for sending weekly assignment notifications#4
Created a mailer for sending weekly assignment notifications#4
Conversation
There was a problem hiding this comment.
You should try getting this going on production after making some of the changes here. We can ignore the normal workflow of merge to master then deploy to production. There will be some additional SMTP configuration, I imagine. I'd start by trying to implement the SendGrid addon: https://elements.heroku.com/addons/sendgrid
app/mailers/user_mailer.rb
Outdated
| @@ -0,0 +1,9 @@ | |||
| class UserMailer < ApplicationMailer | |||
| default from: "MrClean@mrclean.ca" | |||
There was a problem hiding this comment.
This should go in the ApplicationMailer.
We should talk to Dan about getting an official getyardstick.com email address for Mr. Clean.
app/mailers/user_mailer.rb
Outdated
| def assignment_reminder(employee) | ||
| @employee = employee | ||
|
|
||
| mail(to: 'smtp://localhost.1025', subject: "Test email") |
There was a problem hiding this comment.
to: employee.email
Subject should be finalized.
| <p>You can view the schedule <%= link_to "here", assignments_url %>. </p> | ||
|
|
||
| <p>Thanks,</p> | ||
| <p>Mr Clean</p> |
| <p>Thanks,</p> | ||
| <p>Mr Clean</p> | ||
| </body> | ||
| </html> No newline at end of file |
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title></title> |
There was a problem hiding this comment.
This title can be removed.
We should add the content type meta tag here for utf-8. I'm not sure how email clients treat encoding of the email page but better safe than sorry.
| <head> | ||
| <title></title> | ||
| </head> | ||
| <body> |
There was a problem hiding this comment.
Everything above here should go into a layout for emails. See the docs: http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-layouts
| @@ -0,0 +1,9 @@ | |||
| Hello <%= @employee.first_name %> | |||
|
|
||
| Thanks, | ||
|
|
||
| Mr Clean No newline at end of file |
config/application.rb
Outdated
| # Don't generate system test files. | ||
| config.generators.system_tests = nil | ||
|
|
||
| config.action_mailer.default_url_options = { host: "localhost:3000" } |
There was a problem hiding this comment.
Can you also add this to the production config in the environments directory. In production, we should use ENV['HOST'].
| <p>Mr Clean</p> | ||
| </body> | ||
| </html> No newline at end of file | ||
| <p>Mr. Clean</p> |
| config.file_watcher = ActiveSupport::EventedFileUpdateChecker | ||
|
|
||
| config.action_mailer.delivery_method = :smtp | ||
| config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } |
|
Looks good as long as emails work on Heroku. |
config/application.rb
Outdated
| # Don't generate system test files. | ||
| config.generators.system_tests = nil | ||
|
|
||
| #config.action_mailer.default_url_options = { host: "localhost:3000" } |
There was a problem hiding this comment.
Can you hear this code? It's saying "delete me! Delete me!"
config/environments/production.rb
Outdated
| config.action_mailer.smtp_settings = { | ||
| :user_name => ENV['SENDGRID_USERNAME'], | ||
| :password => ENV['SENDGRID_PASSWORD'], | ||
| :domain => 'https://mr-clean.herokuapp.com', |
There was a problem hiding this comment.
Can you get away with just providing the ENV['HOST'] here? Or, are there any other environment variables that work?
jfeaver
left a comment
There was a problem hiding this comment.
This seems fine to me if it makes things work. Did you ever figure out why ENV['HOST'] was causing problems?
…be used by heroku scheduler
…to AddedMailer
lib/tasks/mail_tasks.rake
Outdated
| namespace :mail_tasks do | ||
| desc "rake task for heroku schedule to send out the weekly notifications." | ||
| task send_weekly_notifications: :environment do | ||
| if DateTime.now.wday == 1 #Heroku schedule only allows for every day task not every week so this ensures its only sent of monday |
There was a problem hiding this comment.
Wow. I'm really surprised Heroku doesn't let you use cron-based scheduling.
Ideally, I think I'd want to receive an email on Sunday night so that I see it at the start of the day and don't forget to unload the dishes on Monday morning.
if Date.today.sunday?
lib/tasks/mail_tasks.rake
Outdated
| week = Week.upcoming.first | ||
| assignments = Assignment.where(week_id: week.id) | ||
|
|
||
| assignments.each do |assignment| |
There was a problem hiding this comment.
Assignment.where(week: week).find_each do |assignment|
lib/tasks/mail_tasks.rake
Outdated
| assignments.each do |assignment| | ||
| employee = assignment.employee | ||
|
|
||
| UserMailer.assignment_reminder(employee).deliver |
There was a problem hiding this comment.
An assignment reminder should probably get the assignment. Who knows, maybe it needs more information than just who the employee is. So: UserMailer.assignment_reminder(assignment).deliver
lib/tasks/mail_tasks.rake
Outdated
| employee = assignment.employee | ||
|
|
||
| UserMailer.assignment_reminder(employee).deliver | ||
| end |
There was a problem hiding this comment.
Please put everything inside the if sunday? block into a new service called SendsAssignmentReminders. So the task is just:
SendsAssignmentReminders.call if sunday?
This is ideal because this rake task might be replaced by an ActiveJob, Sidekiq Worker, or any number of other task-runner objects. Also, we can decide to send reminders from anywhere in the app (i.e. maybe a manager wants to click a button to send an extra reminder).
jfeaver
left a comment
There was a problem hiding this comment.
I wanted to make some small changes before this went into production (particularly the email address) so I pulled down the code. I thought it would be faster but I got a little more carried away than I thought.
I'm pretty sure the changes should just work but you should pull down the changes and check them. I think the GeneratesNewWeekAssignments service could be improved a little more (might be combine the SQL queries) but it's good for now.
This is for my Trello story:
As an employee, I want to receive email notifications, so that I know when I'm scheduled for.