Skip to content

Commit

Permalink
Set up ActionMailer; send mail to admin when new event is created.
Browse files Browse the repository at this point in the history
  • Loading branch information
moveson committed Feb 13, 2018
1 parent b2f05ff commit 72b4e86
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ FULL_URI=http://localhost:3000
# Use anything you want for DURABLE_JWT_CODE (but it needs to be not nil)

DURABLE_JWT_CODE=durable-jwt

# Insert your own email for ADMIN_EMAIL

ADMIN_EMAIL=your.email@example.com
1 change: 1 addition & 0 deletions app/controllers/api/v1/staging_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def post_event_course_org
setter = EventCourseOrgSetter.new(event: event, event_group: event_group, course: course, organization: organization, params: params)
setter.set_resources
if setter.status == :ok
AdminMailer.new_event(event, current_user).deliver_later if params[:id] == 'new'
render json: setter.resources.map { |resource| [resource.class.to_s.underscore, resource] }.to_h, status: setter.status
else
render json: {errors: setter.resources.map { |resource| jsonapi_error_object(resource) }}, status: setter.status
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def destroy
authorize @event
@event.destroy

redirect_to events_path
redirect_to event_groups_path
end

# Special views with results
Expand Down
7 changes: 7 additions & 0 deletions app/mailers/admin_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AdminMailer < ApplicationMailer
def new_event(event, user)
@event = event
@user = user
mail(to: ENV['ADMIN_EMAIL'], subject: "New event: #{@event.name}")
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "no-reply@#{ENV['BASE_URI']}"
# layout 'mailer'
layout 'mailer'
end
4 changes: 4 additions & 0 deletions app/views/admin_mailer/new_event.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>
<%= "#{@user.full_name} has created a new event called" %>
<%= link_to @event.name, event_url(@event) %>
</p>
1 change: 1 addition & 0 deletions app/views/admin_mailer/new_event.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= "#{@user.full_name} has created a new event called #{@event.name}." %>
13 changes: 13 additions & 0 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
</head>

<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>
15 changes: 0 additions & 15 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,3 @@

# Initialize the Rails application.
Rails.application.initialize!

Time::DATE_FORMATS[:submitted] = "%b %d %Y"

Time::DATE_FORMATS[:time_short] = ""
Time::DATE_FORMATS[:time_long] = ""

ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
10 changes: 10 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = {host: ENV['BASE_URI'], protocol: 'http'}

config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
authentication: :plain,
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
domain: 'heroku.com',
enable_starttls_auto: true
}

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
Expand Down
38 changes: 37 additions & 1 deletion spec/controllers/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@

context 'when provided with a file' do
let(:request_params) { {id: event.id, data_format: 'csv_efforts', file: file} }
let(:file) { file_fixture('test_efforts.csv') } # Should work in Rails 5
let(:file) { file_fixture('test_efforts.csv') }

it 'returns a successful json response' do
skip 'Until Rails 5 upgrade'
Expand Down Expand Up @@ -344,6 +344,42 @@
end
end

context 'when provided with multiple live_times relating the same bib number in a multi-lap event' do
let(:data) {
[{'type' => 'live_time',
'attributes' =>
{'source' => source,
'sub_split_kind' => 'in',
'with_pacer' => 'false',
'bib_number' => efforts.first.bib_number,
'split_id' => splits.first.id,
'stopped_here' => 'false',
'absolute_time' => '2018-02-10 11:13:17-7:00'}},
{'type' => 'live_time',
'attributes' =>
{'source' => source,
'sub_split_kind' => 'in',
'with_pacer' => 'false',
'bib_number' => efforts.second.bib_number,
'split_id' => splits.first.id,
'stopped_here' => 'false',
'absolute_time' => '2018-02-10 09:16:07-7:00'}},
{'type' => 'live_time',
'attributes' =>
{'source' => source,
'sub_split_kind' => 'in',
'with_pacer' => 'false',
'bib_number' => efforts.first.bib_number,
'split_id' => splits.first.id,
'stopped_here' => 'false',
'absolute_time' => '2018-02-10 09:21:54-7:00'}}]
}

it 'imports data in the expected order' do
skip
end
end

context 'when provided with an adilas url and data_format adilas_bear_times' do
let(:request_params) { {id: event.id, data_format: 'adilas_bear_times', data: source_data} }
let(:source_data) { Net::HTTP.get(URI(url)) }
Expand Down
32 changes: 32 additions & 0 deletions spec/mailers/admin_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "rails_helper"

RSpec.describe AdminMailer, type: :mailer do
include ActiveJob::TestHelper
let(:user){ create(:user) }
let(:event) { create(:event) }

it 'creates a job' do
ActiveJob::Base.queue_adapter = :test
expect { AdminMailer.new_event(event, user).deliver_later }.to have_enqueued_job.on_queue('mailers')
end

# Because we have config.action_mailer.delivery_method set to :test in our :test.rb,
# all 'sent' emails are gathered into the ActionMailer::Base.deliveries array.

it 'sends an email' do
expect {
perform_enqueued_jobs do
AdminMailer.new_event(event, user).deliver_later
end
}.to change { ActionMailer::Base.deliveries.size }.by(1)
end

it 'sends email to the correct admin' do
perform_enqueued_jobs do
AdminMailer.new_event(event, user).deliver_later
end

mail = ActionMailer::Base.deliveries.last
expect(mail.to[0]).to eq ENV['ADMIN_EMAIL']
end
end
8 changes: 8 additions & 0 deletions spec/mailers/previews/admin_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Preview all emails at http://localhost:3000/rails/mailers/admin_mailer
class AdminMailerPreview < ActionMailer::Preview
def new_event
event = Event.first
user = User.first
AdminMailer.new_event(event, user)
end
end

0 comments on commit 72b4e86

Please sign in to comment.