Skip to content

Commit

Permalink
merging plugin_consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkkelly committed Sep 21, 2010
2 parents 5f295e1 + 6ba5d1b commit 2350bf6
Show file tree
Hide file tree
Showing 21 changed files with 624 additions and 422 deletions.
68 changes: 60 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,76 @@ Using forms 'DRY's up the process of creating and reusing forms across a site (w
</body>
</html>

### Email

Delete the following line in config/environment.rb

config.frameworks -= [ :action_mailer ]

or just remove the :action_mailer references

config.frameworks -= []


#### Config

Define your mailing variables

_hardcoded_

mail:
from: email@email.com
to: email@email.com
reply_to: email@email.com
subject: subject text
_variable_

mail:
field:
from: person[email]
to: person[email]
subject: contact[subject]
reply_to: person[email]

#### SMTP

Of course you are probably using sendgrid to make sending emails easy,
but if you're using SMTP create the following to **/config/initializers/form_mail.rb**

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "smtp.gmail.com",
:authentication => :plain,
:user_name => "username@gmail.com",
:password => "password"
}

## Addons

### The Market

* [radiant-forms_mail-extension](http://github.com/squaretalent/radiant-forms_mail-extension) -
A showcase of how to use addons, allows you to send emails directly from the page

### Controller
### Model

Must be named **FormsBlahController** and a controller of the same name
Must be named **FormBlahController**

class FormsBlahController
include Forms::AddonMethods # Manages your controller initialization
class FormBlah
include Forms::Models::Extension # Sorts out initialization giving you
# def initialize(form, page)
# @form = form
# @page = page
#
# @data = @page.data
# @config = @form.config[self.class.to_s.downcase.gsub('form', '').to_sym].symbolize_keys # @form.config[:blah]

def create
# @form = Form which the data comes from
# @page = Page which submitted the form (data contains submitted information)

# return = {
# :hash => 'these details will be returned to the result page namespaced under blah'
# }
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
3.0.0.rc1
12 changes: 8 additions & 4 deletions app/controllers/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class FormsController < ApplicationController
# POST /forms/1
#----------------------------------------------------------------------------
def create
@page = Page.find(params[:page_id])
@form = Form.find(params[:form_id])
@page.data = params

@page = Page.find(params[:page_id])
@page.data = params
@page.request = {
:session => session
}

response = current_response
response.result = params
Expand All @@ -21,8 +25,8 @@ def create
end

@form[:config].each do |ext, config|
ext_controller = ("Forms#{ext.to_s.capitalize}Controller".constantize).new(@form, @page)
response.result = response.result.merge({ "#{ext}_ext" => ext_controller.create })
extension = ("Form#{ext.to_s.capitalize}".constantize).new(@form, @page)
response.result = response.result.merge({ "#{ext}_ext" => extension.create })
end

if response.save
Expand Down
129 changes: 129 additions & 0 deletions app/models/form_mail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
class FormMail
include Forms::Models::Extension

def create
@body = @page.render_snippet(@form)

begin
FormMailer.deliver_mail(
:recipients => recipients,
:from => from,
:reply_to => reply_to,
:subject => subject,
:body => body,
:cc => cc,
:headers => headers,
:content_type => content_type,
:charset => charset
)
@sent = true
rescue Exception => exception
raise exception if RAILS_ENV['development']
@message = exception
@sent = false
end

@result = {
:sent => self.sent?,
:message => self.message,
:subject => self.subject,
:from => self.from
}
end

def from
from = nil
unless @config[:field].nil? or !@config[:field][:from].blank?
from = hash_retrieve(@data, @config[:field][:from])
else
from = @config[:from]
end
from
end

def recipients
to = nil
unless @config[:field].nil? or !@config[:field][:to].blank?
to = hash_retrieve(@data, @config[:field][:to])
else
to = @config[:to]
end
to
end

def reply_to
reply_to = nil
unless @config[:field].nil? or !@config[:field][:reply_to].blank?
reply_to = hash_retrieve(@data, @config[:field][:reply_to])
else
reply_to = @config[:reply_to]
end
reply_to
end

def sender
sender = nil
unless @config[:field].nil? or !@config[:field][:sender].blank?
sender = hash_retrieve(@data, @config[:field][:sender])
else
sender = @config[:sender]
end
sender
end

def subject
subject = nil
unless @config[:field].nil? or !@config[:field][:subject].blank?
subject = hash_retrieve(@data, @config[:field][:subject])
else
subject = @config[:subject]
end
subject
end

def body
@body || ''
end

def cc
@config[:cc]
end

def sent?
@sent || false
end

def message
@message || nil
end

def headers
headers = { 'Reply-To' => reply_to }
if sender
headers['Return-Path'] = sender
headers['Sender'] = sender
end
headers
end

def content_type
content_type = config[:content_type] || 'multipart/mixed'
end

def charset
charset = config[:charset] || 'utf-8'
charset = charset == '' ? nil : charset
end

protected

# takes object[value] || value and accesses the hash as hash[object][value] || hash[value]
def hash_retrieve(hash, array)
data = array.gsub('[','|').gsub(']','').split('|') rescue nil

result = false
result = hash.fetch(data[0]) unless data.nil?
result = result.fetch(data[1]) if !data.nil? and data[1]
end

end
16 changes: 16 additions & 0 deletions app/models/form_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class FormMailer < ActionMailer::Base

def mail(options)
content_type options[:content_type]
charset options[:charset]
headers options[:headers]

recipients options[:recipients]
from options[:from]
cc options[:cc]
bcc options[:bcc]
subject options[:subject]
body options[:body]
end

end
17 changes: 12 additions & 5 deletions forms_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ class FormsExtension < Radiant::Extension
end

def activate
Radiant::AdminUI.send(:include, Forms::AdminUI) unless defined? admin.form
admin.form = Radiant::AdminUI.load_default_form_regions
# View Hooks
unless defined? admin.form
Radiant::AdminUI.send :include, Forms::Interface::Core

admin.form = Radiant::AdminUI.load_default_form_regions
end

# Model Includes
Page.send :include, Forms::Tags::Core, Forms::Models::Page

Page.class_eval { include Forms::Tags, Forms::PageExtensions }
ApplicationController.send(:include, Forms::ApplicationControllerExtensions)
SiteController.send(:include, Forms::SiteControllerExtensions)
# Controller Includes
ApplicationController.send :include, Forms::Controllers::ApplicationController
SiteController.send :include, Forms::Controllers::SiteController

tab 'Design' do
add_item 'Forms', '/admin/forms', :after => 'Snippets'
Expand Down
14 changes: 0 additions & 14 deletions lib/forms/addon_methods.rb

This file was deleted.

23 changes: 0 additions & 23 deletions lib/forms/admin_ui.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/forms/application_controller_extensions.rb

This file was deleted.

24 changes: 24 additions & 0 deletions lib/forms/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Forms
module Controllers
module ApplicationController

def self.included(base)
base.class_eval do
helper_method :current_response

def current_response
if request.session[:form_response]
@response = Response.find(request.session[:form_response])
else
@response = Response.create
request.session = @response.id
end

@response
end
end
end

end
end
end
Loading

0 comments on commit 2350bf6

Please sign in to comment.