Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit 0ee30bb

Browse files
committed
Merge pull request #1529 from YaleSTC/1524_contact_form_55
[1524] Resolve issues with contact form
2 parents 1e3ba07 + b6ff50b commit 0ee30bb

10 files changed

+78
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Terms of Service checkbox correctly determines if editable ([#1497](https://github.com/YaleSTC/reservations/issues/1497)).
1111
* Fixed some strange behavior with the availability calendar ([#1498](https://github.com/YaleSTC/reservations/issues/1498)).
1212
* Directly accessing /reservations/new now redirects to root_path ([#1502](https://github.com/YaleSTC/reservations/issues/1502)).
13+
* The contact form now correctly falls back to the admin e-mail if no separate e-mail is set ([#1524](https://github.com/YaleSTC/reservations/issues/1524)).
1314

1415
### Changed
1516
* Reservations now defaults to deleting missed reservations after one week ([#1491](https://github.com/YaleSTC/reservations/issues/1491)).

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ group :development, :test do
5555
gem 'pry-byebug', '~> 3.0.1'
5656
gem 'pry-stack_explorer', '~> 0.4.9.2'
5757
gem 'pry-remote', '~> 0.1.8'
58-
gem 'letter_opener', '~> 1.3.0'
58+
gem 'letter_opener', '~> 1.4.1'
5959
gem 'letter_opener_web', '~> 1.3.0'
6060
gem 'factory_girl_rails', '~> 4.5.0'
6161
gem 'rspec-rails', '~> 3.2.0'

Gemfile.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ GEM
2727
minitest (~> 5.1)
2828
thread_safe (~> 0.1)
2929
tzinfo (~> 1.1)
30-
addressable (2.3.7)
30+
addressable (2.3.8)
3131
arel (5.0.1.20140414130214)
3232
ast (2.0.0)
3333
astrolabe (1.3.0)
@@ -183,7 +183,7 @@ GEM
183183
kgio (2.9.3)
184184
launchy (2.4.3)
185185
addressable (~> 2.3)
186-
letter_opener (1.3.0)
186+
letter_opener (1.4.1)
187187
launchy (~> 2.2)
188188
letter_opener_web (1.3.0)
189189
actionmailer (>= 3.2)
@@ -446,7 +446,7 @@ DEPENDENCIES
446446
jquery-rails (~> 3.1.2)
447447
jquery-ui-rails (~> 5.0.3)
448448
kaminari (~> 0.16.3)
449-
letter_opener (~> 1.3.0)
449+
letter_opener (~> 1.4.1)
450450
letter_opener_web (~> 1.3.0)
451451
momentjs-rails (~> 2.10.6)
452452
mysql2 (~> 0.3.18)

app/mailers/notifications_mailer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class NotificationsMailer < ActionMailer::Base
22
def new_message(message)
33
@message = message
4-
mail(to: AppConfig.get(:contact_link_location),
4+
mail(to: AppConfig.contact_email,
55
subject: "[#{AppConfig.get(:site_title)}] #{message.subject}",
66
from: @message.email)
77
end

app/models/app_config.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class AppConfig < ActiveRecord::Base
1010
validates :site_title, presence: true,
1111
length: { maximum: 20 }
1212
validates :admin_email,
13+
presence: true,
14+
format: { with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i }
15+
validates :contact_link_location,
16+
allow_blank: true,
1317
format: { with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i }
1418
validates :default_per_cat_page, numericality: { only_integer: true }
1519

@@ -23,6 +27,17 @@ def self.check(prop, val = false)
2327

2428
def self.get(prop, val = false)
2529
# alias for semantics
26-
AppConfig.check(prop, val)
30+
check(prop, val)
31+
end
32+
33+
# Returns the to e-mail for contact form submissions, defaulting to the admin
34+
# e-mail address if no separate address is specified
35+
def self.contact_email
36+
contact_email = get(:contact_link_location, nil)
37+
if contact_email.nil? || contact_email.empty?
38+
get(:admin_email, nil)
39+
else
40+
contact_email
41+
end
2742
end
2843
end

db/seeds.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def generate_objs(method, obj, n)
394394
ac.notify_admin_on_create = false
395395
ac.admin_email = '[email protected]'
396396
ac.department_name = 'Department'
397-
ac.contact_link_location = 'link'
397+
ac.contact_link_location = '[email protected]'
398398
ac.home_link_text = 'home_link'
399399
ac.home_link_location = 'Canada'
400400
ac.deleted_missed_reservation_email_body = DELETED_MISSED_RES_EMAIL

lib/tasks/setup_application.rake

+1-5
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ namespace :app do
145145
home_link_location = STDIN.gets.chomp
146146
puts 'Contact Email (this email address will receive contact form '\
147147
'submissions). Leave blank to default to the admin e-mail.'
148-
if STDIN.gets.chomp.empty?
149-
contact_link_location = admin_email
150-
else
151-
contact_link_location = STDIN.gets.chomp
152-
end
148+
contact_link_location = STDIN.gets.chomp unless STDIN.gets.chomp.empty?
153149

154150
ActiveRecord::Base.transaction do
155151
begin

spec/controllers/contact_controller_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@
4646
end
4747
end
4848
end
49+
context 'with contact e-mail set' do
50+
before do
51+
AppConfig.first
52+
.update_attributes(contact_link_location: '[email protected]')
53+
post :create, message: FactoryGirl.attributes_for(:message)
54+
end
55+
56+
it 'sends the message to the contact address' do
57+
expect(ActionMailer::Base.deliveries.last.to).to\
58+
include('[email protected]')
59+
end
60+
end
61+
context 'with contact e-mail not set' do
62+
before do
63+
AppConfig.first.update_attributes(contact_link_location: '')
64+
post :create, message: FactoryGirl.attributes_for(:message)
65+
end
66+
67+
it 'sends the message to the admin address' do
68+
expect(ActionMailer::Base.deliveries.last.to).to\
69+
include(AppConfig.first.admin_email)
70+
end
71+
end
4972
after(:all) do
5073
@app_config.destroy
5174
end

spec/factories/app_configs.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
site_title 'Reservations Specs'
99
admin_email '[email protected]'
1010
department_name 'MyString'
11-
contact_link_location 'MyString'
11+
contact_link_location '[email protected]'
1212
home_link_text 'MyString'
1313
home_link_location 'MyString'
1414
default_per_cat_page 1

spec/models/app_config_spec.rb

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require 'spec_helper'
22

33
describe AppConfig, type: :model do
4-
before(:each) do
5-
@ac = FactoryGirl.build(:app_config)
6-
end
4+
before(:all) { AppConfig.delete_all }
5+
before(:each) { @ac = FactoryGirl.build(:app_config) }
6+
77
it 'has a working factory' do
88
expect(@ac.save).to be_truthy
99
end
@@ -28,6 +28,13 @@
2828
@ac.admin_email = '[email protected]'
2929
expect(@ac).to be_valid
3030
end
31+
it "shouldn't have an invalid contact e-mail" do
32+
emails = ['ana@com', 'anda@pres,com']
33+
emails.each do |invalid|
34+
@ac.contact_link_location = invalid
35+
expect(@ac).not_to be_valid
36+
end
37+
end
3138
# it "has an attachment that could serve as favicon" do
3239
# @ac.favicon_file_name = "icon.ico"
3340
# @ac.should be_valid
@@ -44,4 +51,24 @@
4451
end
4552
@ac.favicon_content_type = 'image/vnd.microsoft.icon'
4653
end
54+
55+
context '.contact_email' do
56+
it 'returns the contact e-mail if it is set' do
57+
@ac.contact_link_location = '[email protected]'
58+
@ac.save
59+
60+
expect(AppConfig.contact_email).to eq('[email protected]')
61+
62+
AppConfig.delete_all
63+
end
64+
65+
it 'returns the admin e-mail if no contact e-mail is set' do
66+
@ac.contact_link_location = ''
67+
@ac.save
68+
69+
expect(AppConfig.contact_email).to eq(AppConfig.check(:admin_email))
70+
71+
AppConfig.delete_all
72+
end
73+
end
4774
end

0 commit comments

Comments
 (0)