radiant-forms-extension 2.0.1 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
- git clone git://github.com/squaretalent/radiant-forms-extension vendor/extensions/forms
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
- ### Controller
92
-
93
- Must be named **FormsBlahController** and a controller of the same name
147
+ ### Model
94
148
 
95
- class FormsBlahController
96
- include Forms::AddonMethods # Manages your controller initialization
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
- 2.0.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
- # POST /forms/1
8
- #----------------------------------------------------------------------------
9
- def create
10
- @page = Page.find(params[:page_id])
11
- @form = Form.find(params[:form_id])
12
- @page.data = params
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 = current_response
15
- response.result = params
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
- @form[:config] = YAML::load("--- !map:HashWithIndifferentAccess\n"+@form[:config]).symbolize_keys
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
- @form[:config].each do |ext, config|
24
- ext_controller = ("Forms#{ext.to_s.capitalize}Controller".constantize).new(@form, @page)
25
- response.result = response.result.merge({ "#{ext}_ext" => ext_controller.create })
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
- if response.save
29
- redirect_to (@form.redirect_to.nil? ? @page.url : @form.redirect_to)
30
- else
31
- raise "Form '#{@form.name}' could not be submitted. Sorry"
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
- validates_presence_of :title
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
@@ -1,7 +1,12 @@
1
1
  class Response < ActiveRecord::Base
2
2
 
3
3
  def result
4
- ActiveSupport::JSON.decode(self.result_json) unless self.result_json.nil?
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
@@ -4,6 +4,6 @@ ActionController::Routing::Routes.draw do |map|
4
4
  admin.resources :forms
5
5
  end
6
6
 
7
- map.forms '/forms/:form_id', :controller => 'forms', :action => 'create', :conditions => { :method => :post }
7
+ map.resources :forms, :only => [ :update ]
8
8
 
9
9
  end
@@ -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
- Radiant::AdminUI.send(:include, Forms::AdminUI) unless defined? admin.form
14
- admin.form = Radiant::AdminUI.load_default_form_regions
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
- Page.class_eval { include Forms::Tags, Forms::PageExtensions }
17
- ApplicationController.send(:include, Forms::ApplicationControllerExtensions)
18
- SiteController.send(:include, Forms::SiteControllerExtensions)
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'
@@ -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,13 @@
1
+ module Forms
2
+ module Controllers
3
+ module SiteController
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ before_filter :current_response
8
+ end
9
+ end
10
+
11
+ end
12
+ end
13
+ 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
@@ -0,0 +1,14 @@
1
+ module Forms
2
+ module Models
3
+ module Page
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ attr_accessor :last_form
8
+ attr_accessor :data
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+ end