Skip to content

Commit

Permalink
Mailer for on-boarding newcomer (#3025)
Browse files Browse the repository at this point in the history
* new helper function for node

* mailer created

* name typo

* fixtures

* test added

* clean up
  • Loading branch information
grvsachdeva authored and jywarren committed Jul 10, 2018
1 parent 85a277a commit 2fd9e45
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def create
flash[:warning] = I18n.t('users_controller.account_migrated_create_new_password')
redirect_to "/profile/edit"
else
WelcomeMailer.notify_newcomer(@user).deliver_now
flash[:notice] = I18n.t('users_controller.registration_successful').html_safe
flash[:warning] = I18n.t('users_controller.spectralworkbench_or_mapknitter', :url1 => "'#{session[:openid_return_to]}'").html_safe if session[:openid_return_to]
session[:openid_return_to] = nil
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def feature(title)
end
end

def feature_node(title)
features = Node.where(type: 'feature', title: title)
if !features.empty?
return features.last
else
''
end
end

def locale_name_pairs
I18n.available_locales.map do |locale|
[I18n.t('language', locale: locale), locale]
Expand Down
10 changes: 9 additions & 1 deletion app/mailers/welcome_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class WelcomeMailer < ActionMailer::Base
helper :application
include ApplicationHelper
# default from: "notifications@#{ActionMailer::Base.default_url_options[:host]}"
default from: "notifications@#{ActionMailer::Base.default_url_options[:host]}"

# PasswordResetMailer.reset_notify(user).deliver_now
def add_to_list(user, list)
Expand All @@ -10,4 +10,12 @@ def add_to_list(user, list)
@footer = feature('email-footer')
mail(to: list + '[email protected]', subject: subject, from: user.email)
end

def notify_newcomer(user)
subject = 'Welcome to Public Lab'
@user = user
@footer = feature('email-footer')
@body = feature_node('welcome-email-body')
mail(to: user.email, subject: subject)
end
end
4 changes: 4 additions & 0 deletions app/views/welcome_mailer/notify_newcomer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<%= raw auto_link(@body.latest.render_body_email(ActionMailer::Base.default_url_options[:host]), sanitize: false) %>

<%= @footer %>
10 changes: 10 additions & 0 deletions test/fixtures/nodes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,13 @@ draft:
type: "note"
cached_likes: 0
slug: test-<%= Time.now.strftime("%m-%d-%Y") %>-draft-note token:briRpnTHzKHepo6-j8-b5w

welcome_feature:
nid: 22
uid: 1
title: "welcome-email-body"
created: <%= Time.now.to_i %>
changed: <%= Time.now.to_i %>
status: 1
type: "feature"
cached_likes: 0
7 changes: 7 additions & 0 deletions test/fixtures/revisions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ checkbox_two:
# Heading 1
List of things
* [x]
email_body:
nid: 22
uid: 1
title: "welcome-email-body"
body: Welcome email body
timestamp: <%= (Time.now - 1.week).to_i %>
20 changes: 20 additions & 0 deletions test/unit/welcome_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'test_helper'

class WelcomeMailerTest < ActionMailer::TestCase

test 'notify newcomer with welcome email' do
user = users(:bob)
@body = nodes(:welcome_feature)

assert_difference 'ActionMailer::Base.deliveries.size', 1 do
WelcomeMailer.notify_newcomer(user).deliver_now
end
assert !ActionMailer::Base.deliveries.empty?

email = ActionMailer::Base.deliveries.last
assert_equal ["notifications@#{request_host}"], email.from
assert_equal [user.email], email.to
assert_equal "Welcome to Public Lab", email.subject
assert email.body.include?("Welcome email body")
end
end

0 comments on commit 2fd9e45

Please sign in to comment.