From 0dd35b431f5b2bcfd3d78f5109a5e8a554ec7afe Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Tue, 29 Jan 2019 16:30:22 -0800 Subject: [PATCH 1/9] import contact form from hyrax --- app/models/hyrax/contact_form.rb | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/models/hyrax/contact_form.rb diff --git a/app/models/hyrax/contact_form.rb b/app/models/hyrax/contact_form.rb new file mode 100644 index 000000000..15b81d500 --- /dev/null +++ b/app/models/hyrax/contact_form.rb @@ -0,0 +1,35 @@ +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. + def headers + { + subject: "#{Hyrax.config.subject_prefix} #{subject}", + to: Hyrax.config.contact_email, + from: 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 From 0d391d34bcf18874b886532748fee01c5cc5e9bf Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Tue, 29 Jan 2019 16:33:49 -0800 Subject: [PATCH 2/9] fix contact form --- app/models/hyrax/contact_form.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/hyrax/contact_form.rb b/app/models/hyrax/contact_form.rb index 15b81d500..857b04780 100644 --- a/app/models/hyrax/contact_form.rb +++ b/app/models/hyrax/contact_form.rb @@ -15,10 +15,11 @@ def spam? # Declare the e-mail headers. It accepts anything the mail method # in ActionMailer accepts. def headers + # hyrax send the mail 'from' the submitter, which doesnt work on most smtp transports { - subject: "#{Hyrax.config.subject_prefix} #{subject}", + subject: "#{Hyrax.config.subject_prefix} #{email} #{subject}", to: Hyrax.config.contact_email, - from: email + from: Settings.contact_email } end From d358f0da84f44d1c8e7fba132f1927ac6ddb9991 Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Wed, 10 Apr 2019 16:41:54 -0700 Subject: [PATCH 3/9] split email setting in to do and from --- app/models/hyrax/contact_form.rb | 2 +- config/settings.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/hyrax/contact_form.rb b/app/models/hyrax/contact_form.rb index 857b04780..7c0a97eeb 100644 --- a/app/models/hyrax/contact_form.rb +++ b/app/models/hyrax/contact_form.rb @@ -18,7 +18,7 @@ def headers # hyrax send the mail 'from' the submitter, which doesnt work on most smtp transports { subject: "#{Hyrax.config.subject_prefix} #{email} #{subject}", - to: Hyrax.config.contact_email, + to: Settings.contact_email_to, from: Settings.contact_email } end diff --git a/config/settings.yml b/config/settings.yml index e70397f68..a4ef974d1 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -59,6 +59,7 @@ geonames_username: 'jcoyne' # The address to which the contact form is submitted contact_email: "" +contact_email_to: "" devise: account_signup: true From 43acd78b09f323d17f4ae38963f1a075426615f9 Mon Sep 17 00:00:00 2001 From: Daniel Lim Date: Mon, 13 Apr 2020 17:30:58 +0000 Subject: [PATCH 4/9] Merge branch 'i100-contact-us' into 'master' I100 contact us See merge request notch8/hyku-up!32 --- app/controllers/contacts_controller.rb | 28 +++++++ app/models/hyrax/contact_form.rb | 10 ++- app/models/nil_site.rb | 2 +- app/models/site.rb | 2 +- app/views/contacts/edit.html.erb | 35 ++++++++ .../dashboard/sidebar/_configuration.html.erb | 3 + config/locales/hyrax.en.yml | 1 + config/routes.rb | 1 + config/settings.yml | 2 +- ...200103172822_add_contact_email_to_sites.rb | 5 ++ spec/controllers/contact_controller_spec.rb | 82 +++++++++++++++++++ spec/features/contact_spec.rb | 21 +++++ spec/models/hyrax/contact_form_spec.rb | 31 +++++++ spec/routing/contact_routing_spec.rb | 14 ++++ 14 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 app/controllers/contacts_controller.rb create mode 100644 app/views/contacts/edit.html.erb create mode 100644 db/migrate/20200103172822_add_contact_email_to_sites.rb create mode 100644 spec/controllers/contact_controller_spec.rb create mode 100644 spec/features/contact_spec.rb create mode 100644 spec/models/hyrax/contact_form_spec.rb create mode 100644 spec/routing/contact_routing_spec.rb diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb new file mode 100644 index 000000000..81cf44b0e --- /dev/null +++ b/app/controllers/contacts_controller.rb @@ -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 diff --git a/app/models/hyrax/contact_form.rb b/app/models/hyrax/contact_form.rb index 7c0a97eeb..7367f6bcb 100644 --- a/app/models/hyrax/contact_form.rb +++ b/app/models/hyrax/contact_form.rb @@ -1,3 +1,4 @@ +# Copied from hyrax 2.5.1 to override the mail to module Hyrax class ContactForm include ActiveModel::Model @@ -14,11 +15,16 @@ def spam? # 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.present? ? Site.contact_email : Settings.contact_email_to + end + def headers - # hyrax send the mail 'from' the submitter, which doesnt work on most smtp transports + ## override hyrax 2.5.1 send the mail 'from' the submitter, which doesnt work on most smtp transports { subject: "#{Hyrax.config.subject_prefix} #{email} #{subject}", - to: Settings.contact_email_to, + to: self.contact_email, from: Settings.contact_email } end diff --git a/app/models/nil_site.rb b/app/models/nil_site.rb index 3894cde39..3ac350fb5 100644 --- a/app/models/nil_site.rb +++ b/app/models/nil_site.rb @@ -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 diff --git a/app/models/site.rb b/app/models/site.rb index 6c8a5e9e4..eaaa0c712 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -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 diff --git a/app/views/contacts/edit.html.erb b/app/views/contacts/edit.html.erb new file mode 100644 index 000000000..6d2657e55 --- /dev/null +++ b/app/views/contacts/edit.html.erb @@ -0,0 +1,35 @@ +<% content_for :page_header do %> +

<%= t(:'hyrax.admin.sidebar.contact') %>

+<% end %> + +
+
+
+ <%= form_for(@site, url: site_contact_path) do |f| %> +
+

Contact

+
+
+ <% if @site.errors.any? %> +
+

<%= pluralize(@site.errors.count, "error") %> prohibited this site from being saved:

+ +
    + <% @site.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> +
+ <%= f.label :contact_email %>
+ <%= f.text_field :contact_email, class: 'form-control'%> +
+
+ + <% end %> +
+
+
diff --git a/app/views/hyrax/dashboard/sidebar/_configuration.html.erb b/app/views/hyrax/dashboard/sidebar/_configuration.html.erb index e17a6d8b1..6df412f31 100644 --- a/app/views/hyrax/dashboard/sidebar/_configuration.html.erb +++ b/app/views/hyrax/dashboard/sidebar/_configuration.html.erb @@ -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 %> + <%= t('hyrax.admin.sidebar.contact') %> + <% end %> <%= menu.nav_link(main_app.edit_site_labels_path) do %> <%= t('hyrax.admin.sidebar.labels') %> <% end %> diff --git a/config/locales/hyrax.en.yml b/config/locales/hyrax.en.yml index 720acd7c0..49c84c9e2 100644 --- a/config/locales/hyrax.en.yml +++ b/config/locales/hyrax.en.yml @@ -210,6 +210,7 @@ en: collection_types: Collection Types collections: Collections configuration: Configuration + contact: Contact content_blocks: Content Blocks notifications: Notifications pages: Pages diff --git a/config/routes.rb b/config/routes.rb index d43233223..fdc4f10e9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/config/settings.yml b/config/settings.yml index a4ef974d1..46fb37549 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -58,7 +58,7 @@ 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: diff --git a/db/migrate/20200103172822_add_contact_email_to_sites.rb b/db/migrate/20200103172822_add_contact_email_to_sites.rb new file mode 100644 index 000000000..39bc749dd --- /dev/null +++ b/db/migrate/20200103172822_add_contact_email_to_sites.rb @@ -0,0 +1,5 @@ +class AddContactEmailToSites < ActiveRecord::Migration[5.1] + def change + add_column :sites, :contact_email, :string + end +end diff --git a/spec/controllers/contact_controller_spec.rb b/spec/controllers/contact_controller_spec.rb new file mode 100644 index 000000000..61d99cc83 --- /dev/null +++ b/spec/controllers/contact_controller_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/features/contact_spec.rb b/spec/features/contact_spec.rb new file mode 100644 index 000000000..d5aaf089f --- /dev/null +++ b/spec/features/contact_spec.rb @@ -0,0 +1,21 @@ +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' + # TODO(@dlim87): this is another test where capybara is getting logged out after an action + expect(page).to have_current_path(edit_site_contact_path) + expect(page).to have_field('Contact email') + end + + end + end +end diff --git a/spec/models/hyrax/contact_form_spec.rb b/spec/models/hyrax/contact_form_spec.rb new file mode 100644 index 000000000..45f8feb0a --- /dev/null +++ b/spec/models/hyrax/contact_form_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe Hyrax::ContactForm, type: :model do + describe 'headers' do + before do + subject { described_class.new( + subject: "subject", + from: "from@email.com" + )} + allow(Hyrax.config).to receive(:contact_email).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 + byebug + 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 \ No newline at end of file diff --git a/spec/routing/contact_routing_spec.rb b/spec/routing/contact_routing_spec.rb new file mode 100644 index 000000000..bc5550819 --- /dev/null +++ b/spec/routing/contact_routing_spec.rb @@ -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 From 1bb7e34b09e9c3112f4cbd64b027a3209c222891 Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Fri, 17 Apr 2020 23:14:02 -0700 Subject: [PATCH 5/9] add run migration --- db/schema.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index c3257ba79..ce12cd510 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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| From a0f2041599a1f2bb55cff827721ed13b92afac64 Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Fri, 17 Apr 2020 23:55:39 -0700 Subject: [PATCH 6/9] add reply_to --- app/models/hyrax/contact_form.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/hyrax/contact_form.rb b/app/models/hyrax/contact_form.rb index 7367f6bcb..9b611e5f0 100644 --- a/app/models/hyrax/contact_form.rb +++ b/app/models/hyrax/contact_form.rb @@ -25,7 +25,8 @@ def headers { subject: "#{Hyrax.config.subject_prefix} #{email} #{subject}", to: self.contact_email, - from: Settings.contact_email + from: Settings.contact_email, + reply_to: email } end From 4606a7cb24879494c07e1f0843651cc20e0cffc2 Mon Sep 17 00:00:00 2001 From: Daniel Lim Date: Mon, 20 Apr 2020 15:05:43 -0700 Subject: [PATCH 7/9] rubocop fixes --- app/models/hyrax/contact_form.rb | 9 +++++---- ...troller_spec.rb => contacts_controller_spec.rb} | 2 +- spec/features/contact_spec.rb | 1 - spec/models/hyrax/contact_form_spec.rb | 14 ++++++++------ 4 files changed, 14 insertions(+), 12 deletions(-) rename spec/controllers/{contact_controller_spec.rb => contacts_controller_spec.rb} (99%) diff --git a/app/models/hyrax/contact_form.rb b/app/models/hyrax/contact_form.rb index 9b611e5f0..92956673d 100644 --- a/app/models/hyrax/contact_form.rb +++ b/app/models/hyrax/contact_form.rb @@ -6,7 +6,8 @@ class ContactForm 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 + # - 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? @@ -17,14 +18,14 @@ def spam? # in ActionMailer accepts. ###### OVERRODE the to: field to add the Tenant's email, first def contact_email - Site.contact_email.present? ? Site.contact_email : Settings.contact_email_to + Site.contact_email.presence || Settings.contact_email_to end def headers - ## override hyrax 2.5.1 send the mail 'from' the submitter, which doesnt work on most smtp transports + ## 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: self.contact_email, + to: contact_email, from: Settings.contact_email, reply_to: email } diff --git a/spec/controllers/contact_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb similarity index 99% rename from spec/controllers/contact_controller_spec.rb rename to spec/controllers/contacts_controller_spec.rb index 61d99cc83..a851bd09a 100644 --- a/spec/controllers/contact_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -79,4 +79,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/features/contact_spec.rb b/spec/features/contact_spec.rb index d5aaf089f..8db795319 100644 --- a/spec/features/contact_spec.rb +++ b/spec/features/contact_spec.rb @@ -15,7 +15,6 @@ expect(page).to have_current_path(edit_site_contact_path) expect(page).to have_field('Contact email') end - end end end diff --git a/spec/models/hyrax/contact_form_spec.rb b/spec/models/hyrax/contact_form_spec.rb index 45f8feb0a..b42f62da6 100644 --- a/spec/models/hyrax/contact_form_spec.rb +++ b/spec/models/hyrax/contact_form_spec.rb @@ -1,10 +1,13 @@ RSpec.describe Hyrax::ContactForm, type: :model do describe 'headers' do before do - subject { described_class.new( - subject: "subject", - from: "from@email.com" - )} + subject do + described_class.new( + subject: "subject", + from: "from@email.com" + ) + end + allow(Hyrax.config).to receive(:contact_email).and_return('hyrax@email.com') end @@ -14,7 +17,6 @@ allow(Site).to receive(:instance).and_return(site) end it 'uses the hyrax setting' do - byebug expect(subject.headers[:to]).to eq('hyrax@email.com') end end @@ -28,4 +30,4 @@ end end end -end \ No newline at end of file +end From bf941b414b7071864ded612fe8558b7ddc5ae43a Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Wed, 17 Jun 2020 13:09:00 -0700 Subject: [PATCH 8/9] Make contact spec green --- spec/features/contact_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/features/contact_spec.rb b/spec/features/contact_spec.rb index d5aaf089f..ab83d3667 100644 --- a/spec/features/contact_spec.rb +++ b/spec/features/contact_spec.rb @@ -11,8 +11,7 @@ visit edit_site_contact_path fill_in 'Contact email', with: 'contact@email.com' click_on 'Save' - # TODO(@dlim87): this is another test where capybara is getting logged out after an action - expect(page).to have_current_path(edit_site_contact_path) + expect(page).to have_current_path(edit_site_contact_path(locale: 'en')) expect(page).to have_field('Contact email') end From a19b1c3ebc8d4bcd4b34f25ae476ea51389f9fa2 Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Wed, 17 Jun 2020 13:21:17 -0700 Subject: [PATCH 9/9] Fix spec contact mock --- spec/models/hyrax/contact_form_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/hyrax/contact_form_spec.rb b/spec/models/hyrax/contact_form_spec.rb index b42f62da6..a80dabb9e 100644 --- a/spec/models/hyrax/contact_form_spec.rb +++ b/spec/models/hyrax/contact_form_spec.rb @@ -9,6 +9,7 @@ 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