Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact page - customizable email to per tenant #1622

Merged
merged 12 commits into from
Jun 22, 2020
28 changes: 28 additions & 0 deletions app/controllers/contacts_controller.rb
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
44 changes: 44 additions & 0 deletions app/models/hyrax/contact_form.rb
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
2 changes: 1 addition & 1 deletion app/models/nil_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def instance
attr_reader :id, :account, :application_name, :institution_name,
:institution_name_full, :banner_image, :primary_key,
:logo_image, :default_collection_image, :default_work_image,
:directory_image
:directory_image, :contact_email

# rubocop:disable Lint/DuplicateMethods
def reload
Expand Down
2 changes: 1 addition & 1 deletion app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Site < ApplicationRecord

class << self
delegate :account, :application_name, :institution_name,
:institution_name_full, :reload, :update,
:institution_name_full, :reload, :update, :contact_email,
to: :instance

def instance
Expand Down
35 changes: 35 additions & 0 deletions app/views/contacts/edit.html.erb
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>
3 changes: 3 additions & 0 deletions app/views/hyrax/dashboard/sidebar/_configuration.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
icon_class: "fa fa-cog",
id: 'collapseSettings',
open: menu.settings_section? do %>
<%= menu.nav_link(main_app.edit_site_contact_path) do %>
<span class="fa fa-at"></span> <span class="sidebar-action-text"><%= t('hyrax.admin.sidebar.contact') %></span>
<% end %>
<%= menu.nav_link(main_app.edit_site_labels_path) do %>
<span class="fa fa-institution"></span> <span class="sidebar-action-text"><%= t('hyrax.admin.sidebar.labels') %></span>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/hyrax.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ en:
collection_types: Collection Types
collections: Collections
configuration: Configuration
contact: Contact
content_blocks: Content Blocks
notifications: Notifications
pages: Pages
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
resource :site, only: [:update] do
resources :roles, only: [:index, :update]
resource :labels, only: [:edit, :update]
resource :contact, only: [:edit, :update]
end

root 'hyrax/homepage#index'
Expand Down
3 changes: 2 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ google_analytics_id:
geonames_username: 'jcoyne'

# The address to which the contact form is submitted
contact_email: ""
contact_email: "hykuup@notch8.com"
contact_email_to: ""

devise:
account_signup: true
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20200103172822_add_contact_email_to_sites.rb
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
5 changes: 3 additions & 2 deletions 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: 20191212155530) do
ActiveRecord::Schema.define(version: 20200103172822) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -601,11 +601,12 @@
t.string "institution_name"
t.string "institution_name_full"
t.string "banner_image"
t.text "available_works", default: [], array: true
t.string "logo_image"
t.string "default_collection_image"
t.string "default_work_image"
t.text "available_works", default: [], array: true
t.string "directory_image"
t.string "contact_email"
end

create_table "subject_local_authority_entries", id: :serial, force: :cascade do |t|
Expand Down
82 changes: 82 additions & 0 deletions spec/controllers/contacts_controller_spec.rb
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
19 changes: 19 additions & 0 deletions spec/features/contact_spec.rb
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
34 changes: 34 additions & 0 deletions spec/models/hyrax/contact_form_spec.rb
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
14 changes: 14 additions & 0 deletions spec/routing/contact_routing_spec.rb
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