Skip to content

Commit 6b07d57

Browse files
committed
emails as delayed jobs
1 parent c79b2ac commit 6b07d57

File tree

13 files changed

+98
-3
lines changed

13 files changed

+98
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gem 'bcrypt-ruby'
1111
gem 'dalli'
1212
gem 'sass-rails'
1313
gem 'pry'
14+
gem 'delayed_job_active_record'
1415

1516
gem 'bourbon'
1617
gem 'neat'

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ GEM
6767
rails (>= 3, < 5)
6868
dalli (2.7.1)
6969
database_cleaner (1.2.0)
70+
delayed_job (4.0.2)
71+
activesupport (>= 3.0, < 4.2)
72+
delayed_job_active_record (4.0.1)
73+
activerecord (>= 3.0, < 4.2)
74+
delayed_job (>= 3.0, < 4.1)
7075
diff-lcs (1.2.5)
7176
email_validator (1.4.0)
7277
activemodel
@@ -236,6 +241,7 @@ DEPENDENCIES
236241
cucumber-rails
237242
dalli
238243
database_cleaner
244+
delayed_job_active_record
239245
email_validator
240246
factory_girl_rails
241247
font-awesome-sass

Procfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
web: bundle exec unicorn -c ./config/unicorn.rb
1+
web: bundle exec unicorn -c ./config/unicorn.rb
2+
worker: bundle exec rake jobs:work

app/controllers/contact_requests_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ContactRequestsController < ApplicationController
44
def create
55
@contact_request = ContactRequest.new(contact_params)
66
if @contact_request.save
7-
ContactMailer.contact_request(@contact_request)
7+
ContactRequest.delay.send_notification_email(@contact_request.id)
88
render json: @contact_request, status: :created
99
else
1010
render json: @contact_request.errors, status: :unprocessable_entity

app/models/contact_request.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class ContactRequest < ActiveRecord::Base
22
validates_presence_of :name, :message
33
validates :email, presence: true, email: true
4+
5+
def self.send_notification_email(contact_request_id)
6+
contact_request = find(contact_request_id)
7+
ContactMailer.contact_request(contact_request)
8+
end
49
end

bin/delayed_job

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4+
require 'delayed/command'
5+
Delayed::Command.new(ARGV).daemonize

config/initializers/delayed_job.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Delayed::Worker.destroy_failed_jobs = false
2+
Delayed::Worker.max_attempts = 1
3+
Delayed::Worker.delay_jobs = !(Rails.env.test? || Rails.env.development?)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CreateDelayedJobs < ActiveRecord::Migration
2+
def self.up
3+
create_table :delayed_jobs, :force => true do |table|
4+
table.integer :priority, :default => 0, :null => false # Allows some jobs to jump to the front of the queue
5+
table.integer :attempts, :default => 0, :null => false # Provides for retries, but still fail eventually.
6+
table.text :handler, :null => false # YAML-encoded string of the object that will do work
7+
table.text :last_error # reason for last failure (See Note below)
8+
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
9+
table.datetime :locked_at # Set when a client is working on this object
10+
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
11+
table.string :locked_by # Who is working on this object (if locked)
12+
table.string :queue # The name of the queue this job is in
13+
table.timestamps
14+
end
15+
16+
add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
17+
end
18+
19+
def self.down
20+
drop_table :delayed_jobs
21+
end
22+
end

db/schema.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20140702212029) do
14+
ActiveRecord::Schema.define(version: 20140813185505) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -39,6 +39,22 @@
3939
t.boolean "analytics"
4040
end
4141

42+
create_table "delayed_jobs", force: true do |t|
43+
t.integer "priority", default: 0, null: false
44+
t.integer "attempts", default: 0, null: false
45+
t.text "handler", null: false
46+
t.text "last_error"
47+
t.datetime "run_at"
48+
t.datetime "locked_at"
49+
t.datetime "failed_at"
50+
t.string "locked_by"
51+
t.string "queue"
52+
t.datetime "created_at"
53+
t.datetime "updated_at"
54+
end
55+
56+
add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
57+
4258
create_table "employees", force: true do |t|
4359
t.string "name"
4460
t.string "email"

features/contact_request.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Feature: Contact Request Form
2+
3+
@javascript
4+
Scenario: Filling out the contact form as a potential client
5+
Given I am on the homepage
6+
When I submit the contact request form correctly
7+
Then I should see a confirmation message
8+
And there should be mail delivered
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Given(/^I am on the homepage$/) do
2+
visit root_path
3+
end
4+
5+
When(/^I submit the contact request form correctly$/) do
6+
fill_in "Name", with: "Mando"
7+
fill_in "Email", with: "[email protected]"
8+
fill_in "Phone Number", with: "1234123412"
9+
fill_in "What can we help you with?", with: "Help!"
10+
click_on "Send Request!"
11+
end
12+
13+
Then(/^I should see a confirmation message$/) do
14+
expect(page).to have_content("We got the message.")
15+
end
16+
17+
Then(/^there should be mail delivered$/) do
18+
expect(ActionMailer::Base.deliveries.count).to eq(1)
19+
end

features/support/mailers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
After { ActionMailer::Base.deliveries.clear }

spec/models/contact_request_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@
1111
end
1212

1313
end
14+
15+
describe ".send_mail_notification_email" do
16+
it "sends the contact request info in an email" do
17+
expect(ContactMailer).to receive(:contact_request)
18+
request = ContactRequest.create(name: "Armando", email: "[email protected]", phone: "123-123-1234", message: "HELP!")
19+
ContactRequest.send_notification_email(request.id)
20+
end
21+
end
1422
end

0 commit comments

Comments
 (0)