-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1622 from samvera/contact_mailer
Contact page - customizable email to per tenant
- Loading branch information
Showing
15 changed files
with
273 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class ContactsController < SitesController | ||
# GET /sites/1/edit | ||
def edit | ||
add_breadcrumb t(:'hyrax.controls.home'), root_path | ||
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path | ||
add_breadcrumb t(:'hyrax.admin.sidebar.configuration'), '#' | ||
add_breadcrumb t(:'hyrax.admin.sidebar.contact'), edit_site_contact_path | ||
end | ||
|
||
# PATCH/PUT /sites/1 | ||
# PATCH/PUT /sites/1.json | ||
def update | ||
respond_to do |format| | ||
if @site.update(site_params) | ||
format.html { redirect_to edit_site_contact_path, notice: 'Contact page was successfully updated.' } | ||
else | ||
format.html { render :edit } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# Never trust parameters from the scary internet, only allow the white list through. | ||
def site_params | ||
params.require(:site).permit(:contact_email) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copied from hyrax 2.5.1 to override the mail to | ||
module Hyrax | ||
class ContactForm | ||
include ActiveModel::Model | ||
attr_accessor :contact_method, :category, :name, :email, :subject, :message | ||
validates :email, :category, :name, :subject, :message, presence: true | ||
validates :email, format: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i, allow_blank: true | ||
|
||
# - can't use this without ActiveRecord::Base | ||
# validates_inclusion_of :category, in: self.class.issue_types_for_locale | ||
|
||
# They should not have filled out the `contact_method' field. That's there to prevent spam. | ||
def spam? | ||
contact_method.present? | ||
end | ||
|
||
# Declare the e-mail headers. It accepts anything the mail method | ||
# in ActionMailer accepts. | ||
###### OVERRODE the to: field to add the Tenant's email, first | ||
def contact_email | ||
Site.contact_email.presence || Settings.contact_email_to | ||
end | ||
|
||
def headers | ||
## override hyrax 2.5.1 send the mail 'from' the submitter, which doesn't work on most smtp transports | ||
{ | ||
subject: "#{Hyrax.config.subject_prefix} #{email} #{subject}", | ||
to: contact_email, | ||
from: Settings.contact_email, | ||
reply_to: email | ||
} | ||
end | ||
|
||
def self.issue_types_for_locale | ||
[ | ||
I18n.t('hyrax.contact_form.issue_types.depositing'), | ||
I18n.t('hyrax.contact_form.issue_types.changing'), | ||
I18n.t('hyrax.contact_form.issue_types.browsing'), | ||
I18n.t('hyrax.contact_form.issue_types.reporting'), | ||
I18n.t('hyrax.contact_form.issue_types.general') | ||
] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<% content_for :page_header do %> | ||
<h1><span class="fa fa-at"></span> <%= t(:'hyrax.admin.sidebar.contact') %></h1> | ||
<% end %> | ||
|
||
<div class="row"> | ||
<div class="col-md-12"> | ||
<div class="panel panel-default"> | ||
<%= form_for(@site, url: site_contact_path) do |f| %> | ||
<div class="panel-heading"> | ||
<h3 class="panel-title">Contact</h3> | ||
</div> | ||
<div class="panel-body"> | ||
<% if @site.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(@site.errors.count, "error") %> prohibited this site from being saved:</h2> | ||
|
||
<ul> | ||
<% @site.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
<div class="field form-group"> | ||
<%= f.label :contact_email %><br> | ||
<%= f.text_field :contact_email, class: 'form-control'%> | ||
</div> | ||
</div> | ||
<div class="panel-footer"> | ||
<%= f.submit class: 'btn btn-primary pull-right' %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddContactEmailToSites < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :sites, :contact_email, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
RSpec.describe ContactsController, type: :controller do | ||
before do | ||
sign_in user | ||
end | ||
|
||
let(:valid_attributes) do | ||
{ | ||
contact_email: 'test@example.com' | ||
} | ||
end | ||
|
||
let(:invalid_attributes) do | ||
{ contact_email: nil } | ||
end | ||
|
||
context 'with an unprivileged user' do | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
describe "GET #edit" do | ||
it "denies the request" do | ||
get :edit | ||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
end | ||
|
||
describe "PUT #update" do | ||
it "denies the request" do | ||
put :update, params: { site: valid_attributes } | ||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
end | ||
end | ||
|
||
context 'with an administrator' do | ||
let(:user) { FactoryBot.create(:admin) } | ||
|
||
describe "GET #edit" do | ||
it "assigns the requested site as @site" do | ||
get :edit, params: {} | ||
expect(assigns(:site)).to eq(Site.instance) | ||
end | ||
end | ||
|
||
describe "PUT #update" do | ||
context "with valid params" do | ||
let(:new_attributes) do | ||
{ | ||
contact_email: 'new@test.com' | ||
} | ||
end | ||
|
||
it "updates the requested site" do | ||
put :update, params: { site: new_attributes } | ||
Site.reload | ||
expect(Site.contact_email).to eq "new@test.com" | ||
end | ||
|
||
it "assigns the requested site as @site" do | ||
put :update, params: { site: valid_attributes } | ||
expect(assigns(:site)).to eq(Site.instance) | ||
end | ||
|
||
it "redirects to the site" do | ||
put :update, params: { site: valid_attributes } | ||
expect(response).to redirect_to(edit_site_contact_path) | ||
end | ||
end | ||
|
||
context "with invalid params" do | ||
it "assigns the site as @site" do | ||
put :update, params: { site: invalid_attributes } | ||
expect(assigns(:site)).to eq(Site.instance) | ||
end | ||
|
||
it "redirects to the 'edit' template" do | ||
put :update, params: { site: invalid_attributes } | ||
expect(response).to redirect_to(edit_site_contact_path) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
RSpec.describe 'Site contact configuration' do | ||
context 'as an administrator' do | ||
let(:user) { FactoryBot.create(:admin) } | ||
|
||
before do | ||
login_as(user, scope: :user) | ||
end | ||
|
||
describe 'application name' do | ||
it 'updates the application name in the brand bar' do | ||
visit edit_site_contact_path | ||
fill_in 'Contact email', with: 'contact@email.com' | ||
click_on 'Save' | ||
expect(page).to have_current_path(edit_site_contact_path(locale: 'en')) | ||
expect(page).to have_field('Contact email') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
RSpec.describe Hyrax::ContactForm, type: :model do | ||
describe 'headers' do | ||
before do | ||
subject do | ||
described_class.new( | ||
subject: "subject", | ||
from: "from@email.com" | ||
) | ||
end | ||
|
||
allow(Hyrax.config).to receive(:contact_email).and_return('hyrax@email.com') | ||
allow(Settings).to receive(:contact_email_to).and_return('hyrax@email.com') | ||
end | ||
|
||
context 'no email set' do | ||
before do | ||
site = double(Site.new, contact_email: '') | ||
allow(Site).to receive(:instance).and_return(site) | ||
end | ||
it 'uses the hyrax setting' do | ||
expect(subject.headers[:to]).to eq('hyrax@email.com') | ||
end | ||
end | ||
context 'site email set' do | ||
before do | ||
site = double(Site.new, contact_email: 'setting@email.com') | ||
allow(Site).to receive(:instance).and_return(site) | ||
end | ||
it 'uses the Site email' do | ||
expect(subject.headers[:to]).to eq('setting@email.com') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
RSpec.describe ContactsController, type: :routing do | ||
describe "routing" do | ||
it "routes to #edit" do | ||
expect(get: "/site/contact/edit").to route_to("contacts#edit") | ||
end | ||
it "routes to #update via PUT" do | ||
expect(put: "/site/contact").to route_to("contacts#update") | ||
end | ||
|
||
it "routes to #update via PATCH" do | ||
expect(patch: "/site/contact").to route_to("contacts#update") | ||
end | ||
end | ||
end |