Skip to content

Commit

Permalink
Merge pull request #141 from watermarkchurch/71_person_contact_form
Browse files Browse the repository at this point in the history
71 person contact form
  • Loading branch information
rorJeremy committed Dec 18, 2018
2 parents a5196c7 + 64deac3 commit d188b06
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

class WCC::Contentful::App::ContactFormController < ApplicationController
def create
form_model.send_email(form_params)
address = params[:email_object_id] ? email_address(email_model) : form_model.notification_email
form_model.send_email(form_params.merge!({ notification_email: address }))

render json: { type: 'success', message: "Thanks for reaching out. We'll be in touch soon!" }
end

private

def email_address(entry)
return entry.email if defined?(entry.email)

raise ArgumentError, 'email is not defined on this entry'
end

def email_model
raise ArgumentError, 'contentful entry does not exist' unless
entry = WCC::Contentful::Model.find(params[:email_object_id])

entry
end

def form_model
raise ArgumentError, 'missing form ID' unless params[:id]

Expand Down
1 change: 0 additions & 1 deletion wcc-contentful-app/app/views/components/_section.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
<% else %>
<%= render("sections/#{section_name}", section: section) %>
<% end %>
<% end %>
7 changes: 7 additions & 0 deletions wcc-contentful-app/app/views/sections/_contact_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
data: { contact_form: true },
method: 'post' do %>
<input type="hidden" name="id" value="<%= section.id %>" />
<% if defined?(email_object_id) %>
<input
id="email-object-id"
type="hidden"
name="email_object_id"
value="<%= email_object_id %>" />
<% end %>
<% section.fields&.each do |field| %>
<div class="form-group contact-panel__form-field">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class WCC::Contentful::Model::SectionContactForm < WCC::Contentful::Model
def send_email(data)
::WCC::Contentful::App::ContactMailer.contact_form_email(notificationEmail, data).deliver
::WCC::Contentful::App::ContactMailer.contact_form_email(data[:notification_email], data).deliver

save_contact_form(data)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,47 @@
RSpec.describe 'sections/contact_form' do
helper WCC::Contentful::App::SectionHelper

it 'raises action view template error if text is nil' do
section = contentful_create('section-contact-form',
text: nil)
context 'when partial is rendered from components/section' do
it 'raises action view template error if text is nil' do
section = contentful_create('section-contact-form',
text: nil)

expect {
render partial: 'components/section', locals: { section: section }
}.to raise_error(ActionView::Template::Error)
expect {
render 'components/section', section: section, email_object_id: 'IDGOESHERE'
}.to raise_error(ActionView::Template::Error)
end

it 'processes the markdown in the section' do
section = contentful_create('section-contact-form',
text: '## This should be an H2')

render 'components/section', section: section, email_object_id: 'IDGOESHERE'

expect(rendered).to match(/<h2>This should be an H2<\/h2>/)
end

it 'does NOT accept email_object_id' do
email = 'ez.net'
section = contentful_create('section-contact-form',
text: '## This should be an H2',
notification_email: email)

render 'components/section', section: section, email_object_id: 'IDGOESHERE'

expect(rendered).to_not have_selector('input#email-object-id', visible: false)
end
end

it 'processes the markdown in the section' do
section = contentful_create('section-contact-form',
text: '## This should be an H2')
context 'when partial is rendered directly' do
it 'accepts and renders email_object_id as a hidden field' do
email = 'ez.net'
section = contentful_create('section-contact-form',
text: '## This should be an H2',
notification_email: email)

render partial: 'components/section', locals: { section: section }
render 'sections/contact_form', section: section, email_object_id: 'IDGOESHERE'

expect(rendered).to match(/<h2>This should be an H2<\/h2>/)
expect(rendered).to have_selector('input#email-object-id', visible: false)
end
end
end

0 comments on commit d188b06

Please sign in to comment.