formbuilder-rb 0.0.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +14 -0
  7. data/Gemfile.lock +219 -0
  8. data/Guardfile +17 -0
  9. data/{MIT-LICENSE → LICENSE.md} +1 -1
  10. data/README.md +86 -0
  11. data/app/controllers/formbuilder/concerns/forms_controller.rb +59 -0
  12. data/app/models/formbuilder/entry_attachment.rb +2 -8
  13. data/app/models/formbuilder/form.rb +53 -0
  14. data/app/models/formbuilder/response_field.rb +23 -10
  15. data/app/models/formbuilder/response_field_address.rb +12 -1
  16. data/app/models/formbuilder/response_field_checkboxes.rb +38 -0
  17. data/app/models/formbuilder/response_field_date.rb +7 -1
  18. data/app/models/formbuilder/response_field_dropdown.rb +2 -1
  19. data/app/models/formbuilder/response_field_email.rb +14 -2
  20. data/app/models/formbuilder/response_field_file.rb +75 -20
  21. data/app/models/formbuilder/response_field_number.rb +9 -2
  22. data/app/models/formbuilder/response_field_page_break.rb +13 -0
  23. data/app/models/formbuilder/response_field_paragraph.rb +19 -3
  24. data/app/models/formbuilder/response_field_price.rb +10 -6
  25. data/app/models/formbuilder/response_field_radio.rb +11 -2
  26. data/app/models/formbuilder/response_field_section_break.rb +12 -2
  27. data/app/models/formbuilder/response_field_table.rb +222 -0
  28. data/app/models/formbuilder/response_field_text.rb +9 -2
  29. data/app/models/formbuilder/response_field_time.rb +8 -1
  30. data/app/models/formbuilder/response_field_website.rb +26 -2
  31. data/app/uploaders/formbuilder/entry_attachment_uploader.rb +11 -1
  32. data/bin/rails +8 -0
  33. data/circle.yml +9 -0
  34. data/config/spring.rb +1 -0
  35. data/db/migrate/20130924185815_create_formbuilder_entry_attachments.rb +1 -0
  36. data/formbuilder-rb.gemspec +41 -0
  37. data/formbuilder-rb.sublime-project +20 -0
  38. data/lib/formbuilder.rb +8 -3
  39. data/lib/formbuilder/entry.rb +102 -159
  40. data/lib/formbuilder/entry_validator.rb +13 -6
  41. data/lib/formbuilder/version.rb +1 -1
  42. data/lib/formbuilder/views/entry_dl.rb +56 -0
  43. data/lib/formbuilder/views/form.rb +81 -0
  44. data/lib/formbuilder/views/form_field.rb +45 -0
  45. data/spec/controllers/formbuilder/forms_controller_spec.rb +40 -0
  46. data/spec/dummy/.ruby-gemset +1 -0
  47. data/spec/dummy/.ruby-version +1 -0
  48. data/spec/dummy/README.rdoc +28 -0
  49. data/spec/dummy/Rakefile +6 -0
  50. data/spec/dummy/app/assets/images/.keep +0 -0
  51. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  52. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  53. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  54. data/spec/dummy/app/controllers/forms_controller.rb +3 -0
  55. data/spec/dummy/app/controllers/test_controller.rb +57 -0
  56. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  57. data/spec/dummy/app/mailers/.keep +0 -0
  58. data/spec/dummy/app/models/concerns/.keep +0 -0
  59. data/spec/dummy/app/models/entry.rb +7 -0
  60. data/spec/dummy/app/models/entry_with_alternate_column_name.rb +11 -0
  61. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  62. data/spec/dummy/app/views/test/_form.rb +2 -0
  63. data/spec/dummy/app/views/test/render_entry.html.erb +1 -0
  64. data/spec/dummy/app/views/test/show_form.html.erb +1 -0
  65. data/spec/dummy/bin/bundle +3 -0
  66. data/spec/dummy/bin/rails +4 -0
  67. data/spec/dummy/bin/rake +4 -0
  68. data/spec/dummy/config.ru +4 -0
  69. data/spec/dummy/config/application.rb +25 -0
  70. data/spec/dummy/config/boot.rb +5 -0
  71. data/spec/dummy/config/database.yml.ci +15 -0
  72. data/spec/dummy/config/database.yml.example +15 -0
  73. data/spec/dummy/config/environment.rb +5 -0
  74. data/spec/dummy/config/environments/development.rb +29 -0
  75. data/spec/dummy/config/environments/production.rb +80 -0
  76. data/spec/dummy/config/environments/test.rb +52 -0
  77. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  78. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  79. data/spec/dummy/config/initializers/inflections.rb +16 -0
  80. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  81. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  82. data/spec/dummy/config/initializers/session_store.rb +3 -0
  83. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  84. data/spec/dummy/config/locales/en.yml +23 -0
  85. data/spec/dummy/config/routes.rb +12 -0
  86. data/spec/dummy/db/migrate/20130924182939_create_entries.rb +13 -0
  87. data/spec/dummy/db/migrate/20130924192151_create_formbuilder_forms.formbuilder.rb +11 -0
  88. data/spec/dummy/db/migrate/20130924192152_create_formbuilder_response_fields.formbuilder.rb +17 -0
  89. data/spec/dummy/db/migrate/20130926205845_create_formbuilder_entry_attachments.formbuilder.rb +12 -0
  90. data/spec/dummy/db/migrate/20140128000000_create_entry_with_alternate_column_names.rb +13 -0
  91. data/spec/dummy/db/schema.rb +64 -0
  92. data/spec/dummy/lib/assets/.keep +0 -0
  93. data/spec/dummy/log/.keep +0 -0
  94. data/spec/dummy/public/404.html +58 -0
  95. data/spec/dummy/public/422.html +58 -0
  96. data/spec/dummy/public/500.html +57 -0
  97. data/spec/dummy/public/favicon.ico +0 -0
  98. data/spec/dummy/test/fixtures/entries.yml +9 -0
  99. data/spec/dummy/test/fixtures/response_fields.yml +19 -0
  100. data/spec/dummy/test/models/entry_test.rb +7 -0
  101. data/spec/dummy/test/models/response_field_test.rb +7 -0
  102. data/spec/factories/formbuilder_forms.rb +45 -0
  103. data/spec/features/form_renderer_spec.rb +30 -0
  104. data/spec/features/submitting_an_entry_spec.rb +79 -0
  105. data/spec/fixtures/test_files/text.txt +1 -0
  106. data/spec/fixtures/test_files/text2.txt +1 -0
  107. data/spec/lib/formbuilder/entry_spec.rb +199 -0
  108. data/spec/lib/formbuilder/entry_validator_spec.rb +215 -0
  109. data/spec/lib/formbuilder/entry_with_alternate_column_name_spec.rb +37 -0
  110. data/spec/lib/formbuilder/views/entry_dl_spec.rb +38 -0
  111. data/spec/models/formbuilder/entry_attachment_spec.rb +17 -0
  112. data/spec/models/formbuilder/form_spec.rb +100 -0
  113. data/spec/models/formbuilder/response_field_address_spec.rb +25 -0
  114. data/spec/models/formbuilder/response_field_checkboxes_spec.rb +57 -0
  115. data/spec/models/formbuilder/response_field_file_spec.rb +80 -0
  116. data/spec/models/formbuilder/response_field_paragraph_spec.rb +22 -0
  117. data/spec/models/formbuilder/response_field_radio_spec.rb +30 -0
  118. data/spec/models/formbuilder/response_field_spec.rb +21 -0
  119. data/spec/models/formbuilder/response_field_table_spec.rb +124 -0
  120. data/spec/spec_helper.rb +25 -0
  121. data/spec/support/database_cleaner.rb +21 -0
  122. data/spec/support/submitting_an_entry_spec_helper.rb +151 -0
  123. metadata +286 -44
  124. data/app/controllers/formbuilder/forms_controller.rb +0 -53
  125. data/lib/formbuilder/entry_renderer.rb +0 -47
  126. data/lib/formbuilder/entry_table_renderer.rb +0 -58
  127. data/lib/formbuilder/form_renderer.rb +0 -102
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e4893ebfa92c93b1c9e306f32cc8b5ed48aaf33
4
- data.tar.gz: f83905df1c6d607cdf3ea7d3bc5fff8d497952e8
3
+ metadata.gz: ba39021b688857fba2a29ba89b37132da3f41aa7
4
+ data.tar.gz: 76670efaf98c3781c137996f4a18f4151578795a
5
5
  SHA512:
