Skip to content

Commit

Permalink
Merge 48133ee into 6d0882b
Browse files Browse the repository at this point in the history
  • Loading branch information
rorJeremy committed Nov 2, 2018
2 parents 6d0882b + 48133ee commit 1bfe3de
Show file tree
Hide file tree
Showing 27 changed files with 367 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require_tree .
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$(window).load(function() {
$('[data-contact-form]').each((_, input) => {
const $form = $(input);

function handleResponse(status, responseJSON) {
if (responseJSON) {
alert(responseJSON.message);
} else {
alert('Sorry, something went wrong.');
}
$('input:visible, textarea', $form).val('');
}

$form.on('ajax:success', (event, data, status, xhr) => {
handleResponse(status, xhr.responseJSON);
});

$form.on('ajax:error', (event, xhr, status) => {
handleResponse(status, xhr.responseJSON);
});
});
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class WCC::Contentful::App::ContactFormController < ApplicationController
def create
form_model.send_email(form_params)

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

private

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

@form_model ||= WCC::Contentful::Model::SectionContactForm.find(params[:id])
end

def form_params
params.permit(*form_model.fields.map(&:title))
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module WCC::Contentful::App
if defined?(::ApplicationMailer)
class ContactMailer < ::ApplicationMailer
def contact_form_email(to_email, data)
@form_data = data

mail(to: to_email, subject: 'Contact Us Form Submission')
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module WCC::Contentful::App
if defined?(::ActiveRecord)
class ContactFormSubmission < ::ActiveRecord::Base
end
end
end
9 changes: 9 additions & 0 deletions wcc-contentful-app/app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions wcc-contentful-app/app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= content_for?(:content) ? yield(:content) : yield %>
35 changes: 35 additions & 0 deletions wcc-contentful-app/app/views/sections/_contact_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div class="row">
<div class="column">
<div class="contact-panel -mt5">
<div class="contact-panel__header">
<%= markdown(section.text) %>
</div><!--faq-panel__header-->

<div class="contact-panel__content">
<%= form_tag contact_form_path, remote: true, data: { contact_form: '' }, method: 'post' do %>
<input type="hidden" name="id" value="<%= section.id %>" />
<% section.fields&.each do |field| %>
<div class="field">
<label for="" class="contact-panel__label"><%= field.title %></label>
<%= case field.input_type
when 'email'
content_tag(:input, nil, type: 'email', name: field.title, required: true)
when 'textarea'
content_tag(:textarea, nil,
rows: 8, columnns: 80, name: field.title, minlength: 5, required: true)
else
content_tag(:input, nil, type: 'text', name: field.title)
end %>

</div>
<% end %>
<%= submit_tag section.submit_button_text || 'Submit',
class: 'button white', data: { disable_with: 'Submitting...' } %>
<% end %>

</div><!--faq-panel__content-->

</div><!--faq-panel-->

</div><!--column-->
</div><!--row-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<h1>Contact Us Form Submission</h1>
<% @form_data.each do |key, value| %>
<p>
<%= key %> -> <%= value %>
</p>
<% end %>
1 change: 1 addition & 0 deletions wcc-contentful-app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
WCC::Contentful::App::Engine.routes.draw do
get '/*slug', to: 'pages#show'
root 'pages#index'
post '/contact_form', to: 'contact_form#create', as: :contact_form
end
8 changes: 6 additions & 2 deletions wcc-contentful-app/lib/generators/wcc/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ def ensure_initializer_exists
copy_file 'wcc_contentful.rb', 'config/initializers/wcc_contentful.rb'
end

def create_model_migration
copy_file "#{singular}/generated_add_#{plural}.ts",
def create_model_migrations
copy_file "#{singular}/migrations/generated_add_#{plural}.ts",
"db/migrate/#{timestamp}01_generated_add_#{plural}.ts"

Dir.glob("#{File.dirname(__FILE__)}/#{singular}/migrations/*.rb").each do |f|
copy_file f, "db/migrate/#{timestamp}_#{f}"
end
end

def drop_model_overrides_in_app_models
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateWCCContentfulAppContactFormSubmissions < ActiveRecord::Migration[5.2]
def change
create_table :wcc_contentful_app_contact_form_submissions do |t|
t.string :form_id
t.json :data, default: {}

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import Migration, { MigrationFunction } from 'contentful-migration'

// Generated by contentful-schema-diff
// from contentful-export-4gyidsb2jx1u-rorJeremy-2018-10-18T15-41-43.json
// to 7yx6ovlj39n5/master
export = function (migration: Migration, { makeRequest, spaceId, accessToken }) {

/************ section-contact-form ******************/

var sectionContactForm = migration.createContentType('section-contact-form', {
displayField: 'internalTitle',
name: 'Section: Contact Form',
description:
'A Contact Form section contains a Form with several Form Fields. The responses to the form will be collected and provided to the appropriate administrator.'
})

sectionContactForm.createField('internalTitle', {
name: 'Internal Title (Contentful Only)',
type: 'Symbol',
localized: false,
required: true,
validations: [],
disabled: false,
omitted: true
})

sectionContactForm.createField('text', {
name: 'Text',
type: 'Text',
localized: false,
required: true,
validations: [],
disabled: false,
omitted: false
})

sectionContactForm.createField('fields', {
name: 'Fields',
type: 'Array',
localized: false,
required: true,
validations: [],
disabled: false,
omitted: false,
items: {
type: 'Link',
validations: [
{
linkContentType: ['formField']
}
],
linkType: 'Entry'
}
})

sectionContactForm.createField('submitButtonText', {
name: 'Submit Button Text',
type: 'Symbol',
localized: false,
required: false,
validations: [],
disabled: false,
omitted: false
})

sectionContactForm.createField('notificationEmail', {
name: 'NotificationEmail',
type: 'Symbol',
localized: false,
required: true,
validations: [
{
regexp: {
pattern:
'^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$',
flags: null
},
message: 'Must be a valid email address.'
}
],
disabled: false,
omitted: false
})

sectionContactForm.changeEditorInterface('internalTitle', 'singleLine')

sectionContactForm.changeEditorInterface('text', 'markdown')

sectionContactForm.changeEditorInterface('fields', 'entryLinksEditor')

sectionContactForm.changeEditorInterface('submitButtonText', 'singleLine')

sectionContactForm.changeEditorInterface('notificationEmail', 'singleLine', {
helpText:
'This is the email address where the form submission notification goes.'
})

/************ formField ******************/

var formfield = migration.createContentType('formField', {
displayField: 'internalTitle',
name: 'Form Field',
description: 'A Form Field represents a piece of information you want to collect from a user via a Form.'
})

formfield.createField('internalTitle', {
name: 'Internal Title (Contentful Only)',
type: 'Symbol',
localized: false,
required: true,
validations: [],
disabled: false,
omitted: true
})

formfield.createField('title', {
name: 'Title',
type: 'Symbol',
localized: false,
required: false,
validations: [],
disabled: false,
omitted: false
})

formfield.createField('inputType', {
name: 'Input Type',
type: 'Symbol',
localized: false,
required: true,
validations:
[{
in:
['text',
'email',
'textarea']
}],
disabled: false,
omitted: false
})

formfield.changeEditorInterface('title', 'singleLine')

formfield.changeEditorInterface('inputType', 'radio')

formfield.changeEditorInterface('internalTitle', 'singleLine')
} as MigrationFunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# This model represents the 'formField' content type in Contentful. Any linked
# entries of the 'formField' content type will be resolved as instances of this class.
# It exposes #find, #find_by, and #find_all methods to query Contentful.
class FormField < WCC::Contentful::Model::FormField
# Add custom validations to ensure that app-specific properties exist:
# validate_field :foo, :String, :required
# validate_field :bar_links, :Array, link_to: %w[bar baz]

# Override functionality or add utilities
#
# # Example: override equality
# def ===(other)
# ...
# end
#
# # Example: override "name" attribute to always be camelized.
# # `@name` is populated by the gem in the initializer.
# def name
# @name_camelized ||= @name.camelize(true)
# end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# This model represents the 'section-contact-form' content type in Contentful. Any linked
# entries of the 'section-contact-form' content type will be resolved as instances of this class.
# It exposes .find, .find_by, and .find_all methods to query Contentful.
class SectionContactForm < WCC::Contentful::Model::SectionContactForm
# Add custom validations to ensure that app-specific properties exist:
# validate_field :foo, :String, :required
# validate_field :bar_links, :Array, link_to: %w[bar baz]

# Override functionality or add utilities
#
# # Example: override equality
# def ===(other)
# ...
# end
#
# # Example: override "title" attribute to always be titlecase.
# # `@title` is populated by the gem in the initializer.
# def title
# @title_titlecased ||= @title.titlecase
# end
end
11 changes: 11 additions & 0 deletions wcc-contentful-app/lib/wcc/contentful/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,16 @@ def self.init!
file = File.dirname(__FILE__) + "/model/#{t.name.underscore}.rb"
require file if File.exist?(file)
end

@db_connected =
begin
::ActiveRecord::Base.connection_pool.with_connection(&:active?)
rescue StandardError
false
end
end

def self.db_connected?
@db_connected
end
end
4 changes: 4 additions & 0 deletions wcc-contentful-app/lib/wcc/contentful/model/form_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class WCC::Contentful::Model::FormField < WCC::Contentful::Model
end
Loading

0 comments on commit 1bfe3de

Please sign in to comment.