Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Commit

Permalink
Merge branch 'email_controller'
Browse files Browse the repository at this point in the history
* email_controller:
  Remove static mailer templates
  Abstract email count
  Professor dynamic email
  Dynamically build email
  Add email description for ASC
  Add description to emails
  Only create emails if they're not already in the database
  Database email seeds
  Cleanup scaffold
  Scaffold dynamic emails controller
  Email professors on email change
  • Loading branch information
keith committed Apr 15, 2014
2 parents 6d249d6 + dd1ba1e commit db2d4c0
Show file tree
Hide file tree
Showing 30 changed files with 400 additions and 57 deletions.
2 changes: 2 additions & 0 deletions app/assets/javascripts/emails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/emails.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the emails controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
35 changes: 35 additions & 0 deletions app/controllers/emails_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class EmailsController < ApplicationController
before_action :signed_in_admin
before_action :set_email, only: [:show, :edit, :update, :destroy]

# GET /emails
def index
@emails = Email.all
end

# GET /emails/1
def show
end

# GET /emails/1/edit
def edit
end

# PATCH/PUT /emails/1
def update
if @email.update(email_params)
redirect_to @email, notice: 'Email was successfully updated.'
else
render action: 'edit'
end
end

private
def set_email
@email = Email.find(params[:id])
end

def email_params
params.require(:email).permit(:body, :subject, :reply_to)
end
end
22 changes: 17 additions & 5 deletions app/mailers/applicant_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
class ApplicantMailer < ActionMailer::Base
# TODO: Fix these email from addresses
default from: 'from@example.com',
reply_to: 'foo@example.com'
default from: 'from@example.com'
# reply_to: 'foo@example.com'

def account_email(user)
@count = user.positions.count
email_identifier = self.class.name + ':' + __method__.to_s
email_template = Email.find_by_identifier(email_identifier)
return unless email_template

# @count = user.positions.count
@name = user.name
@link = applicant_url(user)
@url = applicant_url(user)

body = email_template.body.dup
body.gsub!(/\[NAME\]/i, @name)
body.gsub!(/\[URL\]/i, @url)
email = user.email + EMAIL_SUFFIX
mail to: email, subject: 'Your ASC tutoring application'
mail(to: email,
body: body,
content_type: 'text/plain',
subject: email_template.subject,
reply_to: email_template.reply_to)
end
end
22 changes: 17 additions & 5 deletions app/mailers/professor_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
class ProfessorMailer < ActionMailer::Base
# TODO: Fix these email from addresses
default from: 'from@example.com',
reply_to: 'foo@example.com'
default from: 'from@example.com'
# reply_to: 'foo@example.com'

def pending_recommendation(professor)
@count = professor.positions.count
email_identifier = self.class.name + ':' + __method__.to_s
email_template = Email.find_by_identifier(email_identifier)
return unless email_template

# @count = user.positions.count
@name = professor.name
@link = professor_url(professor)
@url = professor_url(professor)

body = email_template.body.dup
body.gsub!(/\[NAME\]/i, @name)
body.gsub!(/\[URL\]/i, @url)
email = professor.email + EMAIL_SUFFIX
mail to: email, subject: 'Pending ASC tutoring recommendation'
mail(to: email,
body: body,
content_type: 'text/plain',
subject: email_template.subject,
reply_to: email_template.reply_to)
end
end
2 changes: 2 additions & 0 deletions app/models/email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Email < ActiveRecord::Base
end
2 changes: 1 addition & 1 deletion app/models/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Position < ActiveRecord::Base
belongs_to :applicant
belongs_to :course
belongs_to :professor
accepts_nested_attributes_for :professor
accepts_nested_attributes_for :professor

before_create { self.identifier = new_positions_identifier }
before_create { self.application_status = 0 }
Expand Down
8 changes: 7 additions & 1 deletion app/models/professor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Professor < ActiveRecord::Base
EMAIL_REGEX = /\A\w+\z/

before_create { self.identifier = new_professor_identifier }
before_save { email.downcase! }
before_save { email.downcase!; email_changed }
has_many :positions
validate :static_identifier, on: :update
validates_presence_of :name
Expand All @@ -20,4 +20,10 @@ def to_param
def static_identifier
errors[:identifier] = "can't be changed" if self.identifier_changed?
end

def email_changed
return unless self.persisted?
return if self.email.downcase == self.email_was.downcase
ProfessorMailer.pending_recommendation(self).deliver
end
end
17 changes: 0 additions & 17 deletions app/views/applicant_mailer/account_email.html.erb

This file was deleted.

13 changes: 0 additions & 13 deletions app/views/applicant_mailer/account_email.text.erb

This file was deleted.

29 changes: 29 additions & 0 deletions app/views/emails/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_for(@email) do |f| %>
<% if @email.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@email.errors.count, "error") %> prohibited this email from being saved:</h2>

<ul>
<% @email.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :reply_to %><br>
<%= f.text_field :reply_to %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/emails/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing email</h1>

<%= render 'form' %>
<%= link_to 'Show', @email %> |
<%= link_to 'Back', emails_path %>
28 changes: 28 additions & 0 deletions app/views/emails/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Listing emails</h1>

<table>
<thead>
<tr>
<th>Description</th>
<th>Body</th>
<th>Subject</th>
<th>Reply to</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @emails.each do |email| %>
<tr>
<td><%= email.description %></td>
<td><%= email.body %></td>
<td><%= email.subject %></td>
<td><%= email.reply_to %></td>
<td><%= link_to 'Show', email %></td>
<td><%= link_to 'Edit', edit_email_path(email) %></td>
</tr>
<% end %>
</tbody>
</table>
19 changes: 19 additions & 0 deletions app/views/emails/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Body:</strong>
<%= @email.body %>
</p>

<p>
<strong>Subject:</strong>
<%= @email.subject %>
</p>

<p>
<strong>Reply to:</strong>
<%= @email.reply_to %>
</p>

<%= link_to 'Edit', edit_email_path(@email) %> |
<%= link_to 'Back', emails_path %>
8 changes: 0 additions & 8 deletions app/views/professor_mailer/pending_recommendation.html.erb

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/professor_mailer/pending_recommendation.text.erb

This file was deleted.

3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = {
host: 'localhost:3000'
}

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Asc476::Application.routes.draw do
root 'applicants#new'
resources :emails, except: [:new, :create, :destroy]
resources :positions, except: [:new]
resources :professors, except: [:create, :new]

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20140415173906_create_emails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateEmails < ActiveRecord::Migration
def change
create_table :emails do |t|
t.text :body
t.string :subject
t.string :reply_to
t.string :identifier

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20140415181431_add_description_to_emails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDescriptionToEmails < ActiveRecord::Migration
def change
add_column :emails, :description, :string
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140405151958) do
ActiveRecord::Schema.define(version: 20140415181431) do

create_table "applicants", force: true do |t|
t.string "name"
Expand Down Expand Up @@ -42,6 +42,16 @@
t.boolean "disabled"
end

create_table "emails", force: true do |t|
t.text "body"
t.string "subject"
t.string "reply_to"
t.string "identifier"
t.datetime "created_at"
t.datetime "updated_at"
t.string "description"
end

create_table "positions", force: true do |t|
t.text "professor_comments"
t.boolean "professor_verdict"
Expand Down
37 changes: 37 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,40 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
email_count = 2
reply_to = 'mahona@winthrop.edu'
professor_email = <<END
[NAME],
There are recommendations for ASC tutoring positions awaiting your review.
Please fill out the recommendations here: [URL]
Thank you,
ASC Staff
END
Email.create(body: professor_email,
subject: 'Pending ASC tutoring recommendations',
reply_to: reply_to,
identifier: 'ProfessorMailer:pending_recommendation',
description: 'Email sent to professors for pending recommendations',
id: 1) unless Email.count == email_count

applicant_email = <<END
[NAME],
You can view or edit your application by going here: [URL]
Thank you for submitting your application to be a Peer Tutor at the Academic
Success Center. The professors listed in your application has been notified
of your request for their recommendation. Your application is currently pending
and will be ready for review by the ASC once your faculty
recommendation has been submitted.
Thank you, ASC Staff
END
Email.create(body: applicant_email,
subject: 'Your ASC tutoring application',
reply_to: reply_to,
identifier: 'ApplicantMailer:account_email',
description: 'Email sent for new applicants',
id: 2) unless Email.count == email_count
5 changes: 5 additions & 0 deletions spec/models/email_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Email do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit db2d4c0

Please sign in to comment.