6
- metadata.gz: 2ae9b52dc25a6537a05b0761112e452679f8777181a1d2d368d2a2f2e93323a5b310d5da779cfd2d20c799f3e4de4c5511882095ae30dae1058dc3f3449733f1
7
- data.tar.gz: 5dda69d10ba76a7b43f28d82a86a740fa35150a56a98a6ebe419554520eff56cabdbc51a72693fa1143d3ab19cc61a7c54092288a45f104c89be27f4b2ece2e0
6
+ metadata.gz: 44772ca06fb009e7bd3c2e2b220814c07d233687a98921c4c657c53b388ac6b218e712cdc2403a8b728b3caf9198f38c4be2416e589c9349dffdd8736ae46bb2
7
+ data.tar.gz: 5b0e3f312708d04ade5ec4ec3d7c75a448a8c85156c04ac59ceb6a80d61e599425098afe62785c2e3f11ea1bccbc3c26aded6107f82c97b86649bd6bc14259c0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/config/database.yml
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/.sass-cache
8
+ spec/dummy/public/uploads/*
9
+ tmp/*
10
+ coverage/*
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ formbuilder-rb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in formbuilder.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'debugger'
data/Gemfile.lock ADDED
@@ -0,0 +1,219 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ formbuilder-rb (0.2.0)
5
+ carrierwave
6
+ erector-rails4
7
+ geocoder
8
+ pg
9
+ rails (= 4.1.0.rc1)
10
+ rinku
11
+ rmagick
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ actionmailer (4.1.0.rc1)
17
+ actionpack (= 4.1.0.rc1)
18
+ actionview (= 4.1.0.rc1)
19
+ mail (~> 2.5.4)
20
+ actionpack (4.1.0.rc1)
21
+ actionview (= 4.1.0.rc1)
22
+ activesupport (= 4.1.0.rc1)
23
+ rack (~> 1.5.2)
24
+ rack-test (~> 0.6.2)
25
+ actionview (4.1.0.rc1)
26
+ activesupport (= 4.1.0.rc1)
27
+ builder (~> 3.1)
28
+ erubis (~> 2.7.0)
29
+ activemodel (4.1.0.rc1)
30
+ activesupport (= 4.1.0.rc1)
31
+ builder (~> 3.1)
32
+ activerecord (4.1.0.rc1)
33
+ activemodel (= 4.1.0.rc1)
34
+ activesupport (= 4.1.0.rc1)
35
+ arel (~> 5.0.0)
36
+ activesupport (4.1.0.rc1)
37
+ i18n (~> 0.6, >= 0.6.9)
38
+ json (~> 1.7, >= 1.7.7)
39
+ minitest (~> 5.1)
40
+ thread_safe (~> 0.1)
41
+ tzinfo (~> 1.1)
42
+ addressable (2.3.5)
43
+ arel (5.0.0)
44
+ atomic (1.1.15)
45
+ builder (3.2.2)
46
+ capybara (2.2.1)
47
+ mime-types (>= 1.16)
48
+ nokogiri (>= 1.3.3)
49
+ rack (>= 1.0.0)
50
+ rack-test (>= 0.5.4)
51
+ xpath (~> 2.0)
52
+ carrierwave (0.10.0)
53
+ activemodel (>= 3.2.0)
54
+ activesupport (>= 3.2.0)
55
+ json (>= 1.7)
56
+ mime-types (>= 1.16)
57
+ celluloid (0.15.2)
58
+ timers (~> 1.1.0)
59
+ celluloid-io (0.15.0)
60
+ celluloid (>= 0.15.0)
61
+ nio4r (>= 0.5.0)
62
+ coderay (1.1.0)
63
+ coveralls (0.7.0)
64
+ multi_json (~> 1.3)
65
+ rest-client
66
+ simplecov (>= 0.7)
67
+ term-ansicolor
68
+ thor
69
+ daemons (1.1.9)
70
+ database_cleaner (1.2.0)
71
+ diff-lcs (1.2.5)
72
+ docile (1.1.3)
73
+ erector-rails4 (0.1.0)
74
+ rails (= 4.1.0.rc1)
75
+ treetop (>= 1.2.3)
76
+ erubis (2.7.0)
77
+ eventmachine (1.0.3)
78
+ factory_girl (4.4.0)
79
+ activesupport (>= 3.0.0)
80
+ factory_girl_rails (4.4.1)
81
+ factory_girl (~> 4.4.0)
82
+ railties (>= 3.0.0)
83
+ ffi (1.9.3)
84
+ formatador (0.2.4)
85
+ geocoder (1.1.9)
86
+ guard (2.5.1)
87
+ formatador (>= 0.2.4)
88
+ listen (~> 2.6)
89
+ lumberjack (~> 1.0)
90
+ pry (>= 0.9.12)
91
+ thor (>= 0.18.1)
92
+ guard-rspec (4.2.8)
93
+ guard (~> 2.1)
94
+ rspec (>= 2.14, < 4.0)
95
+ hike (1.2.3)
96
+ i18n (0.6.9)
97
+ json (1.8.1)
98
+ launchy (2.4.2)
99
+ addressable (~> 2.3)
100
+ listen (2.7.0)
101
+ celluloid (>= 0.15.2)
102
+ celluloid-io (>= 0.15.0)
103
+ rb-fsevent (>= 0.9.3)
104
+ rb-inotify (>= 0.9)
105
+ lumberjack (1.0.4)
106
+ mail (2.5.4)
107
+ mime-types (~> 1.16)
108
+ treetop (~> 1.4.8)
109
+ method_source (0.8.2)
110
+ mime-types (1.25.1)
111
+ mini_portile (0.5.2)
112
+ minitest (5.3.0)
113
+ multi_json (1.9.0)
114
+ nio4r (1.0.0)
115
+ nokogiri (1.6.1)
116
+ mini_portile (~> 0.5.0)
117
+ pg (0.17.1)
118
+ polyglot (0.3.4)
119
+ pry (0.9.12.6)
120
+ coderay (~> 1.0)
121
+ method_source (~> 0.8)
122
+ slop (~> 3.4)
123
+ rack (1.5.2)
124
+ rack-test (0.6.2)
125
+ rack (>= 1.0)
126
+ rails (4.1.0.rc1)
127
+ actionmailer (= 4.1.0.rc1)
128
+ actionpack (= 4.1.0.rc1)
129
+ actionview (= 4.1.0.rc1)
130
+ activemodel (= 4.1.0.rc1)
131
+ activerecord (= 4.1.0.rc1)
132
+ activesupport (= 4.1.0.rc1)
133
+ bundler (>= 1.3.0, < 2.0)
134
+ railties (= 4.1.0.rc1)
135
+ sprockets-rails (~> 2.0.0)
136
+ railties (4.1.0.rc1)
137
+ actionpack (= 4.1.0.rc1)
138
+ activesupport (= 4.1.0.rc1)
139
+ rake (>= 0.8.7)
140
+ thor (>= 0.18.1, < 2.0)
141
+ rake (10.1.1)
142
+ rb-fsevent (0.9.4)
143
+ rb-inotify (0.9.3)
144
+ ffi (>= 0.5.0)
145
+ rest-client (1.6.7)
146
+ mime-types (>= 1.16)
147
+ rinku (1.7.3)
148
+ rmagick (2.13.2)
149
+ rspec (2.14.1)
150
+ rspec-core (~> 2.14.0)
151
+ rspec-expectations (~> 2.14.0)
152
+ rspec-mocks (~> 2.14.0)
153
+ rspec-core (2.14.8)
154
+ rspec-expectations (2.14.5)
155
+ diff-lcs (>= 1.1.3, < 2.0)
156
+ rspec-mocks (2.14.6)
157
+ rspec-rails (2.14.1)
158
+ actionpack (>= 3.0)
159
+ activemodel (>= 3.0)
160
+ activesupport (>= 3.0)
161
+ railties (>= 3.0)
162
+ rspec-core (~> 2.14.0)
163
+ rspec-expectations (~> 2.14.0)
164
+ rspec-mocks (~> 2.14.0)
165
+ simplecov (0.8.2)
166
+ docile (~> 1.1.0)
167
+ multi_json
168
+ simplecov-html (~> 0.8.0)
169
+ simplecov-html (0.8.0)
170
+ slop (3.4.7)
171
+ spring (1.1.2)
172
+ spring-commands-rspec (1.0.1)
173
+ spring (>= 0.9.1)
174
+ sprockets (2.11.0)
175
+ hike (~> 1.2)
176
+ multi_json (~> 1.0)
177
+ rack (~> 1.0)
178
+ tilt (~> 1.1, != 1.3.0)
179
+ sprockets-rails (2.0.1)
180
+ actionpack (>= 3.0)
181
+ activesupport (>= 3.0)
182
+ sprockets (~> 2.8)
183
+ term-ansicolor (1.3.0)
184
+ tins (~> 1.0)
185
+ terminal-notifier-guard (1.5.3)
186
+ thin (1.6.2)
187
+ daemons (>= 1.0.9)
188
+ eventmachine (>= 1.0.0)
189
+ rack (>= 1.0.0)
190
+ thor (0.18.1)
191
+ thread_safe (0.2.0)
192
+ atomic (>= 1.1.7, < 2)
193
+ tilt (1.4.1)
194
+ timers (1.1.0)
195
+ tins (1.0.0)
196
+ treetop (1.4.15)
197
+ polyglot
198
+ polyglot (>= 0.3.1)
199
+ tzinfo (1.1.0)
200
+ thread_safe (~> 0.1)
201
+ xpath (2.0.0)
202
+ nokogiri (~> 1.3)
203
+
204
+ PLATFORMS
205
+ ruby
206
+
207
+ DEPENDENCIES
208
+ capybara
209
+ coveralls
210
+ database_cleaner
211
+ factory_girl_rails
212
+ formbuilder-rb!
213
+ guard-rspec
214
+ launchy
215
+ rspec-rails
216
+ spring
217
+ spring-commands-rspec
218
+ terminal-notifier-guard
219
+ thin
data/Guardfile ADDED
@@ -0,0 +1,17 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ group :all_specs do
5
+ guard :rspec, all_on_start: false, all_after_pass: false,
6
+ cmd: 'spring rspec' do
7
+
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
+ watch(%r{^app/models/formbuilder/(.+)\.rb$}) { |m| "app/models/formbuilder/#{m[1]}_spec.rb" }
14
+ watch(%r{^lib/formbuilder/(.+)\.rb$}) { |m| "spec/lib/formbuilder/#{m[1]}_spec.rb" }
15
+
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
- Copyright 2013 YOURNAME
1
+ Copyright 2013 Department of Better Technology
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ #### Alert: This gem is used internally at [DOBT](https://github.com/dobtco), and might not be perfect for your implementation. We recommend forking this repository and using it as a starting opint.
2
+
3
+ Formbuilder.rb [![circle ci status](https://circleci.com/gh/dobtco/formbuilder-rb.png?circle-token=a769ad2fc81271bc1869b5e5a95053efa36b376f)](https://circleci.com/gh/dobtco/formbuilder-rb) <a href='https://coveralls.io/r/dobtco/formbuilder-rb'><img src='https://coveralls.io/repos/dobtco/formbuilder-rb/badge.png' alt='Coverage Status' /></a>
4
+ ========
5
+
6
+ Formbuilder.rb is a [Rails Engine](http://edgeguides.rubyonrails.org/engines.html) that's designed as a compliment to [Formbuilder.js](https://github.com/dobtco/formbuilder), a library that lets your users create their own webforms inside of your application.
7
+
8
+ Since Formbuilder.rb is a fairly non-trial piece of software, it's important to understand its components and how it works:
9
+
10
+ 1. We add `ResponseField`, `EntryAttachment`, and `Form` models to your application. (Each type of response field (text, checkboxes, dropdown, etc.) uses [STI](blog.thirst.co/post/14885390861/rails-single-table-inheritance‎), inheriting from the `ResponseField` model.)
11
+ 3. You `include Formbuilder::Entry` in an existing model.
12
+ 4. We add a few classes to help you render forms and entries.
13
+
14
+ *Note: All Formbuilder models and classes are namespaced within the `Formbuilder` module.*
15
+
16
+ If you have a few moments, consider reading the source, especially the Rails app in `spec/dummy`, as it should give you a good idea of how Formbuilder integrates.
17
+
18
+ ### Requirements
19
+
20
+ **Postgres is currently required.** See [Issue #1](https://github.com/dobtco/formbuilder-rb/issues/1).
21
+
22
+ [Carrierwave](https://github.com/carrierwaveuploader/carrierwave) and [Rmagick](https://github.com/rmagick/rmagick) for file uploads.
23
+ [Geocoder](https://github.com/alexreisner/geocoder) to geocode address fields.
24
+
25
+
26
+ ### Installation
27
+ #### 1) In your Gemfile
28
+ `gem 'formbuilder-rb', require: 'formbuilder'`
29
+
30
+ #### 2) Create the migrations for the Formbuilder models
31
+ 1. `rake formbuilder:install:migrations`
32
+ 2. `rake db:migrate`
33
+
34
+ #### 3) The `Entry` model gets mixed in to an existing model in your application
35
+ ```ruby
36
+ # responses :hstore
37
+ # responses_text :text
38
+
39
+ class Entry < ActiveRecord::Base
40
+ include Formbuilder::Entry
41
+ end
42
+ ```
43
+
44
+ #### 4) Associate a form with an existing model (optional)
45
+ ```ruby
46
+ class MovieTheater < ActiveRecord::Base
47
+ has_one :form, as: :formable, class_name: 'Formbuilder::Form'
48
+ end
49
+ ```
50
+
51
+ ### Usage
52
+
53
+ #### Render a form
54
+ ```erb
55
+ <%= Formbuilder::FormRenderer.new(@form, @entry).to_html %>
56
+ ```
57
+
58
+ #### Save an entry
59
+ ```ruby
60
+ @entry = Entry.new(form: @form)
61
+ @entry.save_responses(params[:response_fields], @form.response_fields) # validates automatically
62
+ ```
63
+
64
+ #### Validate an entry
65
+ ```ruby
66
+ @entry.valid?
67
+ # => false
68
+
69
+ @entry.error_for(form.response_fields.first)
70
+ # => "can't be blank"
71
+ ```
72
+
73
+ #### Integrate with the [Formbuilder.js](https://github.com/dobtco/formbuilder) frontend
74
+ ```ruby
75
+ # config/routes.rb
76
+ resources :forms, only: [:update]
77
+
78
+ # app/controllers/forms_controller.rb
79
+ class FormsController < ApplicationController
80
+ include Formbuilder::Concerns::FormsController
81
+ end
82
+ ```
83
+
84
+ #### License
85
+
86
+ MIT
@@ -0,0 +1,59 @@
1
+ # A controller to integrate with the Formbuilder.js frontend (https://github.com/dobtco/formbuilder)
2
+
3
+ module Formbuilder
4
+ module Concerns
5
+ module FormsController
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ before_filter :load_form, only: [:update]
10
+ end
11
+
12
+ def update
13
+ existing_response_field_ids = []
14
+ cids = {}
15
+
16
+ (params[:fields] || []).each_with_index do |field_params, i|
17
+ response_field = field_params[:id].present? ?
18
+ @form.response_fields.find { |rf| rf.id == field_params[:id].to_i } :
19
+ @form.response_fields.build
20
+
21
+ response_field.update_attributes(transformed_field_params(field_params, i))
22
+ cids[response_field.id] = field_params[:cid]
23
+ existing_response_field_ids.push response_field.id
24
+ end
25
+
26
+ # destroy fields that no longer exist
27
+ @form.response_fields.each do |response_field|
28
+ response_field.destroy unless response_field.id.in?(existing_response_field_ids)
29
+ end
30
+
31
+ @form.response_fields.reload
32
+
33
+ @form.response_fields.each do |rf|
34
+ rf.cid = cids[rf.id]
35
+ end
36
+
37
+ render json: @form.response_fields_json
38
+ end
39
+
40
+ private
41
+ def load_form
42
+ @form = Formbuilder::Form.find(params[:id])
43
+ end
44
+
45
+ def transformed_field_params(field_params, i)
46
+ filtered_params = field_params.select { |k, _|
47
+ k.to_sym.in?(Formbuilder::ResponseField::ALLOWED_PARAMS)
48
+ }.merge(
49
+ sort_order: i,
50
+ type: "Formbuilder::ResponseField#{field_params[:field_type].camelize}"
51
+ )
52
+
53
+ filtered_params.permit!
54
+ filtered_params
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -2,14 +2,8 @@ module Formbuilder
2
2
  class EntryAttachment < ActiveRecord::Base
3
3
 
4
4
  mount_uploader :upload, Formbuilder::EntryAttachmentUploader
5
- before_save :update_upload_attributes
6
5
 
7
- private
8
- def update_upload_attributes
9
- if upload.present? && upload_changed?
10
- self.content_type = upload.file.content_type
11
- end
12
- end
6
+ validates :upload, presence: true
13
7
 
14
8
  end
15
- end
9
+ end