radiant-forms-extension 2.0.1 → 3.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +85 -14
- data/VERSION +1 -1
- data/app/controllers/forms_controller.rb +34 -17
- data/app/models/form.rb +7 -1
- data/app/models/form_mail.rb +118 -0
- data/app/models/form_mailer.rb +16 -0
- data/app/models/response.rb +6 -1
- data/config/routes.rb +1 -1
- data/db/migrate/20100929150423_change_to_updated_by_id.rb +15 -0
- data/forms_extension.rb +12 -8
- data/lib/forms/config.rb +30 -0
- data/lib/forms/controllers/application_controller.rb +39 -0
- data/lib/forms/controllers/site_controller.rb +13 -0
- data/lib/forms/interface/forms.rb +25 -0
- data/lib/forms/models/extension.rb +25 -0
- data/lib/forms/models/page.rb +14 -0
- data/lib/forms/tags/core.rb +201 -0
- data/lib/forms/tags/helpers.rb +54 -0
- data/lib/forms/tags/responses.rb +58 -0
- data/radiant-forms-extension.gemspec +16 -12
- data/spec/controllers/forms_controller_spec.rb +13 -13
- data/spec/datasets/forms.rb +4 -4
- data/spec/lib/forms/tags_spec.rb +3 -14
- data/spec/models/response_spec.rb +2 -2
- metadata +29 -13
- data/lib/forms/addon_methods.rb +0 -14
- data/lib/forms/admin_ui.rb +0 -23
- data/lib/forms/application_controller_extensions.rb +0 -20
- data/lib/forms/page_extensions.rb +0 -6
- data/lib/forms/site_controller_extensions.rb +0 -19
- data/lib/forms/tags.rb +0 -277
- data/spec/lib/forms/addon_methods_spec.rb +0 -41
data/README.md
CHANGED
@@ -2,15 +2,23 @@
|
|
2
2
|
|
3
3
|
This extension allows a developer to create forms which can take on multiple tasks
|
4
4
|
|
5
|
-
The idea was taken from the mailer extension, everything mail specific is used in [radiant-forms_mail-extension](http://github.com/squaretalent/radiant-forms_mail-extension)
|
6
|
-
|
7
5
|
Using forms 'DRY's up the process of creating and reusing forms across a site (well I think so at least).
|
8
6
|
|
9
7
|
## Installation
|
10
|
-
|
11
|
-
|
8
|
+
|
9
|
+
**The New way:**
|
10
|
+
|
11
|
+
gem install radiant-forms-extension
|
12
|
+
# add the following line to your config/environment.rb: config.gem 'radiant-forms-extension', :lib => false
|
13
|
+
rake radiant:extensions:forms:update
|
12
14
|
rake radiant:extensions:forms:migrate
|
15
|
+
|
16
|
+
**The old way:**
|
17
|
+
|
18
|
+
git clone git://github.com/squaretalent/radiant-forms-extension vendor/extensions/forms
|
13
19
|
rake radiant:extensions:forms:update
|
20
|
+
rake radiant:extensions:forms:migrate
|
21
|
+
|
14
22
|
|
15
23
|
## Usage
|
16
24
|
|
@@ -81,24 +89,74 @@ Using forms 'DRY's up the process of creating and reusing forms across a site (w
|
|
81
89
|
</body>
|
82
90
|
</html>
|
83
91
|
|
92
|
+
### Email
|
93
|
+
|
94
|
+
Delete the following line in config/environment.rb
|
95
|
+
|
96
|
+
config.frameworks -= [ :action_mailer ]
|
97
|
+
|
98
|
+
or just remove the :action_mailer references
|
99
|
+
|
100
|
+
config.frameworks -= []
|
101
|
+
|
102
|
+
|
103
|
+
#### Config
|
104
|
+
|
105
|
+
Define your mailing variables
|
106
|
+
|
107
|
+
_hardcoded_
|
108
|
+
|
109
|
+
mail:
|
110
|
+
from: email@email.com
|
111
|
+
to: email@email.com
|
112
|
+
reply_to: email@email.com
|
113
|
+
subject: subject text
|
114
|
+
|
115
|
+
_variable_
|
116
|
+
|
117
|
+
mail:
|
118
|
+
field:
|
119
|
+
from: person[email]
|
120
|
+
to: person[email]
|
121
|
+
subject: contact[subject]
|
122
|
+
reply_to: person[email]
|
123
|
+
|
124
|
+
#### SMTP
|
125
|
+
|
126
|
+
Of course you are probably using sendgrid to make sending emails easy,
|
127
|
+
but if you're using SMTP create the following to **/config/initializers/form_mail.rb**
|
128
|
+
|
129
|
+
ActionMailer::Base.delivery_method = :smtp
|
130
|
+
ActionMailer::Base.raise_delivery_errors = true
|
131
|
+
ActionMailer::Base.smtp_settings = {
|
132
|
+
:enable_starttls_auto => true,
|
133
|
+
:address => "smtp.gmail.com",
|
134
|
+
:port => "587",
|
135
|
+
:domain => "smtp.gmail.com",
|
136
|
+
:authentication => :plain,
|
137
|
+
:user_name => "username@gmail.com",
|
138
|
+
:password => "password"
|
139
|
+
}
|
140
|
+
|
84
141
|
## Addons
|
85
142
|
|
86
143
|
### The Market
|
87
144
|
|
88
|
-
* [radiant-forms_mail-extension](http://github.com/squaretalent/radiant-forms_mail-extension) -
|
89
145
|
A showcase of how to use addons, allows you to send emails directly from the page
|
90
146
|
|
91
|
-
###
|
92
|
-
|
93
|
-
Must be named **FormsBlahController** and a controller of the same name
|
147
|
+
### Model
|
94
148
|
|
95
|
-
class
|
96
|
-
include Forms::
|
149
|
+
class FormBlah
|
150
|
+
include Forms::Models::Extension # Sorts out initialization giving you
|
151
|
+
|
152
|
+
# def initialize(form, page)
|
153
|
+
# @form = form
|
154
|
+
# @page = page
|
155
|
+
#
|
156
|
+
# @data = @page.data
|
157
|
+
# @config = @form.config[self.class.to_s.downcase.gsub('form', '').to_sym].symbolize_keys # @form.config[:blah]
|
97
158
|
|
98
159
|
def create
|
99
|
-
# @form = Form which the data comes from
|
100
|
-
# @page = Page which submitted the form (data contains submitted information)
|
101
|
-
|
102
160
|
# return = {
|
103
161
|
# :hash => 'these details will be returned to the result page namespaced under blah'
|
104
162
|
# }
|
@@ -115,4 +173,17 @@ A showcase of how to use addons, allows you to send emails directly from the pag
|
|
115
173
|
### Functionality
|
116
174
|
|
117
175
|
I'm going to let you sort that out, you have the create action with input and output
|
118
|
-
from here you can decide how your form addon is going to behave.
|
176
|
+
from here you can decide how your form addon is going to behave.
|
177
|
+
|
178
|
+
# Development
|
179
|
+
|
180
|
+
unless ENV["RAILS_ENV"] == "production"
|
181
|
+
config.gem 'rspec', :version => '1.3.0'
|
182
|
+
config.gem 'rspec-rails', :version => '1.3.2'
|
183
|
+
config.gem 'cucumber', :verison => '0.8.5'
|
184
|
+
config.gem 'cucumber-rails', :version => '0.3.2'
|
185
|
+
config.gem 'database_cleaner', :version => '0.4.3'
|
186
|
+
config.gem 'ruby-debug', :version => '0.10.3'
|
187
|
+
config.gem 'webrat', :version => '0.7.1'
|
188
|
+
config.gem 'rr', :version => '0.10.11'
|
189
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.1.1
|
@@ -3,32 +3,49 @@ class FormsController < ApplicationController
|
|
3
3
|
no_login_required
|
4
4
|
|
5
5
|
skip_before_filter :verify_authenticity_token
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@page = Page.find(params[:page_id])
|
11
|
-
@
|
12
|
-
@page.
|
6
|
+
|
7
|
+
def update
|
8
|
+
@form = Form.find(params[:id])
|
9
|
+
|
10
|
+
@page = Page.find(params[:page_id]) rescue Page.first
|
11
|
+
@page.data = params
|
12
|
+
@page.request = OpenStruct.new({
|
13
|
+
:session => session # Creating a pretend response object
|
14
|
+
})
|
13
15
|
|
14
|
-
response
|
15
|
-
response
|
16
|
+
# We need a response object
|
17
|
+
@response = find_or_create_response
|
18
|
+
# Put the submitted data into the response object
|
19
|
+
@response.result = Forms::Config.deep_symbolize_keys(params)
|
16
20
|
|
17
21
|
begin
|
18
|
-
|
22
|
+
# Grab the form configuration data
|
23
|
+
@form[:extensions] = Forms::Config.convert(@form.config)
|
19
24
|
rescue
|
20
25
|
raise "Form '#{@form.title}' has not been configured"
|
21
26
|
end
|
22
27
|
|
23
|
-
@
|
24
|
-
|
25
|
-
|
28
|
+
@results = {}
|
29
|
+
# Iterate through each configured extension
|
30
|
+
@form[:extensions].each do |ext, config|
|
31
|
+
# New Instance of the FormExtension class
|
32
|
+
extension = ("Form#{ext.to_s.pluralize.classify}".constantize).new(@form, @page)
|
33
|
+
# .pluralize.classify means singulars like business and address are converted correctly
|
34
|
+
|
35
|
+
# Result of the extension create method gets merged
|
36
|
+
result = extension.create
|
37
|
+
|
38
|
+
@results.merge!({ ext.to_sym => result }) # merges this extensions results
|
39
|
+
session.merge!(result[:session]) if result[:session].present?
|
26
40
|
end
|
41
|
+
# Those results are merged into the response object
|
42
|
+
@response.result = @response.result.merge!({ :results => @results})
|
27
43
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
44
|
+
begin
|
45
|
+
@response.save!
|
46
|
+
redirect_to @form.redirect_to.present? ? @form.redirect_to : @page.url
|
47
|
+
rescue
|
48
|
+
"Form '#{@form.title}' could not be submitted."
|
32
49
|
end
|
33
50
|
end
|
34
51
|
|
data/app/models/form.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
class Form < ActiveRecord::Base
|
2
2
|
|
3
|
-
|
3
|
+
default_scope :order => 'forms.title ASC'
|
4
|
+
|
5
|
+
validates_presence_of :title
|
6
|
+
validates_uniqueness_of :title
|
7
|
+
|
8
|
+
belongs_to :created_by, :class_name => 'User'
|
9
|
+
belongs_to :updated_by, :class_name => 'User'
|
4
10
|
|
5
11
|
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
class FormMail
|
2
|
+
include Forms::Models::Extension
|
3
|
+
|
4
|
+
def create
|
5
|
+
@body = @page.render_snippet(@form)
|
6
|
+
|
7
|
+
begin
|
8
|
+
FormMailer.deliver_mail(
|
9
|
+
:recipients => recipients,
|
10
|
+
:from => from,
|
11
|
+
:reply_to => reply_to,
|
12
|
+
:subject => subject,
|
13
|
+
:body => body,
|
14
|
+
:cc => cc,
|
15
|
+
:headers => headers,
|
16
|
+
:content_type => content_type,
|
17
|
+
:charset => charset
|
18
|
+
)
|
19
|
+
@sent = true
|
20
|
+
rescue Exception => exception
|
21
|
+
raise exception if RAILS_ENV['development']
|
22
|
+
@message = exception
|
23
|
+
@sent = false
|
24
|
+
end
|
25
|
+
|
26
|
+
@result = {
|
27
|
+
:sent => self.sent?,
|
28
|
+
:message => self.message,
|
29
|
+
:subject => self.subject,
|
30
|
+
:from => self.from
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def from
|
35
|
+
from = nil
|
36
|
+
unless @config[:field].nil? or !@config[:field][:from].blank?
|
37
|
+
from = Forms::Tags::Responses.retrieve(@data, @config[:field][:from])
|
38
|
+
else
|
39
|
+
from = @config[:from]
|
40
|
+
end
|
41
|
+
from
|
42
|
+
end
|
43
|
+
|
44
|
+
def recipients
|
45
|
+
to = nil
|
46
|
+
unless @config[:field].nil? or !@config[:field][:to].blank?
|
47
|
+
to = Forms::Tags::Responses.retrieve(@data, @config[:field][:to])
|
48
|
+
else
|
49
|
+
to = @config[:to]
|
50
|
+
end
|
51
|
+
to
|
52
|
+
end
|
53
|
+
|
54
|
+
def reply_to
|
55
|
+
reply_to = nil
|
56
|
+
unless @config[:field].nil? or !@config[:field][:reply_to].blank?
|
57
|
+
reply_to = Forms::Tags::Responses.retrieve(@data, @config[:field][:reply_to])
|
58
|
+
else
|
59
|
+
reply_to = @config[:reply_to]
|
60
|
+
end
|
61
|
+
reply_to
|
62
|
+
end
|
63
|
+
|
64
|
+
def sender
|
65
|
+
sender = nil
|
66
|
+
unless @config[:field].nil? or !@config[:field][:sender].blank?
|
67
|
+
sender = Forms::Tags::Responses.retrieve(@data, @config[:field][:sender])
|
68
|
+
else
|
69
|
+
sender = @config[:sender]
|
70
|
+
end
|
71
|
+
sender
|
72
|
+
end
|
73
|
+
|
74
|
+
def subject
|
75
|
+
subject = nil
|
76
|
+
unless @config[:field].nil? or !@config[:field][:subject].blank?
|
77
|
+
subject = Forms::Tags::Responses.retrieve(@data, @config[:field][:subject])
|
78
|
+
else
|
79
|
+
subject = @config[:subject]
|
80
|
+
end
|
81
|
+
subject
|
82
|
+
end
|
83
|
+
|
84
|
+
def body
|
85
|
+
@body || ''
|
86
|
+
end
|
87
|
+
|
88
|
+
def cc
|
89
|
+
@config[:cc]
|
90
|
+
end
|
91
|
+
|
92
|
+
def sent?
|
93
|
+
@sent || false
|
94
|
+
end
|
95
|
+
|
96
|
+
def message
|
97
|
+
@message || nil
|
98
|
+
end
|
99
|
+
|
100
|
+
def headers
|
101
|
+
headers = { 'Reply-To' => reply_to }
|
102
|
+
if sender
|
103
|
+
headers['Return-Path'] = sender
|
104
|
+
headers['Sender'] = sender
|
105
|
+
end
|
106
|
+
headers
|
107
|
+
end
|
108
|
+
|
109
|
+
def content_type
|
110
|
+
content_type = @config[:content_type] || 'text/html'
|
111
|
+
end
|
112
|
+
|
113
|
+
def charset
|
114
|
+
charset = @config[:charset] || 'utf-8'
|
115
|
+
charset = charset == '' ? nil : charset
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class FormMailer < ActionMailer::Base
|
2
|
+
|
3
|
+
def mail(options)
|
4
|
+
content_type options[:content_type]
|
5
|
+
charset options[:charset]
|
6
|
+
headers options[:headers]
|
7
|
+
|
8
|
+
recipients options[:recipients]
|
9
|
+
from options[:from]
|
10
|
+
cc options[:cc]
|
11
|
+
bcc options[:bcc]
|
12
|
+
subject options[:subject]
|
13
|
+
body options[:body]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/app/models/response.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
class Response < ActiveRecord::Base
|
2
2
|
|
3
3
|
def result
|
4
|
-
|
4
|
+
result = {}
|
5
|
+
if self.result_json.present?
|
6
|
+
result = ActiveSupport::JSON.decode(self.result_json)
|
7
|
+
result = Forms::Config.deep_symbolize_keys(result)
|
8
|
+
end
|
9
|
+
result
|
5
10
|
end
|
6
11
|
|
7
12
|
def result=(result)
|
data/config/routes.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
class ChangeToUpdatedById < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
add_column :forms, :created_by_id, :integer
|
4
|
+
add_column :forms, :updated_by_id, :integer
|
5
|
+
remove_column :forms, :created_by
|
6
|
+
remove_column :forms, :updated_by
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
remove_column :forms, :created_by_id
|
11
|
+
remove_column :forms, :updated_by_id
|
12
|
+
add_column :forms, :created_by, :integer
|
13
|
+
add_column :forms, :updated_by, :integer
|
14
|
+
end
|
15
|
+
end
|
data/forms_extension.rb
CHANGED
@@ -4,18 +4,22 @@ class FormsExtension < Radiant::Extension
|
|
4
4
|
url 'http://github.com/squaretalent/radiant-forms-extension'
|
5
5
|
|
6
6
|
extension_config do |config|
|
7
|
-
if RAILS_ENV == :test
|
8
|
-
config.gem 'rr', :version => '0.10.11'
|
9
|
-
end
|
10
7
|
end
|
11
8
|
|
12
9
|
def activate
|
13
|
-
|
14
|
-
admin.form
|
10
|
+
# View Hooks
|
11
|
+
unless defined? admin.form
|
12
|
+
Radiant::AdminUI.send :include, Forms::Interface::Forms
|
13
|
+
|
14
|
+
admin.form = Radiant::AdminUI.load_default_form_regions
|
15
|
+
end
|
16
|
+
|
17
|
+
# Model Includes
|
18
|
+
Page.send :include, Forms::Tags::Core, Forms::Models::Page
|
15
19
|
|
16
|
-
|
17
|
-
ApplicationController.send
|
18
|
-
SiteController.send
|
20
|
+
# Controller Includes
|
21
|
+
ApplicationController.send :include, Forms::Controllers::ApplicationController
|
22
|
+
SiteController.send :include, Forms::Controllers::SiteController
|
19
23
|
|
20
24
|
tab 'Design' do
|
21
25
|
add_item 'Forms', '/admin/forms', :after => 'Snippets'
|
data/lib/forms/config.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Forms
|
2
|
+
module Config
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def convert(yaml)
|
6
|
+
hash = hashify(yaml)
|
7
|
+
hash = deep_symbolize_keys(hash)
|
8
|
+
|
9
|
+
hash
|
10
|
+
end
|
11
|
+
|
12
|
+
def hashify(yaml)
|
13
|
+
YAML::load("--- !map:HashWithIndifferentAccess\n"+yaml)
|
14
|
+
end
|
15
|
+
|
16
|
+
def deep_symbolize_keys(item)
|
17
|
+
case item
|
18
|
+
when Hash
|
19
|
+
item.inject({}) do |acc, (k, v)|
|
20
|
+
acc[(k.to_sym rescue k)] = deep_symbolize_keys(v)
|
21
|
+
acc
|
22
|
+
end
|
23
|
+
else
|
24
|
+
item
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Forms
|
2
|
+
module Controllers
|
3
|
+
module ApplicationController
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
def current_response
|
8
|
+
return @current_response if defined?(@current_response)
|
9
|
+
@current_response = find_response
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_response
|
13
|
+
response = nil
|
14
|
+
|
15
|
+
if request.session[:form_response]
|
16
|
+
response = Response.find(request.session[:form_response])
|
17
|
+
end
|
18
|
+
|
19
|
+
response
|
20
|
+
end
|
21
|
+
|
22
|
+
def find_or_create_response
|
23
|
+
response = nil
|
24
|
+
|
25
|
+
if find_response
|
26
|
+
response = find_response
|
27
|
+
else
|
28
|
+
response = Response.create
|
29
|
+
request.session[:form_response] = response.id
|
30
|
+
end
|
31
|
+
|
32
|
+
response
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Forms
|
2
|
+
module Interface
|
3
|
+
module Forms
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
attr_accessor :form
|
8
|
+
alias_method :forms, :form
|
9
|
+
protected
|
10
|
+
def load_default_form_regions
|
11
|
+
returning OpenStruct.new do |form|
|
12
|
+
form.edit = Radiant::AdminUI::RegionSet.new do |edit|
|
13
|
+
edit.main.concat %w{edit_header edit_form}
|
14
|
+
edit.form.concat %w{edit_title edit_content}
|
15
|
+
edit.form_bottom.concat %w{edit_buttons edit_timestamp}
|
16
|
+
end
|
17
|
+
form.new = form.edit
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Forms
|
2
|
+
module Models
|
3
|
+
module Extension
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
def initialize(form, page)
|
7
|
+
@form = form
|
8
|
+
@page = page
|
9
|
+
|
10
|
+
@data = Forms::Config.deep_symbolize_keys(@page.data)
|
11
|
+
|
12
|
+
# Sets the config to be the current environment config: checkout:
|
13
|
+
@config = @form[:extensions][self.class.to_s.underscore.gsub('form_', '').to_sym]
|
14
|
+
end
|
15
|
+
|
16
|
+
def current_user
|
17
|
+
return @current_user if @current_user.present?
|
18
|
+
@current_user = UserActionObserver.current_user
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|