From aee2bce91d5dfcb4eb6ef9754b4e0ee95ebc47ea Mon Sep 17 00:00:00 2001
From: Kevin Peery
Date: Thu, 24 Jan 2019 20:39:21 -0700
Subject: [PATCH 1/7] set up rails active mailer in the backend
---
Gemfile | 1 +
Gemfile.lock | 8 ++++++
app/controllers/api/users_controller.rb | 19 +++++++------
app/mailers/application_mailer.rb | 2 +-
app/mailers/user_mailer.rb | 9 +++++++
app/models/user.rb | 3 +++
app/views/user_mailer/welcome_email.html.erb | 17 ++++++++++++
app/views/user_mailer/welcome_email.text.erb | 5 ++++
client/src/providers/AuthProvider.js | 10 +++----
config/environments/development.rb | 28 +++++++++++++++++---
config/environments/production.rb | 12 +++++----
config/environments/test.rb | 5 ++--
test/mailers/previews/user_mailer_preview.rb | 4 +++
test/mailers/user_mailer_test.rb | 7 +++++
14 files changed, 104 insertions(+), 26 deletions(-)
create mode 100644 app/mailers/user_mailer.rb
create mode 100644 app/views/user_mailer/welcome_email.html.erb
create mode 100644 app/views/user_mailer/welcome_email.text.erb
create mode 100644 test/mailers/previews/user_mailer_preview.rb
create mode 100644 test/mailers/user_mailer_test.rb
diff --git a/Gemfile b/Gemfile
index 1d3a60e..6a44d74 100644
--- a/Gemfile
+++ b/Gemfile
@@ -22,6 +22,7 @@ group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
+ gem 'letter_opener'
end
diff --git a/Gemfile.lock b/Gemfile.lock
index 46bd15b..d26664e 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -42,6 +42,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
+ addressable (2.6.0)
+ public_suffix (>= 2.0.2, < 4.0)
arel (9.0.0)
aws_cf_signer (0.1.3)
bcrypt (3.1.12)
@@ -79,6 +81,10 @@ GEM
domain_name (~> 0.5)
i18n (1.5.1)
concurrent-ruby (~> 1.0)
+ launchy (2.4.3)
+ addressable (~> 2.3)
+ letter_opener (1.7.0)
+ launchy (~> 2.2)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
@@ -108,6 +114,7 @@ GEM
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
+ public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.6)
rack-test (1.1.0)
@@ -182,6 +189,7 @@ DEPENDENCIES
devise_token_auth
dotenv-rails
faker
+ letter_opener
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry
diff --git a/app/controllers/api/users_controller.rb b/app/controllers/api/users_controller.rb
index 4a9f527..0e5e4ef 100644
--- a/app/controllers/api/users_controller.rb
+++ b/app/controllers/api/users_controller.rb
@@ -11,15 +11,15 @@ def show
end
def create
- user = User.new(user_params)
- if user.save
+ @user = User.new(user_params)
+ if @user.save
render json: user
else
render json: user.errors, status: 422
end
end
- def update
+ def update
user = User.find(params[:id])
user.email = params[:email] ? params[:email] : user.email
user.first_name = params[:first_name] ? params[:first_name] : user.first_name
@@ -30,27 +30,26 @@ def update
user.college_degree = params[:college_degree] ? params[:college_degree] : user.college_degree
user.employment_status = params[:employment_status] ? params[:employment_status] : user.employment_status
user.sex = params[:sex] ? params[:sex] : user.sex
- user.github = params[:github] ? params[:github] : user.github
+ user.github = params[:github] ? params[:github] : user.github
user.linkedin = params[:linkedin] ? params[:linkedin] : user.linkedin
user.resume = params[:resume] ? params[:resume] : user.resume
file = params[:file]
-
+
if file != "undefined"
begin
ext = File.extname(file.tempfile)
cloud_image = Cloudinary::Uploader.upload(file, public_id: file.original_filename, secure: true)
- user.image = cloud_image['secure_url']
+ user.image = cloud_image["secure_url"]
rescue => e
- render json: { errors: e }, status: 422
+ render json: {errors: e}, status: 422
end
end
-
+
if user.save
render json: user
else
- render json: { errors: user.errors.full_messages }, status: 422
+ render json: {errors: user.errors.full_messages}, status: 422
end
-
end
def destroy
diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
index 286b223..75e4e4d 100644
--- a/app/mailers/application_mailer.rb
+++ b/app/mailers/application_mailer.rb
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
- default from: 'from@example.com'
+ default from: 'DevTrackerW18@gmail.com'
layout 'mailer'
end
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
new file mode 100644
index 0000000..71eae7a
--- /dev/null
+++ b/app/mailers/user_mailer.rb
@@ -0,0 +1,9 @@
+class UserMailer < ApplicationMailer
+ default from: "notifications@example.com"
+
+ def welcome_email(user)
+ @user = params[:user]
+ @url = "https://dev-tracker19.herokuapp.com/login"
+ mail(to: @user.email, subject: "Welcome to DevTracker!")
+ end
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index f9efae7..8b15e65 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -10,4 +10,7 @@ class User < ActiveRecord::Base
has_many :applications, dependent: :destroy
has_many :companies, through: :applications
has_many :todos, dependent: :destroy
+ after_create do
+ UserMailer.welcome_email(@user).deliver_now
+ end
end
diff --git a/app/views/user_mailer/welcome_email.html.erb b/app/views/user_mailer/welcome_email.html.erb
new file mode 100644
index 0000000..8832fca
--- /dev/null
+++ b/app/views/user_mailer/welcome_email.html.erb
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+ Welcome to https://dev-tracker19.herokuapp.com/, <%= @user.email %>
+
+ You have successfully signed up to dev-tracker19.herokuapp.com,
+ your username is: <%= @user.email %>.
+
+
+ To login to the site, just follow this link: <%= @https://dev-tracker19.herokuapp.com/login %>.
+
+ Thanks for joining and have a great day!
+
+
\ No newline at end of file
diff --git a/app/views/user_mailer/welcome_email.text.erb b/app/views/user_mailer/welcome_email.text.erb
new file mode 100644
index 0000000..33a8f50
--- /dev/null
+++ b/app/views/user_mailer/welcome_email.text.erb
@@ -0,0 +1,5 @@
+Welcome to example.com, <%= @user.email %>
+===============================================
+You have successfully signed up to example.com,
+your username is: <%= @user.email %>.
+Thanks for joining and have a great day!
\ No newline at end of file
diff --git a/client/src/providers/AuthProvider.js b/client/src/providers/AuthProvider.js
index d152e3a..02865c9 100644
--- a/client/src/providers/AuthProvider.js
+++ b/client/src/providers/AuthProvider.js
@@ -25,15 +25,15 @@ class AuthProvider extends React.Component {
axios
.put(
`/api/users/${id}?email=${user.email}&first_name=${
- user.first_name
+ user.first_name
}&last_name=${user.last_name}&image=${user.image}&cohort=${
- user.cohort
+ user.cohort
}&dob=${user.dob}&college_degree=${
- user.college_degree
+ user.college_degree
}&employment_status=${user.employment_status}&sex=${
- user.sex
+ user.sex
}&github=${user.github}&linkedin=${user.linkedin}&resume=${
- user.resume
+ user.resume
}`,
data
)
diff --git a/config/environments/development.rb b/config/environments/development.rb
index d52ec9e..c70c40d 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -14,12 +14,12 @@
# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
- if Rails.root.join('tmp', 'caching-dev.txt').exist?
+ if Rails.root.join("tmp", "caching-dev.txt").exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
- 'Cache-Control' => "public, max-age=#{2.days.to_i}"
+ "Cache-Control" => "public, max-age=#{2.days.to_i}",
}
else
config.action_controller.perform_caching = false
@@ -31,7 +31,7 @@
config.active_storage.service = :local
# Don't care if the mailer can't send.
- config.action_mailer.raise_delivery_errors = false
+ config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
@@ -44,6 +44,28 @@
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true
+ config.action_mailer.delivery_method = :sendmail
+ # Defaults to:
+ # config.action_mailer.sendmail_settings = {
+ # location: '/usr/sbin/sendmail',
+ # arguments: '-i'
+ # }
+ config.action_mailer.perform_deliveries = true
+ config.action_mailer.raise_delivery_errors = true
+ config.action_mailer.default_options = {from: "DevTrackerW18@example.com"}
+
+ config.action_mailer.delivery_method = :smtp
+ config.action_mailer.smtp_settings = {
+ address: "smtp.gmail.com",
+ port: 587,
+ domain: "https://dev-tracker19.herokuapp.com/login",
+ user_name: "DevTrackerW18@gmail.com",
+ password: "dtrackw18",
+ authentication: "plain",
+ enable_starttls_auto: true,
+ }
+
+ config.active_job.queue_adapter = :inline
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
diff --git a/config/environments/production.rb b/config/environments/production.rb
index 801bed4..902c5ab 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -1,6 +1,8 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
+ config.active_job.queue_adapter = :async
+
# Code is not reloaded between requests.
config.cache_classes = true
@@ -11,7 +13,7 @@
config.eager_load = true
# Full error reports are disabled and caching is turned on.
- config.consider_all_requests_local = false
+ config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
@@ -20,7 +22,7 @@
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
- config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
+ config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com'
@@ -45,7 +47,7 @@
config.log_level = :debug
# Prepend all log lines with the following tags.
- config.log_tags = [ :request_id ]
+ config.log_tags = [:request_id]
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
@@ -75,9 +77,9 @@
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
if ENV["RAILS_LOG_TO_STDOUT"].present?
- logger = ActiveSupport::Logger.new(STDOUT)
+ logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
- config.logger = ActiveSupport::TaggedLogging.new(logger)
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
end
# Do not dump schema after migrations.
diff --git a/config/environments/test.rb b/config/environments/test.rb
index 0a38fd3..8a91fd9 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -15,11 +15,11 @@
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
- 'Cache-Control' => "public, max-age=#{1.hour.to_i}"
+ "Cache-Control" => "public, max-age=#{1.hour.to_i}",
}
# Show full error reports and disable caching.
- config.consider_all_requests_local = true
+ config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Raise exceptions instead of rendering exception templates.
@@ -37,6 +37,7 @@
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
+ config.active_job.queue_adapter = :test
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb
new file mode 100644
index 0000000..957e12b
--- /dev/null
+++ b/test/mailers/previews/user_mailer_preview.rb
@@ -0,0 +1,4 @@
+# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
+class UserMailerPreview < ActionMailer::Preview
+
+end
diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb
new file mode 100644
index 0000000..67a1629
--- /dev/null
+++ b/test/mailers/user_mailer_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserMailerTest < ActionMailer::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
From 5c29b1ecc17b95de68b7cdc20bb1db6bef72e14b Mon Sep 17 00:00:00 2001
From: Kevin Peery
Date: Sat, 26 Jan 2019 19:28:39 -0700
Subject: [PATCH 2/7] continue work on user and todo mailer
---
app/controllers/api/todos_controller.rb | 5 ++-
app/mailers/todo_mailer.rb | 9 +++++
app/mailers/user_mailer.rb | 4 +-
app/models/user.rb | 5 ++-
app/views/user_mailer/todos_email.html.erb | 10 +++++
app/views/user_mailer/todos_email.text.erb | 3 ++
app/views/user_mailer/welcome_email.html.erb | 2 +-
app/views/user_mailer/welcome_email.text.erb | 4 +-
config/environments/development.rb | 41 +++++++++++---------
config/environments/production.rb | 18 +++++++++
10 files changed, 73 insertions(+), 28 deletions(-)
create mode 100644 app/mailers/todo_mailer.rb
create mode 100644 app/views/user_mailer/todos_email.html.erb
create mode 100644 app/views/user_mailer/todos_email.text.erb
diff --git a/app/controllers/api/todos_controller.rb b/app/controllers/api/todos_controller.rb
index a3f8dbb..1e263cd 100644
--- a/app/controllers/api/todos_controller.rb
+++ b/app/controllers/api/todos_controller.rb
@@ -10,9 +10,10 @@ def show
end
def create
- todo = current_user.todos.new(todo_params)
+ @todo = current_user.todos.new(todo_params)
- if todo.save
+ if @todo.save
+ TodosMailer.todo_email(@todo).deliver_now
render json: todo
else
render json: todo.errors, status: 422
diff --git a/app/mailers/todo_mailer.rb b/app/mailers/todo_mailer.rb
new file mode 100644
index 0000000..b1313b3
--- /dev/null
+++ b/app/mailers/todo_mailer.rb
@@ -0,0 +1,9 @@
+class TodoMailer < ApplicationMailer
+ default from: "DevTrackerW18@gmail.com"
+
+ def todo_email(user, todo)
+ @todo = todo
+ @url = "https://dev-tracker19.herokuapp.com/login"
+ mail(to: @user.email, subject: "Todo was added.")
+ end
+end
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 71eae7a..854077a 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -1,8 +1,8 @@
class UserMailer < ApplicationMailer
- default from: "notifications@example.com"
+ default from: "DevTrackerW18@gmail.com"
def welcome_email(user)
- @user = params[:user]
+ @user = user
@url = "https://dev-tracker19.herokuapp.com/login"
mail(to: @user.email, subject: "Welcome to DevTracker!")
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 8b15e65..a988d8e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -4,13 +4,14 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
+ :recoverable, :rememberable, :trackable, :validatable, :confirmable
include DeviseTokenAuth::Concerns::User
has_many :contacts, dependent: :destroy
has_many :applications, dependent: :destroy
has_many :companies, through: :applications
has_many :todos, dependent: :destroy
+
after_create do
- UserMailer.welcome_email(@user).deliver_now
+ UserMailer.welcome_email(self).deliver_now
end
end
diff --git a/app/views/user_mailer/todos_email.html.erb b/app/views/user_mailer/todos_email.html.erb
new file mode 100644
index 0000000..8ddb525
--- /dev/null
+++ b/app/views/user_mailer/todos_email.html.erb
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+ You created a new Todo, <%= @user.email %>
+ Have a great day!
+
+
\ No newline at end of file
diff --git a/app/views/user_mailer/todos_email.text.erb b/app/views/user_mailer/todos_email.text.erb
new file mode 100644
index 0000000..970b3a1
--- /dev/null
+++ b/app/views/user_mailer/todos_email.text.erb
@@ -0,0 +1,3 @@
+You created a new Todo, <%= @user.email %>
+===============================================
+Have a great day!
\ No newline at end of file
diff --git a/app/views/user_mailer/welcome_email.html.erb b/app/views/user_mailer/welcome_email.html.erb
index 8832fca..782a684 100644
--- a/app/views/user_mailer/welcome_email.html.erb
+++ b/app/views/user_mailer/welcome_email.html.erb
@@ -10,7 +10,7 @@
your username is: <%= @user.email %>.
- To login to the site, just follow this link: <%= @https://dev-tracker19.herokuapp.com/login %>.
+ To login to the site, just follow this link: https://dev-tracker19.herokuapp.com/login
Thanks for joining and have a great day!