noodall-form-builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +24 -0
  3. data/Gemfile.lock +202 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +3 -0
  6. data/Rakefile +39 -0
  7. data/app/controllers/noodall/admin/fields_controller.rb +21 -0
  8. data/app/controllers/noodall/admin/form_responses_controller.rb +75 -0
  9. data/app/controllers/noodall/admin/forms_controller.rb +97 -0
  10. data/app/controllers/noodall/form_responses_controller.rb +39 -0
  11. data/app/helpers/admin/forms_helper.rb +2 -0
  12. data/app/helpers/forms_helper.rb +11 -0
  13. data/app/mailers/form_mailer.rb +21 -0
  14. data/app/models/contact_form.rb +16 -0
  15. data/app/models/noodall/check_box.rb +5 -0
  16. data/app/models/noodall/field.rb +25 -0
  17. data/app/models/noodall/form.rb +38 -0
  18. data/app/models/noodall/form_response.rb +88 -0
  19. data/app/models/noodall/radio.rb +4 -0
  20. data/app/models/noodall/select.rb +5 -0
  21. data/app/models/noodall/text_field.rb +12 -0
  22. data/app/views/admin/components/_contact_form.html.erb +11 -0
  23. data/app/views/components/_contact_form.html.erb +8 -0
  24. data/app/views/form_mailer/_form_response_fields.html.erb +3 -0
  25. data/app/views/form_mailer/form_response.html.erb +5 -0
  26. data/app/views/form_mailer/form_response_thankyou.html.erb +19 -0
  27. data/app/views/noodall/admin/fields/_check_box.html.erb +14 -0
  28. data/app/views/noodall/admin/fields/_radio.html.erb +14 -0
  29. data/app/views/noodall/admin/fields/_select.html.erb +14 -0
  30. data/app/views/noodall/admin/fields/_text_field.html.erb +26 -0
  31. data/app/views/noodall/admin/form_responses/index.csv.csvbuilder +12 -0
  32. data/app/views/noodall/admin/form_responses/index.html.erb +41 -0
  33. data/app/views/noodall/admin/forms/_field_select.html.erb +11 -0
  34. data/app/views/noodall/admin/forms/index.html.erb +41 -0
  35. data/app/views/noodall/admin/forms/show.html.erb +138 -0
  36. data/app/views/noodall/form_responses/_form.html.erb +45 -0
  37. data/app/views/noodall/form_responses/create.html.erb +5 -0
  38. data/app/views/noodall/form_responses/new.html.erb +5 -0
  39. data/app/views/noodall/forms/edit.html.erb +12 -0
  40. data/app/views/noodall/forms/index.html.erb +18 -0
  41. data/app/views/noodall/forms/new.html.erb +11 -0
  42. data/app/views/noodall/forms/show.html.erb +3 -0
  43. data/cucumber.yml +8 -0
  44. data/features/form_builder.feature +54 -0
  45. data/features/form_module.feature +40 -0
  46. data/features/step_definitions/cms_node_steps.rb +126 -0
  47. data/features/step_definitions/component_steps.rb +138 -0
  48. data/features/step_definitions/email_steps.rb +182 -0
  49. data/features/step_definitions/form_builder_steps.rb +247 -0
  50. data/features/step_definitions/sign_in_steps.rb +48 -0
  51. data/features/step_definitions/web_steps.rb +219 -0
  52. data/features/support/defensio_mock.rb +6 -0
  53. data/features/support/env.rb +52 -0
  54. data/features/support/paths.rb +60 -0
  55. data/lib/noodall/form_builder/engine.rb +13 -0
  56. data/lib/noodall/form_builder/routes.rb +35 -0
  57. data/lib/noodall/form_builder/version.rb +5 -0
  58. data/lib/noodall-form-builder.rb +1 -0
  59. data/noodall-form-builder.gemspec +23 -0
  60. data/public/images/admin/spam.png +0 -0
  61. data/public/javascripts/admin/formbuilder.js +52 -0
  62. data/spec/dummy/Rakefile +7 -0
  63. data/spec/dummy/app/controllers/application_controller.rb +22 -0
  64. data/spec/dummy/app/helpers/application_helper.rb +8 -0
  65. data/spec/dummy/app/models/page_a.rb +8 -0
  66. data/spec/dummy/app/models/user.rb +14 -0
  67. data/spec/dummy/app/views/admin/nodes/_page_a.html.erb +47 -0
  68. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  69. data/spec/dummy/app/views/nodes/page_a.html.erb +14 -0
  70. data/spec/dummy/config/application.rb +46 -0
  71. data/spec/dummy/config/boot.rb +10 -0
  72. data/spec/dummy/config/database.yml +12 -0
  73. data/spec/dummy/config/defensio.yml +16 -0
  74. data/spec/dummy/config/environment.rb +5 -0
  75. data/spec/dummy/config/environments/development.rb +26 -0
  76. data/spec/dummy/config/environments/production.rb +49 -0
  77. data/spec/dummy/config/environments/test.rb +35 -0
  78. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  79. data/spec/dummy/config/initializers/dragonfly.rb +1 -0
  80. data/spec/dummy/config/initializers/inflections.rb +10 -0
  81. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  82. data/spec/dummy/config/initializers/mongo_mapper.rb +13 -0
  83. data/spec/dummy/config/initializers/noodall.rb +5 -0
  84. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  85. data/spec/dummy/config/initializers/session_store.rb +8 -0
  86. data/spec/dummy/config/locales/en.yml +5 -0
  87. data/spec/dummy/config/routes.rb +4 -0
  88. data/spec/dummy/config.ru +4 -0
  89. data/spec/dummy/public/404.html +26 -0
  90. data/spec/dummy/public/422.html +26 -0
  91. data/spec/dummy/public/500.html +26 -0
  92. data/spec/dummy/public/favicon.ico +0 -0
  93. data/spec/dummy/public/javascripts/application.js +2 -0
  94. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  95. data/spec/dummy/script/rails +6 -0
  96. data/spec/factories/asset.rb +6 -0
  97. data/spec/factories/contact_form.rb +3 -0
  98. data/spec/factories/field.rb +15 -0
  99. data/spec/factories/form.rb +13 -0
  100. data/spec/factories/node.rb +5 -0
  101. data/spec/factories/response.rb +7 -0
  102. data/spec/factories/user.rb +15 -0
  103. data/spec/files/beef.png +0 -0
  104. data/spec/integration/navigation_spec.rb +9 -0
  105. data/spec/noodall_form_builder_spec.rb +7 -0
  106. data/spec/spec_helper.rb +30 -0
  107. metadata +204 -0
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ pkg/*
2
+ *.gem
3
+ log/*.log
4
+ .bundle
5
+ spec/dummy/db/*.sqlite3
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'rails', '3.0.1'
6
+ gem 'noodall-ui'
7
+ gem 'capybara', '>= 0.3.9'
8
+ gem 'rspec-rails', '>= 2.0.0.beta'
9
+ gem 'database_cleaner'
10
+ gem 'cucumber-rails'
11
+ gem 'launchy' # So you can do Then show me the page
12
+ gem 'rmagick', :require => 'RMagick'
13
+ gem 'dragonfly', '~> 0.7.6'
14
+ gem 'factory_girl', '~> 1.3.2'
15
+ gem 'faker', '~> 0.3.1'
16
+ gem 'defensio'
17
+ gem 'email_spec'
18
+ gem 'fastercsv'
19
+
20
+
21
+ if RUBY_VERSION < '1.9'
22
+ gem "ruby-debug", ">= 0.10.3"
23
+ end
24
+
data/Gemfile.lock ADDED
@@ -0,0 +1,202 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ noodall-form-builder (0.0.1)
5
+ noodall-core
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ abstract (1.0.0)
11
+ actionmailer (3.0.1)
12
+ actionpack (= 3.0.1)
13
+ mail (~> 2.2.5)
14
+ actionpack (3.0.1)
15
+ activemodel (= 3.0.1)
16
+ activesupport (= 3.0.1)
17
+ builder (~> 2.1.2)
18
+ erubis (~> 2.6.6)
19
+ i18n (~> 0.4.1)
20
+ rack (~> 1.2.1)
21
+ rack-mount (~> 0.6.12)
22
+ rack-test (~> 0.5.4)
23
+ tzinfo (~> 0.3.23)
24
+ activemodel (3.0.1)
25
+ activesupport (= 3.0.1)
26
+ builder (~> 2.1.2)
27
+ i18n (~> 0.4.1)
28
+ activerecord (3.0.1)
29
+ activemodel (= 3.0.1)
30
+ activesupport (= 3.0.1)
31
+ arel (~> 1.0.0)
32
+ tzinfo (~> 0.3.23)
33
+ activeresource (3.0.1)
34
+ activemodel (= 3.0.1)
35
+ activesupport (= 3.0.1)
36
+ activesupport (3.0.1)
37
+ arel (1.0.1)
38
+ activesupport (~> 3.0.0)
39
+ bson (1.1.4)
40
+ builder (2.1.2)
41
+ canable (0.1.1)
42
+ capybara (0.4.0)
43
+ celerity (>= 0.7.9)
44
+ culerity (>= 0.2.4)
45
+ mime-types (>= 1.16)
46
+ nokogiri (>= 1.3.3)
47
+ rack (>= 1.0.0)
48
+ rack-test (>= 0.5.4)
49
+ selenium-webdriver (>= 0.0.27)
50
+ xpath (~> 0.1.2)
51
+ celerity (0.8.4)
52
+ childprocess (0.1.4)
53
+ ffi (~> 0.6.3)
54
+ columnize (0.3.2)
55
+ configuration (1.2.0)
56
+ crack (0.1.8)
57
+ cucumber (0.9.4)
58
+ builder (~> 2.1.2)
59
+ diff-lcs (~> 1.1.2)
60
+ gherkin (~> 2.2.9)
61
+ json (~> 1.4.6)
62
+ term-ansicolor (~> 1.0.5)
63
+ cucumber-rails (0.3.2)
64
+ cucumber (>= 0.8.0)
65
+ culerity (0.2.12)
66
+ database_cleaner (0.6.0)
67
+ defensio (0.9.1)
68
+ httparty (>= 0.5.0)
69
+ diff-lcs (1.1.2)
70
+ dragonfly (0.7.7)
71
+ rack
72
+ dynamic_form (1.1.3)
73
+ email_spec (1.0.0)
74
+ erubis (2.6.6)
75
+ abstract (>= 1.0.0)
76
+ factory_girl (1.3.2)
77
+ faker (0.3.1)
78
+ fastercsv (1.5.3)
79
+ ffi (0.6.3)
80
+ rake (>= 0.8.7)
81
+ gherkin (2.2.9)
82
+ json (~> 1.4.6)
83
+ term-ansicolor (~> 1.0.5)
84
+ httparty (0.6.1)
85
+ crack (= 0.1.8)
86
+ i18n (0.4.2)
87
+ jnunemaker-validatable (1.8.4)
88
+ activesupport (>= 2.3.4)
89
+ json (1.4.6)
90
+ json_pure (1.4.6)
91
+ launchy (0.3.7)
92
+ configuration (>= 0.0.5)
93
+ rake (>= 0.8.1)
94
+ linecache (0.43)
95
+ mail (2.2.10)
96
+ activesupport (>= 2.3.6)
97
+ i18n (~> 0.4.1)
98
+ mime-types (~> 1.16)
99
+ treetop (~> 1.4.8)
100
+ mime-types (1.16)
101
+ mm-multi-parameter-attributes (0.1.1)
102
+ mongo_mapper
103
+ mongo (1.1.4)
104
+ bson (>= 1.1.1)
105
+ mongo_mapper (0.8.6)
106
+ activesupport (>= 2.3.4)
107
+ jnunemaker-validatable (~> 1.8.4)
108
+ plucky (~> 0.3.6)
109
+ nokogiri (1.4.4)
110
+ noodall-core (0.3.4)
111
+ canable (= 0.1.1)
112
+ mm-multi-parameter-attributes (~> 0.1.1)
113
+ mongo_mapper (~> 0.8.6)
114
+ ramdiv-mongo_mapper_acts_as_tree (~> 0.1.1)
115
+ ruby-stemmer
116
+ noodall-ui (0.0.13)
117
+ dynamic_form
118
+ noodall-core
119
+ thoughtbot-sortable_table (= 0.0.6)
120
+ will_paginate (~> 3.0.pre2)
121
+ plucky (0.3.6)
122
+ mongo (~> 1.1)
123
+ polyglot (0.3.1)
124
+ rack (1.2.1)
125
+ rack-mount (0.6.13)
126
+ rack (>= 1.0.0)
127
+ rack-test (0.5.6)
128
+ rack (>= 1.0)
129
+ rails (3.0.1)
130
+ actionmailer (= 3.0.1)
131
+ actionpack (= 3.0.1)
132
+ activerecord (= 3.0.1)
133
+ activeresource (= 3.0.1)
134
+ activesupport (= 3.0.1)
135
+ bundler (~> 1.0.0)
136
+ railties (= 3.0.1)
137
+ railties (3.0.1)
138
+ actionpack (= 3.0.1)
139
+ activesupport (= 3.0.1)
140
+ rake (>= 0.8.4)
141
+ thor (~> 0.14.0)
142
+ rake (0.8.7)
143
+ ramdiv-mongo_mapper_acts_as_tree (0.1.2)
144
+ mongo_mapper (>= 0.6.8)
145
+ rmagick (2.13.1)
146
+ rspec (2.2.0)
147
+ rspec-core (~> 2.2)
148
+ rspec-expectations (~> 2.2)
149
+ rspec-mocks (~> 2.2)
150
+ rspec-core (2.2.1)
151
+ rspec-expectations (2.2.0)
152
+ diff-lcs (~> 1.1.2)
153
+ rspec-mocks (2.2.0)
154
+ rspec-rails (2.2.1)
155
+ actionpack (~> 3.0)
156
+ activesupport (~> 3.0)
157
+ railties (~> 3.0)
158
+ rspec (~> 2.2.0)
159
+ ruby-debug (0.10.4)
160
+ columnize (>= 0.1)
161
+ ruby-debug-base (~> 0.10.4.0)
162
+ ruby-debug-base (0.10.4)
163
+ linecache (>= 0.3)
164
+ ruby-stemmer (0.8.2)
165
+ rubyzip (0.9.4)
166
+ selenium-webdriver (0.1.1)
167
+ childprocess (= 0.1.4)
168
+ ffi (~> 0.6.3)
169
+ json_pure
170
+ rubyzip
171
+ term-ansicolor (1.0.5)
172
+ thor (0.14.6)
173
+ thoughtbot-sortable_table (0.0.6)
174
+ treetop (1.4.9)
175
+ polyglot (>= 0.3.1)
176
+ tzinfo (0.3.23)
177
+ will_paginate (3.0.pre2)
178
+ xpath (0.1.2)
179
+ nokogiri (~> 1.3)
180
+
181
+ PLATFORMS
182
+ ruby
183
+
184
+ DEPENDENCIES
185
+ bundler (>= 1.0.0)
186
+ capybara (>= 0.3.9)
187
+ cucumber-rails
188
+ database_cleaner
189
+ defensio
190
+ dragonfly (~> 0.7.6)
191
+ email_spec
192
+ factory_girl (~> 1.3.2)
193
+ faker (~> 0.3.1)
194
+ fastercsv
195
+ launchy
196
+ noodall-core
197
+ noodall-form-builder!
198
+ noodall-ui
199
+ rails (= 3.0.1)
200
+ rmagick
201
+ rspec-rails (>= 2.0.0.beta)
202
+ ruby-debug (>= 0.10.3)
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2010 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Noodall-form-builder
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require 'rspec/core'
10
+ require 'rspec/core/rake_task'
11
+
12
+ Rspec::Core::RakeTask.new(:spec)
13
+
14
+ require 'cucumber/rake/task'
15
+
16
+ namespace :cucumber do
17
+ Cucumber::Rake::Task.new(:ok, 'Run features that should pass') do |t|
18
+ t.fork = true # You may get faster startup if you set this to false
19
+ t.profile = 'default'
20
+ end
21
+
22
+ Cucumber::Rake::Task.new(:wip, 'Run features that are being worked on') do |t|
23
+ t.fork = true # You may get faster startup if you set this to false
24
+ t.profile = 'wip'
25
+ end
26
+
27
+ Cucumber::Rake::Task.new(:rerun, 'Record failing features and run only them if any exist') do |t|
28
+ t.fork = true # You may get faster startup if you set this to false
29
+ t.profile = 'rerun'
30
+ end
31
+
32
+ desc 'Run all features'
33
+ task :all => [:ok, :wip]
34
+ end
35
+ desc 'Alias for cucumber:ok'
36
+ task :cucumber => 'cucumber:ok'
37
+
38
+ task :default => :cucumber
39
+
@@ -0,0 +1,21 @@
1
+ module Noodall
2
+ class Admin::FieldsController < Noodall::Admin::BaseController
3
+ include SortableTable::App::Controllers::ApplicationController
4
+ # too long on the form builder and the system kicks you out
5
+ before_filter :sign_in_required, :except => [:new]
6
+
7
+ def new
8
+
9
+ field = "Noodall::#{params[:type].classify}".constantize
10
+ # check that field class exists
11
+ if defined?(field)
12
+ @field = field.new
13
+
14
+ respond_to do |format|
15
+ format.html { render :layout => false, :partial => "noodall/admin/fields/#{params[:type]}", :locals => {:field => @field, :index => params[:index].to_i}}
16
+ format.xml { render :xml => @forms }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,75 @@
1
+ module Noodall
2
+ class Admin::FormResponsesController < Noodall::Admin::BaseController
3
+ include SortableTable::App::Controllers::ApplicationController
4
+ before_filter :find_form,:set_title
5
+
6
+ def index
7
+ if @form.nil?
8
+ @responses = Noodall::FormResponse.all
9
+ else
10
+ @responses = @form.responses
11
+ end
12
+
13
+ respond_to do |format|
14
+ format.html # index.html.erb
15
+ format.xml { render :xml => @responses }
16
+
17
+
18
+ format.csv do
19
+ csv_string = FasterCSV.generate do |csv|
20
+ header_row = @form.fields.map do |field|
21
+ field.name
22
+ end
23
+ header_row += ['Date', 'IP', 'Form Location']
24
+ csv << header_row
25
+
26
+ for response in @responses
27
+ response_row = @form.fields.map do |field|
28
+ begin
29
+ response.send(field.underscored_name)
30
+ rescue NoMethodError => e
31
+ nil
32
+ end
33
+ end
34
+ response_row += [response.created_at.to_formatted_s(:long_dot), response.ip, response.referrer]
35
+ csv << response_row
36
+ end
37
+ end
38
+ send_data csv_string, :filename => "#{@form.title} responses - #{Time.now.to_formatted_s(:db)}.csv",
39
+ :type => 'text/csv',
40
+ :disposition => 'attachment'
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ def destroy
47
+ @form.responses = @form.responses.reject{|r| r.id.to_s == params[:id]}
48
+
49
+ @form.save
50
+ flash[:notice] = "Response was successfully deleted."
51
+
52
+ respond_to do |format|
53
+ format.html { redirect_to(noodall_admin_form_form_responses_url(@form)) }
54
+ format.xml { head :ok }
55
+ end
56
+ end
57
+
58
+ def mark_as_spam
59
+ @response = @form.responses.find(params[:id])
60
+
61
+ @response.mark_as_spam!
62
+
63
+ redirect_to(noodall_admin_form_form_responses_url(@form))
64
+ end
65
+
66
+ private
67
+ def set_title
68
+ @page_title = "#{@form.title} Responses"
69
+ end
70
+ def find_form
71
+ @form = Noodall::Form.find(params[:form_id]) unless params[:form_id].blank?
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,97 @@
1
+ module Noodall
2
+ class Admin::FormsController < Noodall::Admin::BaseController
3
+ include SortableTable::App::Controllers::ApplicationController
4
+ sortable_attributes :created_at, :updated_at, :title
5
+
6
+ before_filter :set_title
7
+
8
+ def index
9
+ @forms = Noodall::Form.all
10
+
11
+ respond_to do |format|
12
+ format.html # index.html.erb
13
+ format.xml { render :xml => @forms }
14
+ end
15
+ end
16
+
17
+ def show
18
+ @form = Noodall::Form.find(params[:id])
19
+
20
+ respond_to do |format|
21
+ format.html
22
+ format.xml { render :xml => @form }
23
+ end
24
+ end
25
+
26
+ def new
27
+ @form = Noodall::Form.new
28
+ @form.create_mandatory_fields!
29
+
30
+ respond_to do |format|
31
+ format.html { render :action => "show" }
32
+ format.xml { render :xml => @form }
33
+ end
34
+ end
35
+
36
+ def create
37
+ # convert hashes with stringy integer keys to an array of hashes
38
+ params[:form][:fields] = params[:form][:fields].sort{|a,b| a.first.to_i <=> b.first.to_i }.map{|item| item.last }
39
+
40
+ @form = Noodall::Form.new(params[:form])
41
+
42
+ respond_to do |format|
43
+ if @form.save
44
+ flash[:notice] = "Form was successfully created."
45
+ format.html { redirect_to noodall_admin_forms_path }
46
+ format.xml { render :xml => @form, :status => :created }
47
+ else
48
+ format.html do
49
+ render :action => "show"
50
+ end
51
+ format.xml { render :xml => @form.errors, :status => :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ def update
57
+ # convert hashes with stringy integer keys to an array of hashes
58
+ params[:form][:fields] = params[:form][:fields].sort{|a,b| a.first.to_i <=> b.first.to_i }.map{|item| item.last }
59
+
60
+ logger.debug(params[:form][:fields].inspect)
61
+
62
+ @form = Noodall::Form.find(params[:id])
63
+
64
+ respond_to do |format|
65
+ if @form.update_attributes(params[:form])
66
+ flash[:notice] = "Form was successfully updated."
67
+ format.html {
68
+ redirect_to noodall_admin_forms_path
69
+ }
70
+ format.xml { head :ok }
71
+ else
72
+ format.html { render :action => "show" }
73
+ format.xml { render :xml => @form.errors, :status => :unprocessable_entity }
74
+ end
75
+ end
76
+ end
77
+
78
+ def destroy
79
+ @form = Noodall::Form.find(params[:id])
80
+
81
+ @form.destroy
82
+ flash[:notice] = "Form was successfully deleted."
83
+
84
+ respond_to do |format|
85
+ format.html { redirect_to(noodall_admin_forms_url) }
86
+ format.xml { head :ok }
87
+ end
88
+ end
89
+
90
+
91
+ private
92
+ def set_title
93
+ @page_title = 'Form Builder'
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,39 @@
1
+ module Noodall
2
+ class FormResponsesController < ApplicationController
3
+ # POST /responses
4
+ # POST /responses.xml
5
+ def create
6
+
7
+ # long way of adding a new response to a form (create then append to form model)
8
+ # as redirecting back to the form will cause errors with half built appended objects
9
+ @form = Noodall::Form.find(params[:form_id])
10
+
11
+ @form_response = @form.responses.build(params[:form_response])
12
+
13
+ @form_response.ip = request.remote_ip
14
+ @form_response.referrer = request.referer if @form_response.referrer.blank?
15
+ @form_response.created_at = Time.zone.now
16
+
17
+ respond_to do |format|
18
+ if @form_response.valid? and @form_response.save
19
+
20
+ unless @form_response.is_spam?
21
+ begin
22
+ # mail the response to the form recipient
23
+ FormMailer.form_response(@form, @form_response).deliver unless @form.email.blank?
24
+ FormMailer.form_response_thankyou(@form, @form_response).deliver
25
+ rescue Net::SMTPSyntaxError
26
+ end
27
+ end
28
+
29
+ flash[:notice] = 'Thank you for getting in contact.'
30
+ format.html
31
+ format.xml { render :xml => @form, :status => :created, :location => @form }
32
+ else
33
+ format.html { render 'new' }
34
+ format.xml { render :xml => @form_response.errors, :status => :unprocessable_entity }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ module Admin::FormsHelper
2
+ end
@@ -0,0 +1,11 @@
1
+ module FormsHelper
2
+
3
+ def response_setup(form)
4
+ # collect the default values together
5
+ defaults = {}
6
+ form.fields.collect{|f| defaults[f.underscored_name.to_sym] = f.default } unless form.nil? || form.fields.nil?
7
+
8
+ Noodall::FormResponse.new(defaults)
9
+ end
10
+
11
+ end
@@ -0,0 +1,21 @@
1
+ class FormMailer < ActionMailer::Base
2
+ default :from => "from@example.com"
3
+
4
+ def form_response(form, response)
5
+ @form = form
6
+ @response = response
7
+
8
+ mail(:to => form.email,
9
+ :reply_to => response.email,
10
+ :subject => "[#{Noodall::UI.app_name}] Response to the #{form.title} form.")
11
+ end
12
+
13
+ def form_response_thankyou(form, response)
14
+ @form = form
15
+ @response = response
16
+
17
+ mail(:to => response.email,
18
+ :reply_to => form.email,
19
+ :subject => "[#{Noodall::UI.app_name}] Thank you for getting in contact.")
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ class ContactForm < Noodall::Component
2
+ key :form_id, String
3
+
4
+ has_one :form, :class => Noodall::Form
5
+
6
+ allowed_positions :wide
7
+
8
+ module ClassMethods
9
+ def form_options
10
+ lists = Noodall::Form.all(:order => 'title ASC')
11
+ lists.collect{|l| [l.title, l.id.to_s]}
12
+ end
13
+
14
+ end
15
+ extend ClassMethods
16
+ end
@@ -0,0 +1,5 @@
1
+ module Noodall
2
+ class CheckBox < Noodall::Field
3
+ key :default, Boolean
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ module Noodall
2
+ class Field
3
+ include MongoMapper::EmbeddedDocument
4
+
5
+ key :_type, String, :required => true
6
+ key :name, String, :required => true
7
+ key :label, String
8
+ key :default, String
9
+ key :required, Boolean, :default => false, :required => true
10
+
11
+ embedded_in :form
12
+
13
+ def underscored_name
14
+ name.parameterize('_').to_s
15
+ end
16
+
17
+ def default_class(response)
18
+ 'default-value' if response.send(underscored_name.to_sym) == default
19
+ end
20
+
21
+ def mandatory?
22
+ Form::MANDATORY_FIELDS.include?(self.name)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ module Noodall
2
+ class Form
3
+ include MongoMapper::Document
4
+ plugin MongoMapper::Plugins::MultiParameterAttributes
5
+ plugin Noodall::GlobalUpdateTime
6
+
7
+ key :title, String, :required => true
8
+ key :description, String
9
+ key :email, String, :format => /.+\@.+\..+/
10
+
11
+ MANDATORY_FIELDS = ['Name','Email']
12
+ many :fields, :class => Noodall::Field
13
+ many :responses, :class => Noodall::FormResponse
14
+
15
+ before_save :create_mandatory_fields!
16
+
17
+ timestamps!
18
+
19
+ validates_associated :fields, :message => "have not had a name completed"
20
+
21
+ def boolean_fields
22
+ self.fields.select{|f| f.class == Noodall::CheckBox }
23
+ end
24
+
25
+ def required_fields
26
+ self.fields.select{|f| f.required }
27
+ end
28
+
29
+ def create_mandatory_fields!
30
+ MANDATORY_FIELDS.each do |mf|
31
+ if fields.blank? or fields.select{|f| f.name == mf }.empty?
32
+ self.fields << Noodall::TextField.new(:name => mf, :required => true)
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end