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
@@ -1,53 +0,0 @@
1
- # A controller to integrate with the Formbuilder.js frontend (https://github.com/dobtco/formbuilder)
2
-
3
- module Formbuilder
4
- class FormsController < ApplicationController
5
-
6
- before_filter :load_form
7
-
8
- protect_from_forgery except: :update
9
-
10
- def update
11
- existing_response_field_ids = []
12
- cids = {}
13
-
14
- (params[:fields] || []).each_with_index do |field_params, i|
15
- response_field = field_params[:id].present? ?
16
- @form.response_fields.find { |rf| rf.id == field_params[:id].to_i } :
17
- @form.response_fields.build
18
-
19
- response_field.update_attributes(transformed_field_params(field_params, i))
20
- cids[response_field.id] = field_params[:cid]
21
- existing_response_field_ids.push response_field.id
22
- end
23
-
24
- # destroy fields that no longer exist
25
- @form.response_fields.each do |response_field|
26
- response_field.destroy unless response_field.id.in?(existing_response_field_ids)
27
- end
28
-
29
- @form.response_fields.reload
30
-
31
- @form.response_fields.each do |rf|
32
- rf.cid = cids[rf.id]
33
- end
34
-
35
- render json: @form.response_fields_json
36
- end
37
-
38
- private
39
- def load_form
40
- @form = Formbuilder::Form.find(params[:id])
41
- end
42
-
43
- def transformed_field_params(field_params, i)
44
- filtered_params = field_params.select { |k, _|
45
- k.to_sym.in?(Formbuilder::ResponseField::ALLOWED_PARAMS)
46
- }.merge(
47
- sort_order: i,
48
- type: "Formbuilder::ResponseField#{field_params[:field_type].camelize}"
49
- )
50
- end
51
-
52
- end
53
- end
@@ -1,47 +0,0 @@
1
- module Formbuilder
2
- class EntryRenderer
3
- include ActionView::Context
4
- include ActionView::Helpers::TagHelper
5
-
6
- def initialize(entry, form, opts = {})
7
- @entry, @form = entry, form
8
- @options = opts # merge defaults?
9
- end
10
-
11
- def fields
12
- return_fields = @form.response_fields.reject { |rf| !rf.input_field }
13
- return_fields.reject! { |rf| rf.blind? } unless @options[:show_blind]
14
- return_fields
15
- end
16
-
17
- def to_html
18
- content_tag 'dl', class: 'entry-dl' do
19
- fields.map do |rf|
20
- """
21
- <dt>#{field_labels(rf)}</dt>
22
- <dd>
23
- #{field_value(rf) || no_value}
24
- </dd>
25
- """
26
- end.join('').html_safe
27
- end
28
- end
29
-
30
- def field_labels(rf)
31
- """
32
- #{rf.label}
33
- #{rf.blind? ? '<span class="label">Blind</span>' : ''}
34
- #{rf.admin_only? ? '<span class="label">Admin Only</span>' : ''}
35
- """
36
- end
37
-
38
- def no_value
39
- "<span class='no-response'>No response</span>"
40
- end
41
-
42
- def field_value(rf)
43
- value = @entry.response_value(rf)
44
- rf.render_entry(value, entry: @entry)
45
- end
46
- end
47
- end
@@ -1,58 +0,0 @@
1
- module Formbuilder
2
- class EntryTableRenderer < EntryRenderer
3
- # include ActionView::Context
4
- # include ActionView::Helpers::TagHelper
5
-
6
- # def initialize(entry, form, opts = {})
7
- # @entry, @form = entry, form
8
- # @options = opts # merge defaults?
9
- # end
10
-
11
- # def fields
12
- # return_fields = @form.response_fields.reject { |rf| rf.non_input_field? }
13
- # return_fields.reject! { |rf| rf.blind? } unless @options[:show_blind]
14
- # return_fields
15
- # end
16
-
17
- def to_html
18
- content_tag 'dl', class: 'entry-dl' do
19
- fields.map do |rf|
20
- field_value(rf)
21
- end.join('').html_safe
22
- end
23
- end
24
-
25
- def field_value(rf)
26
- value = @entry.response_value(rf)
27
- rf.field_class.render_entry_for_table(rf, value, entry: @entry)
28
- end
29
-
30
- # def field_labels(rf)
31
- # """
32
- # #{rf.label}
33
- # #{rf.blind? ? '<span class="label">Blind</span>' : ''}
34
- # #{rf.admin_only? ? '<span class="label">Admin Only</span>' : ''}
35
- # """
36
- # end
37
-
38
- # def no_value
39
- # "<span class='no-response'>No response</span>"
40
- # end
41
-
42
- # def field_value(rf)
43
- # value = @entry.response_value(rf)
44
- # Formbuilder::Fields.const_get(rf.field_type.camelize + 'Field').render_entry(rf, value, entry: @entry)
45
- # end
46
-
47
- # def checkboxes_for_table
48
- # (@response_field.field_options['options'] || []).map do |option|
49
- # """
50
- # <td data-column-id='#{@response_field.id}_sortable_values_#{option['label']}'>
51
- # <i class='#{@value && @value[option['label']] ? 'icon-ok' : 'icon-remove'}'></i>
52
- # </td>
53
- # """
54
- # end.join('')
55
- # end
56
-
57
- end
58
- end
@@ -1,102 +0,0 @@
1
- module Formbuilder
2
- class FormRenderer
3
-
4
- include ActionView::Helpers::UrlHelper
5
- include ActionView::Helpers::FormTagHelper
6
- include ActionView::Context
7
-
8
- def protect_against_forgery?; false; end;
9
-
10
- DEFAULT_OPTIONS = {
11
- action: '',
12
- method: 'POST'
13
- }
14
-
15
- def initialize(form, entry, options = {})
16
- @form, @entry = form, entry
17
- @options = self.class::DEFAULT_OPTIONS.merge(options)
18
- end
19
-
20
- def to_html
21
- form_tag @options[:action], method: @options[:method], class: 'formbuilder-form', multipart: true do
22
- hidden_fields +
23
- render_fields +
24
- render_actions
25
- end
26
- end
27
-
28
- def hidden_fields
29
- """
30
- <input type='hidden' name='draft_only' />
31
- """.html_safe
32
- end
33
-
34
- def render_fields
35
- @form.response_fields.map do |field|
36
- @field = field
37
- render_field
38
- end.join('').html_safe
39
- end
40
-
41
- def render_field
42
- value = @entry.try(:response_value, @field)
43
-
44
- """
45
- <div class='fb-field-wrapper response-field-#{@field.field_type} #{@entry.try(:error_for, @field) && 'error'}'>
46
- #{render_label}
47
- #{@field.render_input(value, entry: @entry)}
48
- <div class='cf'></div>
49
- #{render_error}
50
- #{render_description}
51
- #{render_length_validations}
52
- #{render_min_max_validations}
53
- </div>
54
- """
55
- end
56
-
57
- def render_label
58
- return unless @field.input_field
59
-
60
- """
61
- <label>
62
- #{@field[:label]}
63
- #{render_label_required if @field.required?}
64
- </label>
65
- """
66
- end
67
-
68
- def render_label_required
69
- "<abbr title='required'>*</abbr>"
70
- end
71
-
72
- def render_error
73
- return unless @field.input_field
74
- return unless @entry.error_for(@field)
75
- "<span class='help-block validation-message-wrapper'>#{@entry.error_for(@field)}</span>"
76
- end
77
-
78
- def render_description
79
- return unless @field.input_field
80
- return if @field[:field_options]["description"].blank?
81
- "<span class='help-block'>#{simple_format(@field[:field_options]["description"])}</span>"
82
- end
83
-
84
- def render_length_validations
85
- return unless @field.input_field
86
- return unless !@field.has_length_validations?
87
- end
88
-
89
- def render_min_max_validations
90
- return unless @field.input_field
91
- end
92
-
93
- def render_actions
94
- """
95
- <div class='form-actions'>
96
- <button class='button primary'>Submit</button>
97
- <a class='button save-draft-button' data-loading-text='All changes saved'>Save draft</a>
98
- </div>
99
- """.html_safe
100
- end
101
- end
102
- end