Skip to content

Created a mailer for sending weekly assignment notifications#4

Merged
ryannoort merged 13 commits intomasterfrom
AddedMailer
Oct 27, 2017
Merged

Created a mailer for sending weekly assignment notifications#4
ryannoort merged 13 commits intomasterfrom
AddedMailer

Conversation

@ryannoort
Copy link
Copy Markdown
Contributor

This is for my Trello story:

As an employee, I want to receive email notifications, so that I know when I'm scheduled for.

@ryannoort ryannoort requested a review from jfeaver October 20, 2017 19:58
Copy link
Copy Markdown
Contributor

@jfeaver jfeaver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@@ -0,0 +1,9 @@
class UserMailer < ApplicationMailer
default from: "MrClean@mrclean.ca"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in the ApplicationMailer.

We should talk to Dan about getting an official getyardstick.com email address for Mr. Clean.

def assignment_reminder(employee)
@employee = employee

mail(to: 'smtp://localhost.1025', subject: "Test email")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mr. Clean

<p>Thanks,</p>
<p>Mr Clean</p>
</body>
</html> No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This email is great!

<!DOCTYPE html>
<html>
<head>
<title></title>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 %>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma


Thanks,

Mr Clean No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mr. Clean

# Don't generate system test files.
config.generators.system_tests = nil

config.action_mailer.default_url_options = { host: "localhost:3000" }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add this to the production config in the environments directory. In production, we should use ENV['HOST'].

@ryannoort ryannoort requested a review from jfeaver October 20, 2017 21:12
<p>Mr Clean</p>
</body>
</html> No newline at end of file
<p>Mr. Clean</p>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please de-indent.

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@ryannoort ryannoort requested a review from jfeaver October 20, 2017 22:10
@jfeaver
Copy link
Copy Markdown
Contributor

jfeaver commented Oct 20, 2017

Looks good as long as emails work on Heroku.

# Don't generate system test files.
config.generators.system_tests = nil

#config.action_mailer.default_url_options = { host: "localhost:3000" }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you hear this code? It's saying "delete me! Delete me!"

config.action_mailer.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'https://mr-clean.herokuapp.com',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you get away with just providing the ENV['HOST'] here? Or, are there any other environment variables that work?

@ryannoort ryannoort requested a review from jfeaver October 23, 2017 22:13
Copy link
Copy Markdown
Contributor

@jfeaver jfeaver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me if it makes things work. Did you ever figure out why ENV['HOST'] was causing problems?

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

week = Week.upcoming.first
assignments = Assignment.where(week_id: week.id)

assignments.each do |assignment|
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment.where(week: week).find_each do |assignment|

assignments.each do |assignment|
employee = assignment.employee

UserMailer.assignment_reminder(employee).deliver
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

employee = assignment.employee

UserMailer.assignment_reminder(employee).deliver
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@ryannoort ryannoort requested a review from jfeaver October 27, 2017 02:28
Copy link
Copy Markdown
Contributor

@jfeaver jfeaver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ryannoort ryannoort merged commit 021fe01 into master Oct 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants