Skip to content

Commit

Permalink
Merge pull request #30 from cs169/187044314-add-to-field
Browse files Browse the repository at this point in the history
187044314 add to field
  • Loading branch information
perryzjc authored Feb 27, 2024
2 parents 325fb70 + ac57d48 commit d9d096d
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 12 deletions.
15 changes: 10 additions & 5 deletions app/controllers/email_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ def edit
def update
template = EmailTemplate.find(params[:id])
template.update(template_params)
template.save!
redirect_to email_templates_path
if template.save
flash[:success] = "Updated #{template.title} template successfully."
redirect_to email_templates_path
else
flash[:alert] = "Failed to save #{template.title} template: " + template.errors.full_messages.join(", ")
redirect_to edit_email_template_path(params[:id])
end
end

def create
Expand All @@ -27,11 +32,11 @@ def create
end
load_ordered_email_templates

if @email_template.save!
if @email_template.save
flash[:success] = "Created #{@email_template.title} successfully."
redirect_to email_templates_path
else
flash[:alert] = "Failed to submit information :("
flash[:alert] = "Failed to submit information: " + @email_template.errors.full_messages.join(", ")
render "new"
end
end
Expand All @@ -49,7 +54,7 @@ def destroy

private
def template_params
params.require(:email_template).permit(:body, :subject, :title)
params.require(:email_template).permit(:body, :subject, :title, :to)
end

def load_ordered_email_templates
Expand Down
18 changes: 14 additions & 4 deletions app/mailers/teacher_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class TeacherMailer < ApplicationMailer
def welcome_email(teacher)
@teacher = teacher
set_body
mail to: teacher.email_name,
set_recipients
mail to: @recipients, # ActionMailer accepts comma-separated lists of emails
cc: CONTACT_EMAIL,
subject: email_template.subject
end
Expand All @@ -20,7 +21,8 @@ def deny_email(teacher, denial_reason)
@teacher = teacher
@denial_reason = denial_reason
set_body
mail to: @teacher.email_name,
set_recipients
mail to: @recipients,
cc: CONTACT_EMAIL,
subject: email_template.subject
end
Expand All @@ -29,16 +31,18 @@ def request_info_email(teacher, request_reason)
@teacher = teacher
@request_reason = request_reason
set_body
mail to: @teacher.email_name,
set_recipients
mail to: @recipients,
cc: CONTACT_EMAIL,
subject: email_template.subject
end

def form_submission(teacher)
@teacher = teacher
set_body
set_recipients
if @teacher.not_reviewed?
mail to: CONTACT_EMAIL,
mail to: @recipients,
subject: email_template.subject
end
end
Expand All @@ -60,7 +64,13 @@ def email_template
@email_template ||= EmailTemplate.find_by(title: action_name.titlecase)
end

# renders the email body with the {{parameter}} things
def set_body
@body = Liquid::Template.parse(email_template.body).render(liquid_assigns).html_safe
end

# renders the list of recipients with the {{parameter}} things
def set_recipients
@recipients = Liquid::Template.parse(email_template.to).render(liquid_assigns).html_safe
end
end
4 changes: 3 additions & 1 deletion app/models/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
# required :boolean default(FALSE)
# subject :string
# title :string
# to :string
# created_at :datetime not null
# updated_at :datetime not null
#
class EmailTemplate < ApplicationRecord
validates :title,
inclusion: TeacherMailer.instance_methods(false).map { |method| method.to_s.titlecase },
if: -> { self.required? }
validates :body, presence: true
validates :body, presence: { message: "cannot be blank" }
validates :to, presence: { message: "cannot be blank" }

before_destroy :prevent_deleting_required_emails

Expand Down
5 changes: 4 additions & 1 deletion app/views/email_templates/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<%# This part of the page is the email template form %>
<%= form_with(model: email_template, local: true) do |form| %>
<div class="form">
<label for="email_template_title"><h3>Title</h3></label>
<%= form.text_field :title, class: "form-control", readonly: email_template.required? %>
<label for="email_template_to"><h3>To</h3></label>
<%= form.text_field :to, class: "form-control" %>
<label for="email_template_subject"><h3>Subject</h3></label>
<%= form.text_field :subject, class: "form-control" %>
<label for="email_template_body"><h3>Body</h3></label>
Expand All @@ -12,7 +15,7 @@
<%= form.submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>

<%# This renders the section on "Allowed Tags" %>
<%= render 'liquid_fields' %>

<script type="text/javascript">
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20240218005604_add_to_field_to_email_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddToFieldToEmailTemplate < ActiveRecord::Migration[6.1]
def change
add_column :email_templates, :to, :string
#below is the default prepopulated 'to' field value,
to_field = "{{teacher_email}}, {{teacher_personal_email}}"
EmailTemplate.update_all(to: to_field)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeToFieldForFormSubmission < ActiveRecord::Migration[6.1]
def change
EmailTemplate.find_by(path: "teacher_mailer/form_submission").update(to: "[email protected], [email protected]")
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_09_05_181019) do
ActiveRecord::Schema.define(version: 2024_02_20_192015) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -65,6 +65,7 @@
t.string "title"
t.string "subject"
t.boolean "required", default: false
t.string "to"
end

create_table "pages", force: :cascade do |t|
Expand Down
6 changes: 6 additions & 0 deletions db/seed_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ module SeedData
REQUEST_INFO_EMAIL


@default_to_field = "{{teacher_email}}, {{teacher_personal_email}}"

def self.emails
[
{
to: @default_to_field,
body: @welcome_email,
path: "teacher_mailer/welcome_email",
locale: nil,
Expand All @@ -96,6 +99,7 @@ def self.emails
subject: "Welcome to The Beauty and Joy of Computing!"
},
{
to: "[email protected], [email protected]",
body: @form_submission,
path: "teacher_mailer/form_submission",
locale: nil,
Expand All @@ -107,6 +111,7 @@ def self.emails
subject: "Form Submission"
},
{
to: @default_to_field,
body: @deny_email,
path: "teacher_mailer/deny_email",
locale: nil,
Expand All @@ -119,6 +124,7 @@ def self.emails
},
{
body: @request_info_email,
to: @default_to_field,
path: "teacher_mailer/request_info_email",
locale: nil,
handler: "liquid",
Expand Down
30 changes: 30 additions & 0 deletions features/email_template.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,33 @@ Scenario: Logging in as an admin should be able to edit email subject
And I press "Submit"
And I follow "Welcome Email"
Then the "email_template_subject" field should contain "Test Subject"

Scenario: Creating new email template with blank fields displays flash error
Given I am on the email templates index
And I press "New Email Templates"
When I press "Submit"
Then I should see "Failed to submit information: Body cannot be blank, To cannot be blank"

Scenario: Creating and deleting new email template with valid fields succeeds
Given I am on the email templates index
And I press "New Email Templates"
When I fill in "email_template_title" with "Test Email"
And I fill in "email_template_subject" with "Test Subject"
And I fill in "email_template_to" with "{{teacher_email}}"
And I fill in TinyMCE email form with "This is the body of my test email"
And I press "Submit"
Then I should see "Created Test Email successfully"
When I follow the first "❌" link
And I accept the popup alert
Then I should be on the email templates index
And I should not see "Test Email"

Scenario: Editing email template to have blank body or to field displays flash error
Given I am on the email templates index
And I follow "Welcome Email"
And I fill in "email_template_to" with ""
And I press "Submit"
Then I should see "Failed to save Welcome Email template: To cannot be blank"
When I fill in TinyMCE email form with ""
And I press "Submit"
Then I should see "Failed to save Welcome Email template: Body cannot be blank"
12 changes: 12 additions & 0 deletions features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def with_scope(locator, &block)
page.execute_script('$(tinymce.editors[0].setContent("' + value + '"))')
end

When(/^(?:|I )follow the first "([^"]*)" link$/) do |link_text|
first("a", text: link_text).click
end

When(/^(?:|I )press the first "([^"]*)" button$/) do |button_text|
first("button", text: button_text).click
end

Then(/^(?:|I )accept the popup alert$/) do
page.driver.browser.switch_to.alert.accept
end

When(/^(?:|I )fill in "([^"]*)" for "([^"]*)"$/) do |value, field|
fill_in(field, with: value)
end
Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/teachers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,16 @@ reimu:
school: berkeley
application_status: Info Needed
education_level: -1

barney:
id: 6
first_name: Barney
last_name: Dinosaur
snap: barney
email: '[email protected]'
personal_email: '[email protected]'
status: 1
more_info: ''
application_status: Denied
school: berkeley
education_level: -1
11 changes: 11 additions & 0 deletions spec/mailers/teacher_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
expect(email.body.encoded).to include("Hi Bob")
end

it "Sends to Both School and Personal Email When Possible" do
teacher = teachers(:barney)
email = TeacherMailer.welcome_email(teacher)
email.deliver_now
expect(email.from[0]).to eq("[email protected]")
expect(email.to[0]).to eq("[email protected]")
expect(email.to[1]).to eq("[email protected]")
expect(email.subject).to eq("Welcome to The Beauty and Joy of Computing!")
expect(email.body.encoded).to include("Hi Barney")
end


it "Sends Deny Email" do
teacher = teachers(:long)
Expand Down

0 comments on commit d9d096d

Please sign in to comment.