surveyor_gui 0.0.3

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.
Files changed (283) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +44 -0
  3. data/.travis.yml +17 -0
  4. data/Gemfile +7 -0
  5. data/LICENSE +21 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +159 -0
  8. data/Rakefile +66 -0
  9. data/app/controllers/answers_controller.rb +3 -0
  10. data/app/controllers/application_controller.rb +3 -0
  11. data/app/controllers/dependencys_controller.rb +286 -0
  12. data/app/controllers/question_groups_controller.rb +84 -0
  13. data/app/controllers/questions_controller.rb +187 -0
  14. data/app/controllers/survey_sections_controller.rb +80 -0
  15. data/app/controllers/surveyforms_controller.rb +4 -0
  16. data/app/controllers/surveyor_controller.rb +89 -0
  17. data/app/controllers/surveyor_gui/reports_controller.rb +339 -0
  18. data/app/controllers/surveyor_gui/responses_controller.rb +39 -0
  19. data/app/controllers/surveyor_gui/survey_controller.rb +16 -0
  20. data/app/facades/report_formatter.rb +44 -0
  21. data/app/facades/report_preview_wrapper.rb +11 -0
  22. data/app/facades/report_response_generator.rb +147 -0
  23. data/app/helpers/application_helper.rb +2 -0
  24. data/app/helpers/dependency_helper.rb +5 -0
  25. data/app/helpers/question_helper.rb +22 -0
  26. data/app/helpers/surveyform_helper.rb +179 -0
  27. data/app/helpers/surveyor_gui/report_helper.rb +19 -0
  28. data/app/helpers/surveyor_helper.rb +4 -0
  29. data/app/inputs/currency_input.rb +5 -0
  30. data/app/inputs/date_picker_input.rb +7 -0
  31. data/app/inputs/datetime_picker_input.rb +5 -0
  32. data/app/inputs/percent_input.rb +5 -0
  33. data/app/inputs/time_picker_input.rb +5 -0
  34. data/app/mailers/.gitkeep +0 -0
  35. data/app/models/.gitkeep +0 -0
  36. data/app/models/answer.rb +4 -0
  37. data/app/models/column.rb +3 -0
  38. data/app/models/dependency.rb +4 -0
  39. data/app/models/dependency_condition.rb +4 -0
  40. data/app/models/permitted_params_decorator.rb +80 -0
  41. data/app/models/question.rb +4 -0
  42. data/app/models/question_group.rb +4 -0
  43. data/app/models/question_type.rb +7 -0
  44. data/app/models/response.rb +4 -0
  45. data/app/models/response_set.rb +4 -0
  46. data/app/models/row.rb +3 -0
  47. data/app/models/survey.rb +4 -0
  48. data/app/models/survey_section.rb +4 -0
  49. data/app/models/surveyform.rb +103 -0
  50. data/app/uploaders/blob_uploader.rb +48 -0
  51. data/app/views/dependencys/_column.html.erb +1 -0
  52. data/app/views/dependencys/_dependency.html.erb +22 -0
  53. data/app/views/dependencys/_dependency_condition_fields.html.erb +48 -0
  54. data/app/views/dependencys/_form.html.erb +20 -0
  55. data/app/views/dependencys/blank.html.erb +0 -0
  56. data/app/views/dependencys/edit.html.erb +1 -0
  57. data/app/views/dependencys/new.html.erb +1 -0
  58. data/app/views/layouts/application.html.erb +14 -0
  59. data/app/views/layouts/surveyor_gui_blank.html.erb +12 -0
  60. data/app/views/layouts/surveyor_gui_default.html.erb +16 -0
  61. data/app/views/layouts/surveyor_modified.html.erb +14 -0
  62. data/app/views/partials/_answer.html.haml +33 -0
  63. data/app/views/partials/_question.html.haml +33 -0
  64. data/app/views/partials/_question_group.html.haml +73 -0
  65. data/app/views/partials/_section.html.haml +13 -0
  66. data/app/views/question_groups/_form.html.erb +56 -0
  67. data/app/views/question_groups/_group_inline_field.html.erb +21 -0
  68. data/app/views/question_groups/_group_inline_fields.html.erb +25 -0
  69. data/app/views/question_groups/blank.html.erb +0 -0
  70. data/app/views/question_groups/edit.html.erb +1 -0
  71. data/app/views/question_groups/new.html.erb +1 -0
  72. data/app/views/questions/_answer_fields.html.erb +23 -0
  73. data/app/views/questions/_answer_options.html.erb +28 -0
  74. data/app/views/questions/_form.html.erb +65 -0
  75. data/app/views/questions/_grid_dropdown_columns.html.erb +10 -0
  76. data/app/views/questions/_grid_dropdown_fields.html.erb +42 -0
  77. data/app/views/questions/_grid_fields.html.erb +9 -0
  78. data/app/views/questions/_no_picks.html.erb +29 -0
  79. data/app/views/questions/_pick.html +21 -0
  80. data/app/views/questions/_picks.html.erb +12 -0
  81. data/app/views/questions/blank.html.erb +0 -0
  82. data/app/views/questions/edit.html.erb +1 -0
  83. data/app/views/questions/new.html.erb +1 -0
  84. data/app/views/survey_sections/_form.html.erb +13 -0
  85. data/app/views/survey_sections/blank.html.erb +0 -0
  86. data/app/views/survey_sections/edit.html.erb +1 -0
  87. data/app/views/survey_sections/new.html.erb +1 -0
  88. data/app/views/surveyforms/_form.html.erb +50 -0
  89. data/app/views/surveyforms/_question_field.html.erb +148 -0
  90. data/app/views/surveyforms/_question_group.html.erb +116 -0
  91. data/app/views/surveyforms/_question_group_fields.html.erb +3 -0
  92. data/app/views/surveyforms/_question_name_and_number.html.erb +6 -0
  93. data/app/views/surveyforms/_question_section.html.erb +15 -0
  94. data/app/views/surveyforms/_question_wrapper.html.erb +109 -0
  95. data/app/views/surveyforms/_survey_section_fields.html.erb +138 -0
  96. data/app/views/surveyforms/clone_survey.html.erb +13 -0
  97. data/app/views/surveyforms/edit.html.erb +5 -0
  98. data/app/views/surveyforms/index.html.erb +40 -0
  99. data/app/views/surveyforms/new.html.erb +1 -0
  100. data/app/views/surveyforms/show.html.erb +5 -0
  101. data/app/views/surveyor_gui/reports/_graphs.html.haml +21 -0
  102. data/app/views/surveyor_gui/reports/_grid.html.haml +42 -0
  103. data/app/views/surveyor_gui/reports/_grid_dropdown.html.haml +56 -0
  104. data/app/views/surveyor_gui/reports/_repeater.html.haml +28 -0
  105. data/app/views/surveyor_gui/reports/_show_report.html.haml +33 -0
  106. data/app/views/surveyor_gui/reports/_single_question.html.haml +70 -0
  107. data/app/views/surveyor_gui/reports/show.html.erb +14 -0
  108. data/app/views/surveyor_gui/responses/_grid.html.haml +20 -0
  109. data/app/views/surveyor_gui/responses/_grid_dropdown.html.haml +20 -0
  110. data/app/views/surveyor_gui/responses/_repeater.html.haml +22 -0
  111. data/app/views/surveyor_gui/responses/_survey_results.html.haml +40 -0
  112. data/app/views/surveyor_gui/responses/index.html.haml +24 -0
  113. data/app/views/surveyor_gui/responses/show.html.haml +42 -0
  114. data/app/views/surveyor_gui/shared/_grid_comments.html.haml +10 -0
  115. data/app/views/surveyor_gui/shared/_new_line.html.haml +2 -0
  116. data/app/views/surveyor_gui/shared/_pick_comments.html.haml +15 -0
  117. data/app/views/surveyor_gui/shared/_question_number.html.haml +9 -0
  118. data/app/views/surveyor_gui/shared/_report_data.html.haml +24 -0
  119. data/app/views/surveyor_gui/shared/_stars_report_data.html.haml +14 -0
  120. data/config.ru +4 -0
  121. data/config/environment.rb +0 -0
  122. data/config/routes.rb +71 -0
  123. data/db/migrate/20140307204049_add_template_to_surveys.rb +5 -0
  124. data/db/migrate/20140307235607_add_test_data_to_response_sets.rb +5 -0
  125. data/db/migrate/20140308171947_add_original_choice_to_answers.rb +5 -0
  126. data/db/migrate/20140308172118_add_blob_to_responses.rb +5 -0
  127. data/db/migrate/20140308172417_add_modifiable_to_survey_section.rb +5 -0
  128. data/db/migrate/20140308174532_add_modifiable_to_question.rb +5 -0
  129. data/db/migrate/20140308175305_add_dynamically_generate_to_questions.rb +5 -0
  130. data/db/migrate/20140311032923_add_dummy_blob_to_questions.rb +5 -0
  131. data/db/migrate/20140311160609_add_dynamic_source_to_questions.rb +5 -0
  132. data/db/migrate/20140311161714_add_report_code_to_questions.rb +5 -0
  133. data/db/migrate/20140530181134_add_is_comment_to_questions.rb +5 -0
  134. data/db/migrate/20140531012006_add_is_comment_to_answers.rb +5 -0
  135. data/db/migrate/20140531225529_create_rows.rb +9 -0
  136. data/db/migrate/20140601011151_create_columns.rb +11 -0
  137. data/db/migrate/20140602030330_add_column_id_to_answers.rb +5 -0
  138. data/db/migrate/20140603155606_add_column_id_to_responses.rb +5 -0
  139. data/db/migrate/20140606023527_add_column_id_to_dependency_conditions.rb +5 -0
  140. data/db/migrate/20140815165307_add_user_id_to_survey.rb +5 -0
  141. data/lib/assets/.gitkeep +0 -0
  142. data/lib/assets/images/addicon.png +0 -0
  143. data/lib/assets/images/datepicker.gif +0 -0
  144. data/lib/assets/images/delete.gif +0 -0
  145. data/lib/assets/images/delete.png +0 -0
  146. data/lib/assets/images/images/border.png +0 -0
  147. data/lib/assets/images/images/controls.png +0 -0
  148. data/lib/assets/images/images/loading.gif +0 -0
  149. data/lib/assets/images/images/loading_background.png +0 -0
  150. data/lib/assets/images/images/overlay.png +0 -0
  151. data/lib/assets/images/rails.png +0 -0
  152. data/lib/assets/images/star.gif +0 -0
  153. data/lib/assets/javascripts/.gitkeep +0 -0
  154. data/lib/assets/javascripts/surveyor_gui/jquery.MetaData.js +121 -0
  155. data/lib/assets/javascripts/surveyor_gui/jquery.blockUI.js +619 -0
  156. data/lib/assets/javascripts/surveyor_gui/jquery.rating.js +377 -0
  157. data/lib/assets/javascripts/surveyor_gui/jquery.validate.js +1188 -0
  158. data/lib/assets/javascripts/surveyor_gui/surveyor_add_ons.js +10 -0
  159. data/lib/assets/javascripts/surveyor_gui/surveyor_gui.js +1417 -0
  160. data/lib/assets/stylesheets/.gitkeep +0 -0
  161. data/lib/assets/stylesheets/surveyor_gui/jquery.rating.css +12 -0
  162. data/lib/assets/stylesheets/surveyor_gui/surveyor_add_ons.css +3 -0
  163. data/lib/assets/stylesheets/surveyor_gui/surveyor_gui.sass +650 -0
  164. data/lib/enumerable_extenders.rb +31 -0
  165. data/lib/generators/surveyor_gui/install_generator.rb +57 -0
  166. data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_add_ons.js +2 -0
  167. data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_gui_all.js +24 -0
  168. data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_add_ons.css.sass +1 -0
  169. data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_gui_all.css.sass +8 -0
  170. data/lib/generators/surveyor_gui/templates/app/helpers/surveyor_helper.rb +4 -0
  171. data/lib/generators/surveyor_gui/templates/app/models/response_set_user.rb +13 -0
  172. data/lib/generators/surveyor_gui/templates/app/views/layouts/surveyor_gui_default.html.erb +16 -0
  173. data/lib/generators/surveyor_gui/templates/config/initializers/simple_form.rb +140 -0
  174. data/lib/generators/surveyor_gui/templates/config/locales/en.yml +5 -0
  175. data/lib/surveyor_gui.rb +17 -0
  176. data/lib/surveyor_gui/engine.rb +30 -0
  177. data/lib/surveyor_gui/helpers/surveyor_helper_methods.rb +19 -0
  178. data/lib/surveyor_gui/models/answer_methods.rb +24 -0
  179. data/lib/surveyor_gui/models/column_methods.rb +43 -0
  180. data/lib/surveyor_gui/models/dependency_condition_methods.rb +53 -0
  181. data/lib/surveyor_gui/models/dependency_methods.rb +83 -0
  182. data/lib/surveyor_gui/models/question_and_group_shared_methods.rb +11 -0
  183. data/lib/surveyor_gui/models/question_group_methods.rb +55 -0
  184. data/lib/surveyor_gui/models/question_methods.rb +435 -0
  185. data/lib/surveyor_gui/models/question_type_methods.rb +493 -0
  186. data/lib/surveyor_gui/models/response_methods.rb +67 -0
  187. data/lib/surveyor_gui/models/response_set_methods.rb +54 -0
  188. data/lib/surveyor_gui/models/row_methods.rb +11 -0
  189. data/lib/surveyor_gui/models/survey_methods.rb +32 -0
  190. data/lib/surveyor_gui/models/survey_section_methods.rb +32 -0
  191. data/lib/surveyor_gui/surveyforms_controller_methods.rb +258 -0
  192. data/lib/surveyor_gui/version.rb +3 -0
  193. data/lib/tasks/.gitkeep +0 -0
  194. data/lib/tasks/surveyor_gui_tasks.rake +4 -0
  195. data/lib/templates/erb/scaffold/_form.html.erb +13 -0
  196. data/spec/controllers/surveyforms_controller_spec.rb +361 -0
  197. data/spec/controllers/surveyor_controller_spec.rb +303 -0
  198. data/spec/factories.rb +181 -0
  199. data/spec/features/create_survey_spec.rb +418 -0
  200. data/spec/features/dependencies_spec.rb +61 -0
  201. data/spec/features/rearrange_survey_spec.rb +118 -0
  202. data/spec/features/ui_spec.rb +469 -0
  203. data/spec/fixtures/REDCapDemoDatabase_DataDictionary.csv +127 -0
  204. data/spec/fixtures/chinese_survey.rb +14 -0
  205. data/spec/fixtures/everything.rb +215 -0
  206. data/spec/fixtures/favorites-ish.rb +22 -0
  207. data/spec/fixtures/favorites.rb +22 -0
  208. data/spec/fixtures/feelings.rb +38 -0
  209. data/spec/fixtures/lifestyle.rb +55 -0
  210. data/spec/fixtures/numbers.rb +21 -0
  211. data/spec/fixtures/redcap_new_headers.csv +1 -0
  212. data/spec/fixtures/redcap_siblings.csv +1 -0
  213. data/spec/fixtures/redcap_whitespace.csv +1 -0
  214. data/spec/helpers/formtastic_custom_input_spec.rb +15 -0
  215. data/spec/helpers/surveyor_helper_spec.rb +116 -0
  216. data/spec/lib/common_spec.rb +37 -0
  217. data/spec/lib/parser_spec.rb +393 -0
  218. data/spec/lib/rake_kitchen_sink.rb +42 -0
  219. data/spec/lib/redcap_parser_spec.rb +129 -0
  220. data/spec/lib/unparser_spec.rb +126 -0
  221. data/spec/models/answer_spec.rb +144 -0
  222. data/spec/models/dependency_condition_spec.rb +428 -0
  223. data/spec/models/dependency_spec.rb +90 -0
  224. data/spec/models/question_group_spec.rb +66 -0
  225. data/spec/models/question_spec.rb +176 -0
  226. data/spec/models/response_set_spec.rb +452 -0
  227. data/spec/models/response_spec.rb +208 -0
  228. data/spec/models/survey_section_spec.rb +58 -0
  229. data/spec/models/survey_spec.rb +155 -0
  230. data/spec/models/surveyor_gui/question_spec.rb +60 -0
  231. data/spec/models/surveyor_gui/question_type_spec.rb +268 -0
  232. data/spec/models/validation_condition_spec.rb +98 -0
  233. data/spec/models/validation_spec.rb +64 -0
  234. data/spec/spec.opts +4 -0
  235. data/spec/spec_helper.rb +119 -0
  236. data/spec/support/scenario_outline_helper.rb +39 -0
  237. data/spec/support/shared.rb +10 -0
  238. data/spec/support/surveyforms_creation_helpers.rb +312 -0
  239. data/spec/support/surveyforms_rearrangement_helpers.rb +170 -0
  240. data/spec/support/surveyor_api_helpers.rb +15 -0
  241. data/spec/support/surveyor_ui_helpers.rb +108 -0
  242. data/spec/support/wait_for_ajax.rb +11 -0
  243. data/spec/views/questions/edit.html.erb_spec.rb +73 -0
  244. data/spec/views/surveyforms/edit.html.erb_spec.rb +126 -0
  245. data/surveyor_gui.gemspec +52 -0
  246. data/test/dummy/README.rdoc +261 -0
  247. data/test/dummy/Rakefile +7 -0
  248. data/test/dummy/app/assets/javascripts/application.js +15 -0
  249. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  250. data/test/dummy/app/controllers/application_controller.rb +3 -0
  251. data/test/dummy/app/helpers/application_helper.rb +2 -0
  252. data/test/dummy/app/mailers/.gitkeep +0 -0
  253. data/test/dummy/app/models/.gitkeep +0 -0
  254. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  255. data/test/dummy/config.ru +4 -0
  256. data/test/dummy/config/application.rb +59 -0
  257. data/test/dummy/config/boot.rb +10 -0
  258. data/test/dummy/config/database.yml +25 -0
  259. data/test/dummy/config/environment.rb +5 -0
  260. data/test/dummy/config/environments/development.rb +37 -0
  261. data/test/dummy/config/environments/production.rb +67 -0
  262. data/test/dummy/config/environments/test.rb +37 -0
  263. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  264. data/test/dummy/config/initializers/inflections.rb +15 -0
  265. data/test/dummy/config/initializers/mime_types.rb +5 -0
  266. data/test/dummy/config/initializers/secret_token.rb +7 -0
  267. data/test/dummy/config/initializers/session_store.rb +8 -0
  268. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  269. data/test/dummy/config/locales/en.yml +5 -0
  270. data/test/dummy/config/routes.rb +58 -0
  271. data/test/dummy/lib/assets/.gitkeep +0 -0
  272. data/test/dummy/log/.gitkeep +0 -0
  273. data/test/dummy/public/404.html +26 -0
  274. data/test/dummy/public/422.html +26 -0
  275. data/test/dummy/public/500.html +25 -0
  276. data/test/dummy/public/favicon.ico +0 -0
  277. data/test/dummy/script/rails +6 -0
  278. data/test/surveyor-gui_test.rb +7 -0
  279. data/test/test_helper.rb +15 -0
  280. data/vendor/assets/stylesheets/.gitkeep +0 -0
  281. data/vendor/assets/stylesheets/custom.sass +5 -0
  282. data/vendor/plugins/.gitkeep +0 -0
  283. metadata +664 -0
@@ -0,0 +1,3 @@
1
+ module SurveyorGui
2
+ VERSION = "0.0.3"
3
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :surveyor_gui do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,13 @@
1
+ <%%= simple_form_for(@<%= singular_name %>) do |f| %>
2
+ <%%= f.error_notification %>
3
+
4
+ <div class="inputs">
5
+ <%- attributes.each do |attribute| -%>
6
+ <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
7
+ <%- end -%>
8
+ </div>
9
+
10
+ <div class="actions">
11
+ <%%= f.button :submit %>
12
+ </div>
13
+ <%% end %>
@@ -0,0 +1,361 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SurveyformsController do
4
+ include Surveyor::Engine.routes.url_helpers
5
+ # before do
6
+ # @routes = Surveyor::Engine.routes
7
+ # end
8
+
9
+ let!(:survey) { FactoryGirl.create(:survey, :title => "Alphabet", :access_code => "alpha", :survey_version => 0)}
10
+ let!(:survey_beta) { FactoryGirl.create(:survey, :title => "Alphabet", :access_code => "alpha", :survey_version => 1)}
11
+ let!(:survey_with_no_responses) {FactoryGirl.create(:survey)}
12
+ let!(:survey_with_responses) {FactoryGirl.create(:survey)}
13
+ let!(:template) {FactoryGirl.create(:template)}
14
+ let!(:surveyform) {FactoryGirl.create(:surveyform)}
15
+ let!(:response_set) { FactoryGirl.create(:survey_sections, :survey => survey_with_responses)}
16
+ let!(:response_set) { FactoryGirl.create(:response_set, :survey => survey_with_responses, :access_code => "pdq")}
17
+
18
+ def survey_with_sections
19
+ {
20
+ :title=>'New Survey',
21
+ :survey_sections_attributes => {
22
+ "0" => {
23
+ :title => 'New Section',
24
+ :display_order => 0
25
+ }
26
+ }
27
+ }
28
+ end
29
+
30
+ context "#index" do
31
+ def do_get(params = {})
32
+ get :index, params
33
+ end
34
+
35
+ context "index parameters specify surveys" do
36
+
37
+ it "set the title to 'manage surveys'" do
38
+ do_get()
39
+ assigns(:title).should eq("Manage Surveys")
40
+ end
41
+
42
+ it "should not populate an array of templates" do
43
+ do_get()
44
+ expect(assigns(:surveyforms)).not_to eq([template])
45
+ end
46
+
47
+ it "should populate an array of surveys" do
48
+ do_get()
49
+ expect(assigns(:surveyforms)).to include(surveyform)
50
+ end
51
+
52
+ it "shows the surveys" do
53
+ do_get()
54
+ expect(response).to render_template('index')
55
+ end
56
+ end
57
+
58
+ context "index parameters specify survey templates" do
59
+
60
+ it "set the title to 'manage templates'" do
61
+ do_get(:template=>"true")
62
+ assigns(:title).should eq("Manage Templates")
63
+ end
64
+
65
+ it "should populate an array of templates" do
66
+ do_get(params={:template=>"true"})
67
+ expect(assigns(:surveyforms)).to eq([template])
68
+ end
69
+
70
+ it "should not populate an array of surveys" do
71
+ do_get(params={:template=>"true"})
72
+ expect(assigns(:surveyforms)).not_to eq([surveyform])
73
+ end
74
+
75
+ it "shows the survey templates" do
76
+ do_get(:template=>"true")
77
+ expect(response).to render_template('index')
78
+ end
79
+ end
80
+ end
81
+
82
+ context "#new" do
83
+ def do_get
84
+ get :new
85
+ end
86
+
87
+ it "renders new" do
88
+ do_get
89
+ expect(response).to be_success
90
+ expect(response).to render_template('new')
91
+ end
92
+
93
+ it "populates an empty survey" do
94
+ do_get
95
+ expect(assigns(:surveyform).id).to eq(nil)
96
+ end
97
+ end
98
+
99
+ context "#create" do
100
+
101
+ def do_post(params = {})
102
+ post :create, :surveyform=>params
103
+ end
104
+
105
+ context "it saves successfully" do
106
+
107
+ it "returns to the edit page" do
108
+ do_post(:title=>'New surv')
109
+ expect(response).to redirect_to(edit_surveyform_url(assigns(:surveyform).id))
110
+ end
111
+
112
+ it "resets question_no to 0" do
113
+ do_post(:title=>'New surv')
114
+ expect(assigns(:question_no)).to eq(0)
115
+ end
116
+
117
+ end
118
+
119
+ context "it fails to save" do
120
+
121
+ it "renders new" do
122
+ do_post()
123
+ expect(response).to render_template('new')
124
+ end
125
+ end
126
+
127
+ context "if it includes survey sections" do
128
+
129
+ before :each do
130
+ @survey_with_sections = survey_with_sections
131
+ end
132
+
133
+ context "when sections are valid" do
134
+ it "redirects to the edit page" do
135
+ do_post @survey_with_sections
136
+ expect(response).to redirect_to(edit_surveyform_url(assigns(:surveyform).id))
137
+ end
138
+ end
139
+
140
+ context "when sections are not valid" do
141
+ before :each do
142
+ @survey_with_sections[:survey_sections_attributes]["0"][:display_order]=nil
143
+ end
144
+ it "renders new" do
145
+ do_post()
146
+ expect(response).to render_template('new')
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ context "#edit" do
153
+
154
+ context "the survey has no responses" do
155
+
156
+ def do_get(params = {})
157
+ get :edit, {:id => survey_with_no_responses.id}.merge(params)
158
+ end
159
+
160
+ it "renders edit" do
161
+ do_get
162
+ expect(response).to be_success
163
+ expect(response).to render_template('edit')
164
+ end
165
+ end
166
+
167
+ context "the survey has responses" do
168
+
169
+ def do_get(params = {})
170
+ get :edit, {:id => survey_with_responses.id}.merge(params)
171
+ end
172
+
173
+ it "still lets you see the edit page" do
174
+ do_get
175
+ expect(response).to be_success
176
+ expect(response).to render_template('edit')
177
+ end
178
+ it "warns that responses have been collected" do
179
+ expect(flash[:error]) =~ /been collected/i
180
+ end
181
+ end
182
+ end
183
+
184
+ context "#update" do
185
+
186
+ context "it saves successfully" do
187
+
188
+ def do_put(params = {})
189
+ put :update, params
190
+ end
191
+
192
+ it "redirects to index" do
193
+ do_put(:id=>survey.id,:surveyform=>{:id=>survey.id})
194
+ expect(response).to redirect_to(surveyforms_url)
195
+ end
196
+
197
+ end
198
+
199
+ context "it fails to save" do
200
+
201
+ def do_put(params = {})
202
+ put :update, params
203
+ end
204
+
205
+ it "renders edit" do
206
+ do_put(:id=>survey.id, :surveyform=>{:id=>survey.id,:title=>''})
207
+ expect(response).to render_template('edit')
208
+ end
209
+
210
+ it "resets question_no to 0" do
211
+ do_put(:id=>survey.id,:surveyform=>{:id=>survey.id,:title=>''})
212
+ expect(assigns(:question_no)).to eq(0)
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+
219
+
220
+ context "#show" do
221
+ def do_get
222
+ get :show, {:id => survey.id}
223
+ end
224
+
225
+ it "shows survey" do
226
+ do_get
227
+ expect(response).to be_success
228
+ expect(response).to render_template('show')
229
+ end
230
+ end
231
+
232
+ context "#destroy" do
233
+
234
+ context "no responses were submitted" do
235
+ def do_delete
236
+ delete :destroy, :id => survey_with_no_responses
237
+ end
238
+
239
+ it "successfully destroys the survey" do
240
+ do_delete
241
+ expect(response).to redirect_to(surveyforms_url)
242
+ expect(Survey.exists?(survey_with_no_responses.id)).to be_false
243
+ end
244
+ end
245
+
246
+ context "responses were submitted" do
247
+ def do_delete
248
+ delete :destroy, :id => survey_with_responses
249
+ end
250
+
251
+ it "fails to delete the survey" do
252
+ do_delete
253
+ expect(response).to redirect_to(surveyforms_url)
254
+ expect(Survey.exists?(survey_with_responses.id)).to be_true
255
+ end
256
+
257
+ it "displays a flash message warning responses were collected" do
258
+ do_delete
259
+ expect(flash[:error]).to have_content /not be deleted/i
260
+ end
261
+ end
262
+ end
263
+
264
+ context "#replace form" do
265
+
266
+ def do_get(params = {})
267
+ FactoryGirl.create(:survey_section, :survey => survey)
268
+ get :replace_form, {:id=>survey.id,:survey_section_id=>survey.sections.first.id}.merge(params)
269
+ end
270
+
271
+ it "resets question_no to 0" do
272
+ do_get
273
+ expect(assigns(:question_no)).to eq(0)
274
+ end
275
+
276
+ it "renders new" do
277
+ do_get
278
+ expect(response).to be_success
279
+ expect(response).to render_template('new')
280
+ end
281
+
282
+ end
283
+
284
+ context "#insert_survey_section" do
285
+ def do_get(params = {})
286
+ survey.sections = [FactoryGirl.create(:survey_section, :survey => survey)]
287
+ get :insert_survey_section,{id: survey.id}.merge(params)
288
+ end
289
+ it "inserts a survey section" do
290
+ do_get
291
+ expect(response).to be_success
292
+ expect(response).to render_template('_survey_section_fields')
293
+ end
294
+ end
295
+
296
+ context "#replace_survey_section" do
297
+
298
+ def do_get(params = {})
299
+ FactoryGirl.create(:survey_section, :survey => survey)
300
+ get :replace_survey_section, {:id=>survey.id,:survey_section_id=>survey.sections.first.id}.merge(params)
301
+ end
302
+
303
+ it "resets question_no to 0" do
304
+ do_get
305
+ expect(assigns(:question_no)).to eq(0)
306
+ end
307
+
308
+ it "renders survey_section partial" do
309
+ do_get
310
+ expect(response).to be_success
311
+ expect(response).to render_template('_survey_section_fields')
312
+ end
313
+
314
+ end
315
+ context "#insert_new_question" do
316
+ def do_get(params = {})
317
+ survey.sections = [FactoryGirl.create(:survey_section, :survey => survey)]
318
+ survey.sections.first.questions = [FactoryGirl.create(:question, :survey_section => survey.sections.first)]
319
+ get :insert_new_question,{:id => survey.id, :question_id => survey.sections.first.questions.first.id}.merge(params)
320
+ end
321
+ it "inserts a question" do
322
+ do_get
323
+ expect(response).to be_success
324
+ expect(response).to render_template('new')
325
+ end
326
+ end
327
+
328
+ context "#cut_question" do
329
+ def do_get(params = {})
330
+ survey.sections = [FactoryGirl.create(:survey_section, :survey => survey)]
331
+ survey.sections.first.questions = [FactoryGirl.create(:question, :survey_section => survey.sections.first)]
332
+ get :cut_question,{:id => survey.id, :question_id => survey.sections.first.questions.first.id}.merge(params)
333
+ end
334
+ it "cuts a question" do
335
+ do_get
336
+ expect(response).to be_success
337
+ end
338
+ end
339
+
340
+ context "#clone_survey" do
341
+ def do_put(params={})
342
+ survey.sections = [FactoryGirl.create(:survey_section, :survey => survey)]
343
+ survey.sections.first.questions = [FactoryGirl.create(:question, :survey_section => survey.sections.first, text: 'my cloned question')]
344
+ put :clone_survey,{id: survey.id}
345
+ end
346
+
347
+ it "creates a new survey" do
348
+ expect{do_put}.to change{Survey.count}.by(1)
349
+ end
350
+
351
+ it "gives a different id to the clone" do
352
+ do_put
353
+ expect(Survey.last.id).not_to eql survey.id
354
+ end
355
+
356
+ it "copies the text of the question" do
357
+ do_put
358
+ expect(Survey.last.survey_sections.first.questions.first.text).to eql 'my cloned question'
359
+ end
360
+ end
361
+ end
@@ -0,0 +1,303 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SurveyorController do
4
+ include Surveyor::Engine.routes.url_helpers
5
+ before do
6
+ @routes = Surveyor::Engine.routes
7
+ end
8
+
9
+ let!(:survey) { FactoryGirl.create(:survey, :title => "Alphabet", :access_code => "alpha", :survey_version => 0)}
10
+ let!(:survey_beta) { FactoryGirl.create(:survey, :title => "Alphabet", :access_code => "alpha", :survey_version => 1)}
11
+ let!(:response_set) { FactoryGirl.create(:response_set, :survey => survey, :access_code => "pdq")}
12
+
13
+ before { ResponseSet.stub(:create).and_return(response_set) }
14
+
15
+ # match '/', :to => 'surveyor#new', :as => 'available_surveys', :via => :get
16
+ # match '/:survey_code', :to => 'surveyor#create', :as => 'take_survey', :via => :post
17
+ # match '/:survey_code', :to => 'surveyor#export', :as => 'export_survey', :via => :get
18
+ # match '/:survey_code/:response_set_code', :to => 'surveyor#show', :as => 'view_my_survey', :via => :get
19
+ # match '/:survey_code/:response_set_code/take', :to => 'surveyor#edit', :as => 'edit_my_survey', :via => :get
20
+ # match '/:survey_code/:response_set_code', :to => 'surveyor#update', :as => 'update_my_survey', :via => :put
21
+
22
+ context "#new" do
23
+ def do_get
24
+ get :new
25
+ end
26
+ it "renders new" do
27
+ do_get
28
+ response.should be_success
29
+ response.should render_template('new')
30
+ end
31
+ it "assigns surveys_by_access_code" do
32
+ do_get
33
+ assigns(:surveys_by_access_code).should == {"alpha" => [survey_beta,survey]}
34
+ end
35
+ end
36
+
37
+ context "#create" do
38
+ def do_post(params = {})
39
+ post :create, {:survey_code => "alpha"}.merge(params)
40
+ end
41
+ it "finds latest version" do
42
+ do_post
43
+ assigns(:survey).should == survey_beta
44
+ end
45
+ it "finds specified survey_version" do
46
+ do_post :survey_version => 0
47
+ assigns(:survey).should == survey
48
+ end
49
+ it "creates a new response_set" do
50
+ ResponseSet.should_receive(:create)
51
+ do_post
52
+ end
53
+ it "should redirects to the new response_set" do
54
+ do_post
55
+ response.should redirect_to( edit_my_survey_path(:survey_code => "alpha", :response_set_code => "pdq"))
56
+ end
57
+
58
+ context "with failures" do
59
+ it "redirect to #new on failed ResponseSet#create" do
60
+ ResponseSet.should_receive(:create).and_return(false)
61
+ do_post
62
+ response.should redirect_to(available_surveys_path)
63
+ end
64
+ it "redirect to #new on failed Survey#find" do
65
+ do_post :survey_code => "missing"
66
+ response.should redirect_to(available_surveys_path)
67
+ end
68
+ end
69
+
70
+ context "with javascript check, assigned in session" do
71
+ it "enabled" do
72
+ do_post :surveyor_javascript_enabled => "true"
73
+ session[:surveyor_javascript].should_not be_nil
74
+ session[:surveyor_javascript].should == "enabled"
75
+ end
76
+ it "disabled" do
77
+ post :create, :survey_code => "xyz", :surveyor_javascript_enabled => "not_true"
78
+ session[:surveyor_javascript].should_not be_nil
79
+ session[:surveyor_javascript].should == "not_enabled"
80
+ end
81
+ end
82
+ end
83
+
84
+ context "#show" do
85
+ def do_get(params = {})
86
+ get :show, {:survey_code => "alpha", :response_set_code => "pdq"}.merge(params)
87
+ end
88
+ it "renders show" do
89
+ do_get
90
+ response.should be_success
91
+ response.should render_template('show')
92
+ end
93
+ it "redirects for missing response set" do
94
+ do_get :response_set_code => "DIFFERENT"
95
+ response.should redirect_to(available_surveys_path)
96
+ end
97
+ it "assigns earlier survey_version" do
98
+ response_set
99
+ do_get
100
+ assigns[:response_set].should == response_set
101
+ assigns[:survey].should == survey
102
+ end
103
+ it "assigns later survey_version" do
104
+ response_set_beta = FactoryGirl.create(:response_set, :survey => survey_beta, :access_code => "rst")
105
+ do_get :response_set_code => "rst"
106
+ assigns[:response_set].should == response_set_beta
107
+ assigns[:survey].should == survey_beta
108
+ end
109
+ end
110
+
111
+ context "#edit" do
112
+ context "responses have been collected" do
113
+ def do_get(params = {})
114
+ @survey = FactoryGirl.create(:survey, :title => "Alphabet Soup", :access_code => "alphasoup", :survey_version => 0)
115
+ @survey.sections = [FactoryGirl.create(:survey_section, :survey => @survey)]
116
+ @response_set = FactoryGirl.create(:response_set, :survey => @survey, :access_code => "foo")
117
+ get :edit, {:survey_code => "alphasoup", :response_set_code => "foo"}.merge(params)
118
+ end
119
+ it "renders edit" do
120
+ do_get
121
+ response.should be_success
122
+ response.should render_template('edit')
123
+ end
124
+ it "assigns survey and response set" do
125
+ do_get
126
+ assigns[:survey].should == @survey
127
+ assigns[:response_set].should == @response_set
128
+ end
129
+ it "redirects for missing response set" do
130
+ do_get :response_set_code => "DIFFERENT"
131
+ response.should redirect_to(available_surveys_path)
132
+ end
133
+ it "assigns dependents if javascript not enabled" do
134
+ controller.stub(:get_unanswered_dependencies_minus_section_questions).and_return([FactoryGirl.create(:question)])
135
+ session[:surveyor_javascript].should be_nil
136
+ do_get
137
+ assigns[:dependents].should_not be_empty
138
+ end
139
+ it "does not assign dependents if javascript is enabled" do
140
+ controller.stub(:get_unanswered_dependencies_minus_section_questions).and_return([FactoryGirl.create(:question)])
141
+ session[:surveyor_javascript] = "enabled"
142
+ do_get
143
+ assigns[:dependents].should be_empty
144
+ end
145
+ it "assigns earlier survey_version" do
146
+ do_get
147
+ assigns[:response_set].should == @response_set
148
+ assigns[:survey].should == @survey
149
+ end
150
+ it "assigns later survey_version" do
151
+ survey_beta.sections = [FactoryGirl.create(:survey_section, :survey => survey_beta)]
152
+ response_set_beta = FactoryGirl.create(:response_set, :survey => survey_beta, :access_code => "rst")
153
+ do_get :response_set_code => "rst"
154
+ assigns[:survey].should == survey_beta
155
+ assigns[:response_set].should == response_set_beta
156
+ end
157
+ end
158
+ end
159
+
160
+ context "#update" do
161
+ let(:responses_ui_hash) { {} }
162
+ let(:update_params) {
163
+ {
164
+ :survey_code => "alpha",
165
+ :response_set_code => "pdq"
166
+ }
167
+ }
168
+ shared_examples "#update action" do
169
+ before do
170
+ ResponseSet.stub_chain(:includes, :where, :first).and_return(response_set)
171
+ responses_ui_hash['11'] = {'api_id' => 'something', 'answer_id' => '56', 'question_id' => '9'}
172
+ end
173
+ it "saves responses" do
174
+ response_set.should_receive(:update_from_ui_hash).with(responses_ui_hash)
175
+
176
+ do_put(:r => responses_ui_hash)
177
+ end
178
+ it "does not fail when there are no responses" do
179
+ lambda { do_put }.should_not raise_error
180
+ end
181
+ context "with update exceptions" do
182
+ it 'retries the update on a constraint violation' do
183
+ response_set.should_receive(:update_from_ui_hash).ordered.with(responses_ui_hash).and_raise(ActiveRecord::StatementInvalid.new(''))
184
+ response_set.should_receive(:update_from_ui_hash).ordered.with(responses_ui_hash)
185
+
186
+ expect { do_put(:r => responses_ui_hash) }.to_not raise_error
187
+ end
188
+
189
+ it 'only retries three times' do
190
+ response_set.should_receive(:update_from_ui_hash).exactly(3).times.with(responses_ui_hash).and_raise(ActiveRecord::StatementInvalid.new(''))
191
+
192
+ expect { do_put(:r => responses_ui_hash) }.to raise_error(ActiveRecord::StatementInvalid)
193
+ end
194
+
195
+ it 'does not retry for other errors' do
196
+ response_set.should_receive(:update_from_ui_hash).once.with(responses_ui_hash).and_raise('Bad news')
197
+
198
+ expect { do_put(:r => responses_ui_hash) }.to raise_error('Bad news')
199
+ end
200
+ end
201
+ end
202
+
203
+ context "with form submission" do
204
+ def do_put(extra_params = {})
205
+ put :update, update_params.merge(extra_params)
206
+ end
207
+
208
+ it_behaves_like "#update action"
209
+ it "redirects to #edit without params" do
210
+ do_put
211
+ response.should redirect_to(edit_my_survey_path(:survey_code => "alpha", :response_set_code => "pdq"))
212
+ end
213
+ it "completes the found response set on finish" do
214
+ do_put :finish => 'finish'
215
+ response_set.reload.should be_complete
216
+ end
217
+ it 'flashes completion' do
218
+ do_put :finish => 'finish'
219
+ flash[:notice].should == "Completed survey"
220
+ end
221
+ it "redirects for missing response set" do
222
+ do_put :response_set_code => "DIFFERENT"
223
+ response.should redirect_to(available_surveys_path)
224
+ flash[:notice].should == "Unable to find your responses to the survey"
225
+ end
226
+ end
227
+
228
+ context 'with ajax' do
229
+ def do_put(extra_params = {})
230
+ xhr :put, :update, update_params.merge(extra_params)
231
+ end
232
+
233
+ it_behaves_like "#update action"
234
+ it "returns dependencies" do
235
+ ResponseSet.stub_chain(:includes, :where, :first).and_return(response_set)
236
+ response_set.should_receive(:all_dependencies).and_return({"show" => ['q_1'], "hide" => ['q_2']})
237
+
238
+ do_put
239
+ JSON.parse(response.body).should == {"show" => ['q_1'], "hide" => ["q_2"]}
240
+ end
241
+ it "returns 404 for missing response set" do
242
+ do_put :response_set_code => "DIFFERENT"
243
+ response.status.should == 404
244
+ end
245
+ end
246
+ end
247
+
248
+ context "#export" do
249
+ render_views
250
+
251
+ let(:json) {
252
+ get :export, :survey_code => survey.access_code, :format => 'json'
253
+ JSON.parse(response.body)
254
+ }
255
+
256
+ context "question inside and outside a question group" do
257
+ def question_text(refid)
258
+ <<-SURVEY
259
+ q "Where is a foo?", :pick => :one, :help_text => 'Look around.', :reference_identifier => #{refid.inspect},
260
+ :data_export_identifier => 'X.FOO', :common_namespace => 'F', :common_identifier => 'f'
261
+ a_L 'To the left', :data_export_identifier => 'X.L', :common_namespace => 'F', :common_identifier => 'l'
262
+ a_R 'To the right', :data_export_identifier => 'X.R', :common_namespace => 'F', :common_identifier => 'r'
263
+ a_O 'Elsewhere', :string
264
+
265
+ dependency :rule => 'R'
266
+ condition_R :q_bar, "==", :a_1
267
+ SURVEY
268
+ end
269
+ let(:survey_text) {
270
+ <<-SURVEY
271
+ survey 'xyz' do
272
+ section 'Sole' do
273
+ q_bar "Should that other question show up?", :pick => :one
274
+ a_1 'Yes'
275
+ a_2 'No'
276
+
277
+ #{question_text('foo_solo')}
278
+
279
+ group do
280
+ #{question_text('foo_grouped')}
281
+ end
282
+ end
283
+ end
284
+ SURVEY
285
+ }
286
+ let(:survey) { Surveyor::Parser.new.parse(survey_text) }
287
+ let(:solo_question_json) { json['sections'][0]['questions_and_groups'][1] }
288
+ let(:grouped_question_json) { json['sections'][0]['questions_and_groups'][2]['questions'][0] }
289
+
290
+ it "produces identical JSON except for API IDs and question reference identifers" do
291
+ solo_question_json['answers'].to_json.should be_json_eql( grouped_question_json['answers'].to_json).excluding("uuid", "reference_identifier")
292
+ solo_question_json['dependency'].to_json.should be_json_eql( grouped_question_json['dependency'].to_json).excluding("uuid", "reference_identifier")
293
+ solo_question_json.to_json.should be_json_eql( grouped_question_json.to_json).excluding("uuid", "reference_identifier")
294
+ end
295
+ it "produces the expected reference identifier for the solo question" do
296
+ solo_question_json['reference_identifier'].should == 'foo_solo'
297
+ end
298
+ it "produces the expected reference identifer for the question in the group" do
299
+ grouped_question_json['reference_identifier'].should == 'foo_grouped'
300
+ end
301
+ end
302
+ end
303
+ end