surveyor 0.18.2 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/.gitignore +2 -1
  2. data/CHANGELOG +12 -0
  3. data/Rakefile +1 -0
  4. data/VERSION +1 -1
  5. data/app/helpers/surveyor_helper.rb +40 -22
  6. data/app/views/layouts/surveyor_default.html.erb +0 -1
  7. data/app/views/partials/_answer.html.haml +19 -58
  8. data/app/views/partials/_dependents.html.haml +5 -0
  9. data/app/views/partials/_question.html.haml +24 -72
  10. data/app/views/partials/_question_group.html.haml +30 -39
  11. data/app/views/partials/_section.html.haml +10 -11
  12. data/app/views/partials/_section_menu.html.haml +5 -0
  13. data/app/views/surveyor/edit.html.haml +13 -25
  14. data/app/views/surveyor/new.html.haml +6 -3
  15. data/features/{redcap.feature → redcap_parser.feature} +0 -0
  16. data/features/step_definitions/parser_steps.rb +71 -0
  17. data/features/step_definitions/surveyor_steps.rb +8 -78
  18. data/features/support/paths.rb +2 -0
  19. data/features/surveyor.feature +30 -168
  20. data/features/surveyor_parser.feature +157 -0
  21. data/generators/surveyor/surveyor_generator.rb +2 -1
  22. data/generators/surveyor/templates/assets/images/next.gif +0 -0
  23. data/generators/surveyor/templates/assets/images/prev.gif +0 -0
  24. data/generators/surveyor/templates/assets/javascripts/jquery.surveyor.js +46 -0
  25. data/generators/surveyor/templates/assets/javascripts/jquery.tools.min.js +204 -0
  26. data/generators/surveyor/templates/assets/stylesheets/dateinput.css +149 -0
  27. data/generators/surveyor/templates/assets/stylesheets/sass/surveyor.sass +84 -226
  28. data/generators/surveyor/templates/migrate/add_default_value_to_answers.rb +9 -0
  29. data/lib/formtastic/surveyor_builder.rb +83 -0
  30. data/lib/surveyor.rb +5 -0
  31. data/lib/surveyor/models/answer_methods.rb +8 -3
  32. data/lib/surveyor/models/question_group_methods.rb +1 -1
  33. data/lib/surveyor/models/question_methods.rb +6 -2
  34. data/lib/surveyor/models/response_methods.rb +2 -12
  35. data/lib/surveyor/models/response_set_methods.rb +25 -71
  36. data/lib/surveyor/surveyor_controller_methods.rb +29 -28
  37. data/lib/tasks/surveyor_tasks.rake +1 -15
  38. data/spec/controllers/surveyor_controller_spec.rb +40 -8
  39. data/spec/helpers/surveyor_helper_spec.rb +53 -0
  40. data/spec/models/answer_spec.rb +14 -9
  41. data/spec/models/dependency_condition_spec.rb +56 -61
  42. data/spec/models/question_group_spec.rb +3 -3
  43. data/spec/models/question_spec.rb +10 -0
  44. data/spec/models/response_set_spec.rb +72 -62
  45. data/surveyor.gemspec +21 -58
  46. data/testbed/Gemfile +1 -0
  47. metadata +34 -60
  48. data/app/helpers/survey_form_builder.rb +0 -37
  49. data/generators/surveyor/templates/assets/images/222222_11x11_icon_arrows_leftright.gif +0 -0
  50. data/generators/surveyor/templates/assets/images/222222_11x11_icon_arrows_updown.gif +0 -0
  51. data/generators/surveyor/templates/assets/images/222222_11x11_icon_close.gif +0 -0
  52. data/generators/surveyor/templates/assets/images/222222_11x11_icon_doc.gif +0 -0
  53. data/generators/surveyor/templates/assets/images/222222_11x11_icon_folder_closed.gif +0 -0
  54. data/generators/surveyor/templates/assets/images/222222_11x11_icon_folder_open.gif +0 -0
  55. data/generators/surveyor/templates/assets/images/222222_11x11_icon_minus.gif +0 -0
  56. data/generators/surveyor/templates/assets/images/222222_11x11_icon_plus.gif +0 -0
  57. data/generators/surveyor/templates/assets/images/222222_11x11_icon_resize_se.gif +0 -0
  58. data/generators/surveyor/templates/assets/images/222222_35x9_colorpicker_indicator.gif.gif +0 -0
  59. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_down.gif +0 -0
  60. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_left.gif +0 -0
  61. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_right.gif +0 -0
  62. data/generators/surveyor/templates/assets/images/222222_7x7_arrow_up.gif +0 -0
  63. data/generators/surveyor/templates/assets/images/454545_11x11_icon_arrows_leftright.gif +0 -0
  64. data/generators/surveyor/templates/assets/images/454545_11x11_icon_arrows_updown.gif +0 -0
  65. data/generators/surveyor/templates/assets/images/454545_11x11_icon_close.gif +0 -0
  66. data/generators/surveyor/templates/assets/images/454545_11x11_icon_doc.gif +0 -0
  67. data/generators/surveyor/templates/assets/images/454545_11x11_icon_folder_closed.gif +0 -0
  68. data/generators/surveyor/templates/assets/images/454545_11x11_icon_folder_open.gif +0 -0
  69. data/generators/surveyor/templates/assets/images/454545_11x11_icon_minus.gif +0 -0
  70. data/generators/surveyor/templates/assets/images/454545_11x11_icon_plus.gif +0 -0
  71. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_down.gif +0 -0
  72. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_left.gif +0 -0
  73. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_right.gif +0 -0
  74. data/generators/surveyor/templates/assets/images/454545_7x7_arrow_up.gif +0 -0
  75. data/generators/surveyor/templates/assets/images/888888_11x11_icon_arrows_leftright.gif +0 -0
  76. data/generators/surveyor/templates/assets/images/888888_11x11_icon_arrows_updown.gif +0 -0
  77. data/generators/surveyor/templates/assets/images/888888_11x11_icon_close.gif +0 -0
  78. data/generators/surveyor/templates/assets/images/888888_11x11_icon_doc.gif +0 -0
  79. data/generators/surveyor/templates/assets/images/888888_11x11_icon_folder_closed.gif +0 -0
  80. data/generators/surveyor/templates/assets/images/888888_11x11_icon_folder_open.gif +0 -0
  81. data/generators/surveyor/templates/assets/images/888888_11x11_icon_minus.gif +0 -0
  82. data/generators/surveyor/templates/assets/images/888888_11x11_icon_plus.gif +0 -0
  83. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_down.gif +0 -0
  84. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_left.gif +0 -0
  85. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_right.gif +0 -0
  86. data/generators/surveyor/templates/assets/images/888888_7x7_arrow_up.gif +0 -0
  87. data/generators/surveyor/templates/assets/images/dadada_40x100_textures_02_glass_75.png +0 -0
  88. data/generators/surveyor/templates/assets/images/e6e6e6_40x100_textures_02_glass_75.png +0 -0
  89. data/generators/surveyor/templates/assets/images/ffffff_40x100_textures_01_flat_0.png +0 -0
  90. data/generators/surveyor/templates/assets/images/ffffff_40x100_textures_02_glass_65.png +0 -0
  91. data/generators/surveyor/templates/assets/javascripts/accessibleUISlider.jQuery.js +0 -201
  92. data/generators/surveyor/templates/assets/javascripts/jquery-1.2.6.js +0 -3549
  93. data/generators/surveyor/templates/assets/javascripts/jquery-ui-personalized-1.5.3.js +0 -7616
  94. data/generators/surveyor/templates/assets/javascripts/jquery.form.js +0 -637
  95. data/generators/surveyor/templates/assets/javascripts/surveyor.js +0 -40
  96. data/generators/surveyor/templates/assets/stylesheets/jquery-ui-slider-additions.css +0 -71
  97. data/generators/surveyor/templates/assets/stylesheets/ui.theme.css +0 -851
  98. data/spec/views/surveyor/show.html.haml_spec.rb +0 -32
  99. data/uninstall.rb +0 -1
@@ -0,0 +1,9 @@
1
+ class AddDefaultValueToAnswers < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :answers, :default_value, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :answers, :default_value
8
+ end
9
+ end
@@ -0,0 +1,83 @@
1
+ module Formtastic
2
+ class SurveyorBuilder < SemanticFormBuilder
3
+ def quiet_input(method, options = {})
4
+ html_options = options.delete(:input_html) || strip_formtastic_options(options)
5
+ html_options[:id] ||= generate_html_id(method, "")
6
+ hidden_field(method, html_options)
7
+ end
8
+ def surveyor_check_boxes_input(method, options)
9
+ collection = find_collection_for_column(method, options)
10
+ html_options = options.delete(:input_html) || {}
11
+
12
+ input_name = generate_association_input_name(method)
13
+ hidden_fields = options.delete(:hidden_fields)
14
+ value_as_class = options.delete(:value_as_class)
15
+ unchecked_value = options.delete(:unchecked_value) || ''
16
+ html_options = { :name => "#{@object_name}[#{input_name}][]" }.merge(html_options)
17
+ input_ids = []
18
+
19
+ selected_values = find_selected_values_for_column(method, options)
20
+ disabled_option_is_present = options.key?(:disabled)
21
+ disabled_values = [*options[:disabled]] if disabled_option_is_present
22
+
23
+ li_options = value_as_class ? { :class => [method.to_s.singularize, 'default'].join('_') } : {}
24
+
25
+ list_item_content = collection.map do |c|
26
+ label = c.is_a?(Array) ? c.first : c
27
+ value = c.is_a?(Array) ? c.last : c
28
+ input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
29
+ input_ids << input_id
30
+
31
+ html_options[:checked] = selected_values.include?(value)
32
+ html_options[:disabled] = disabled_values.include?(value) if disabled_option_is_present
33
+ html_options[:id] = input_id
34
+
35
+ li_content = create_hidden_field_for_check_boxes(input_name, value_as_class) unless hidden_fields
36
+ li_content << template.content_tag(:label,
37
+ Formtastic::Util.html_safe("#{create_check_boxes(input_name, html_options, value, unchecked_value, hidden_fields)} #{escape_html_entities(label)}"),
38
+ :for => input_id
39
+ )
40
+ li_content << basic_input_helper(:text_field, :string, :string_value, options) if options[:response_class] == "other_and_string"
41
+ li_content << basic_input_helper(:text_field, :string, :string_value, options) if %w(string other_and_string).include?(options[:response_class])
42
+
43
+ # li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
44
+ Formtastic::Util.html_safe(li_content)
45
+ end
46
+
47
+ Formtastic::Util.html_safe(list_item_content.join)
48
+ end
49
+ def surveyor_radio_input(method, options)
50
+ collection = find_collection_for_column(method, options)
51
+ html_options = strip_formtastic_options(options).merge(options.delete(:input_html) || {})
52
+
53
+ input_name = generate_association_input_name(method)
54
+ value_as_class = options.delete(:value_as_class)
55
+ input_ids = []
56
+
57
+ list_item_content = collection.map do |c|
58
+ label = c.is_a?(Array) ? c.first : c
59
+ value = c.is_a?(Array) ? c.last : c
60
+ input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
61
+ input_ids << input_id
62
+
63
+ html_options[:id] = input_id
64
+
65
+ li_content = template.content_tag(:label,
66
+ Formtastic::Util.html_safe("#{radio_button(input_name, value, html_options)} #{escape_html_entities(label)}"),
67
+ :for => input_id
68
+ )
69
+
70
+ li_content << basic_input_helper(:text_field, :string, :integer_value, options) if options[:response_class] == 'integer'
71
+ li_content << basic_input_helper(:text_field, :string, :string_value, options) if options[:response_class] == 'string'
72
+
73
+ # li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
74
+ Formtastic::Util.html_safe(li_content)
75
+ end
76
+
77
+ Formtastic::Util.html_safe(list_item_content.join)
78
+ end
79
+ def date_input(method, options)
80
+ string_input(method, options)
81
+ end
82
+ end
83
+ end
data/lib/surveyor.rb CHANGED
@@ -1,2 +1,7 @@
1
1
  require 'surveyor/common'
2
2
  require 'surveyor/acts_as_response'
3
+ require 'formtastic/surveyor_builder'
4
+ Formtastic::SemanticFormHelper.builder = Formtastic::SurveyorBuilder
5
+ Formtastic::SemanticFormBuilder.default_text_area_height = 5
6
+ Formtastic::SemanticFormBuilder.default_text_area_width = 50
7
+ Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
@@ -35,10 +35,15 @@ module Surveyor
35
35
  self.data_export_identifier ||= Surveyor::Common.normalize(text)
36
36
  end
37
37
 
38
- def renderer(q = question)
39
- r = [q.pick.to_s, self.response_class].compact.map(&:downcase).join("_")
40
- r.blank? ? :default : r.to_sym
38
+ def css_class
39
+ [(is_exclusive ? "exclusive" : nil), custom_class].compact.join(" ")
41
40
  end
41
+
42
+ def split_or_hidden_text(part = nil)
43
+ return "" if hide_label.to_s == "true"
44
+ part == :pre ? text.split("|",2)[0] : (part == :post ? text.split("|",2)[1] : text)
45
+ end
46
+
42
47
  end
43
48
  end
44
49
  end
@@ -32,7 +32,7 @@ module Surveyor
32
32
  dependent? ? self.dependency.is_met?(response_set) : true
33
33
  end
34
34
  def css_class(response_set)
35
- [(dependent? ? "dependent" : nil), (triggered?(response_set) ? nil : "hidden"), custom_class].compact.join(" ")
35
+ [(dependent? ? "g_dependent" : nil), (triggered?(response_set) ? nil : "g_hidden"), custom_class].compact.join(" ")
36
36
  end
37
37
  end
38
38
  end
@@ -56,7 +56,7 @@ module Surveyor
56
56
  dependent? ? self.dependency.is_met?(response_set) : true
57
57
  end
58
58
  def css_class(response_set)
59
- [(dependent? ? "dependent" : nil), (triggered?(response_set) ? nil : "hidden"), custom_class].compact.join(" ")
59
+ [(dependent? ? "q_dependent" : nil), (triggered?(response_set) ? nil : "q_hidden"), custom_class].compact.join(" ")
60
60
  end
61
61
 
62
62
  def part_of_group?
@@ -65,7 +65,11 @@ module Surveyor
65
65
  def solo?
66
66
  self.question_group.nil?
67
67
  end
68
-
68
+
69
+ def split_text(part = nil)
70
+ (part == :pre ? text.split("|",2)[0] : (part == :post ? text.split("|",2)[1] : text)).to_s
71
+ end
72
+
69
73
  def renderer(g = question_group)
70
74
  r = [g ? g.renderer.to_s : nil, display_type].compact.join("_")
71
75
  r.blank? ? :default : r.to_sym
@@ -6,7 +6,6 @@ module Surveyor
6
6
  base.send :belongs_to, :response_set
7
7
  base.send :belongs_to, :question
8
8
  base.send :belongs_to, :answer
9
-
10
9
  @@validations_already_included ||= nil
11
10
  unless @@validations_already_included
12
11
  # Validations
@@ -14,22 +13,13 @@ module Surveyor
14
13
 
15
14
  @@validations_already_included = true
16
15
  end
17
-
18
16
  base.send :include, Surveyor::ActsAsResponse # includes "as" instance method
19
-
20
17
  end
21
18
 
22
19
  # Instance Methods
23
- def selected
24
- !self.new_record?
20
+ def answer_id=(val)
21
+ write_attribute :answer_id, (val.is_a?(Array) ? val.detect{|x| !x.to_s.blank?} : val)
25
22
  end
26
-
27
- alias_method :selected?, :selected
28
-
29
- def selected=(value)
30
- true
31
- end
32
-
33
23
  def correct?
34
24
  question.correct_answer_id.nil? or self.answer.response_class != "answer" or (question.correct_answer_id.to_i == answer_id.to_i)
35
25
  end
@@ -6,6 +6,7 @@ module Surveyor
6
6
  base.send :belongs_to, :survey
7
7
  base.send :belongs_to, :user
8
8
  base.send :has_many, :responses, :dependent => :destroy
9
+ base.send :accepts_nested_attributes_for, :responses, :allow_destroy => true
9
10
 
10
11
  @@validations_already_included ||= nil
11
12
  unless @@validations_already_included
@@ -16,13 +17,27 @@ module Surveyor
16
17
 
17
18
  @@validations_already_included = true
18
19
  end
19
-
20
+
20
21
  # Attributes
21
22
  base.send :attr_protected, :completed_at
22
- base.send :attr_accessor, :current_section_id
23
-
24
- # Callbacks
25
- base.send :after_update, :save_responses
23
+
24
+ # Class methods
25
+ base.instance_eval do
26
+ def reject_or_destroy_blanks(hash_of_hashes)
27
+ result = {}
28
+ (hash_of_hashes || {}).each_pair do |k, hash|
29
+ if has_blank_value?(hash)
30
+ result.merge!({k => hash.merge("_destroy" => "true")}) if hash.has_key?("id")
31
+ else
32
+ result.merge!({k => hash})
33
+ end
34
+ end
35
+ result
36
+ end
37
+ def has_blank_value?(hash)
38
+ hash["answer_id"].blank? or hash.any?{|k,v| v.is_a?(Array) ? v.all?{|x| x.to_s.blank?} : v.to_s.blank?}
39
+ end
40
+ end
26
41
  end
27
42
 
28
43
  # Instance methods
@@ -56,65 +71,6 @@ module Surveyor
56
71
  end
57
72
  result
58
73
  end
59
-
60
- def response_for(question_id, answer_id, group = nil)
61
- found = responses.detect{|r| r.question_id == question_id && r.answer_id == answer_id && r.response_group.to_s == group.to_s}
62
- found.blank? ? responses.new(:question_id => question_id, :answer_id => answer_id, :response_group => group) : found
63
- end
64
-
65
- def clear_responses
66
- question_ids = Question.find_all_by_survey_section_id(current_section_id).map(&:id)
67
- responses.select{|r| question_ids.include? r.question_id}.map(&:destroy)
68
- responses.reload
69
- end
70
-
71
- def response_attributes=(response_attributes)
72
- response_attributes.each do |question_id, responses_hash|
73
- # Response.delete_all(["response_set_id =? AND question_id =?", self.id, question_id])
74
- if (answer_id = responses_hash[:answer_id])
75
- if (!responses_hash[:answer_id].empty?) # Dropdowns return answer id but have an empty value if they are not set... ignoring those.
76
- #radio or dropdown - only one response
77
- responses.build({:question_id => question_id, :answer_id => answer_id, :survey_section_id => current_section_id}.merge(responses_hash[answer_id] || {}))
78
- end
79
- else
80
- #possibly multiples responses - unresponded radios end up here too
81
- # we use the variable question_id, not the "question_id" in the response_hash
82
- responses_hash.delete_if{|k,v| k == "question_id"}.each do |answer_id, response_hash|
83
- unless response_hash.delete_if{|k,v| v.blank?}.empty?
84
- responses.build({:question_id => question_id, :answer_id => answer_id, :survey_section_id => current_section_id}.merge(response_hash))
85
- end
86
- end
87
- end
88
- end
89
- end
90
-
91
- def response_group_attributes=(response_attributes)
92
- response_attributes.each do |question_id, responses_group_hash|
93
- # Response.delete_all(["response_set_id =? AND question_id =?", self.id, question_id])
94
- responses_group_hash.each do |response_group_number, group_hash|
95
- if (answer_id = group_hash[:answer_id]) # if group_hash has an answer_id key we treat it differently
96
- if (!group_hash[:answer_id].empty?) # dropdowns return empty values in answer_ids if they are not selected
97
- #radio or dropdown - only one response
98
- responses.build({:question_id => question_id, :answer_id => answer_id, :response_group => response_group_number, :survey_section_id => current_section_id}.merge(group_hash[answer_id] || {}))
99
- end
100
- else
101
- #possibly multiples responses - unresponded radios end up here too
102
- # we use the variable question_id in the key, not the "question_id" in the response_hash... same with response_group key
103
- group_hash.delete_if{|k,v| (k == "question_id") or (k == "response_group")}.each do |answer_id, inner_hash|
104
- unless inner_hash.delete_if{|k,v| v.blank?}.empty?
105
- responses.build({:question_id => question_id, :answer_id => answer_id, :response_group => response_group_number, :survey_section_id => current_section_id}.merge(inner_hash))
106
- end
107
- end
108
- end
109
-
110
- end
111
- end
112
- end
113
-
114
- def save_responses
115
- responses.each{|response| response.save(false)}
116
- end
117
-
118
74
  def complete!
119
75
  self.completed_at = Time.now
120
76
  end
@@ -157,22 +113,20 @@ module Surveyor
157
113
  dependencies.select{|d| d.is_met?(self) and self.is_unanswered?(d.question)}.map(&:question)
158
114
  end
159
115
 
160
- def all_dependencies
161
- arr = dependencies.partition{|d| d.is_met?(self) }
162
- {:show => arr[0].map{|d| d.question_group_id.nil? ? "question_#{d.question_id}" : "question_group_#{d.question_group_id}"}, :hide => arr[1].map{|d| d.question_group_id.nil? ? "question_#{d.question_id}" : "question_group_#{d.question_group_id}"}}
116
+ def all_dependencies(question_ids = nil)
117
+ arr = dependencies(question_ids).partition{|d| d.is_met?(self) }
118
+ {:show => arr[0].map{|d| d.question_group_id.nil? ? "q_#{d.question_id}" : "qg_#{d.question_group_id}"}, :hide => arr[1].map{|d| d.question_group_id.nil? ? "q_#{d.question_id}" : "qg_#{d.question_group_id}"}}
163
119
  end
164
120
 
165
121
  # Check existence of responses to questions from a given survey_section
166
122
  def no_responses_for_section?(section)
167
- self.responses.count(:conditions => {:survey_section_id => section.id}) == 0
123
+ !responses.any?{|r| r.survey_section_id == section.id}
168
124
  end
169
125
 
170
126
  protected
171
127
 
172
128
  def dependencies(question_ids = nil)
173
- question_ids ||= Question.find_all_by_survey_section_id(current_section_id).map(&:id)
174
- depdendecy_ids = DependencyCondition.all(:conditions => {:question_id => question_ids}).map(&:dependency_id)
175
- Dependency.find(depdendecy_ids, :include => :dependency_conditions)
129
+ Dependency.all(:include => :dependency_conditions, :conditions => {:dependency_conditions => {:question_id => question_ids || responses.map(&:question_id)}})
176
130
  end
177
131
  end
178
132
  end
@@ -50,7 +50,6 @@ module Surveyor
50
50
  else
51
51
  @section = @sections.with_includes.first
52
52
  end
53
- @questions = @section.questions
54
53
  @dependents = (@response_set.unanswered_dependencies - @section.questions) || []
55
54
  else
56
55
  flash[:notice] = t('surveyor.unable_to_find_your_responses')
@@ -59,38 +58,28 @@ module Surveyor
59
58
  end
60
59
 
61
60
  def update
62
- saved = nil
63
- ActiveRecord::Base.transaction do
64
- if @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => :answer},:lock => true)
65
- @response_set.current_section_id = params[:current_section_id]
66
- else
67
- flash[:notice] = t('surveyor.unable_to_find_your_responses')
68
- redirect_to(available_surveys_path) and return
69
- end
70
-
71
- if params[:responses] or params[:response_groups]
72
- @response_set.clear_responses
73
- saved = @response_set.update_attributes(:response_attributes => (params[:responses] || {}).dup ,
74
- :response_group_attributes => (params[:response_groups] || {}).dup) #copy (dup) to preserve params because we manipulate params in the response_set methods
75
- if (saved && params[:finish])
76
- @response_set.complete!
77
- saved = @response_set.save!
78
- end
79
- end
61
+ @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => :answer}, :lock => true)
62
+ return redirect_with_message(available_surveys_path, :notice, t('surveyor.unable_to_find_your_responses')) if @response_set.blank?
63
+ saved = false
64
+ ActiveRecord::Base.transaction do
65
+ saved = @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(params[:r]))
66
+ saved = @response_set.complete! if saved && params[:finish]
80
67
  end
68
+ return redirect_with_message(surveyor_finish, :notice, t('surveyor.completed_survey')) if saved && params[:finish]
69
+
81
70
  respond_to do |format|
82
71
  format.html do
83
- if saved && params[:finish]
84
- flash[:notice] = t('surveyor.completed_survey')
85
- redirect_to surveyor_finish
86
- else
87
- flash[:notice] = t('surveyor.unable_to_update_survey') if !saved #and !saved.nil? # saved.nil? is true if there are no questions on the page (i.e. if it only contains a label)
88
- redirect_to :action => "edit", :anchor => anchor_from(params[:section]), :params => {:section => section_id_from(params[:section])}
89
- end
72
+ flash[:notice] = t('surveyor.unable_to_update_survey') unless saved
73
+ redirect_to :action => "edit", :anchor => anchor_from(params[:section]), :params => {:section => section_id_from(params[:section])}
90
74
  end
91
- # No redirect needed if we're talking to the page via json
92
75
  format.js do
93
- render :json => @response_set.all_dependencies.to_json
76
+ ids, remove, question_ids = {}, {}, []
77
+ ResponseSet.reject_or_destroy_blanks(params[:r]).each do |k,v|
78
+ ids[k] = @response_set.responses.find(:first, :conditions => v).id if !v.has_key?("id")
79
+ remove[k] = v["id"] if v.has_key?("id") && v.has_key?("_destroy")
80
+ question_ids << v["question_id"]
81
+ end
82
+ render :json => {"ids" => ids, "remove" => remove}.merge(@response_set.reload.all_dependencies(question_ids))
94
83
  end
95
84
  end
96
85
  end
@@ -118,5 +107,17 @@ module Surveyor
118
107
  def surveyor_finish
119
108
  available_surveys_path
120
109
  end
110
+
111
+ def redirect_with_message(path, message_type, message)
112
+ respond_to do |format|
113
+ format.html do
114
+ flash[message_type] = message if !message.blank? and !message_type.blank?
115
+ redirect_to path
116
+ end
117
+ format.js do
118
+ render :text => message, :status => 403
119
+ end
120
+ end
121
+ end
121
122
  end
122
123
  end
@@ -60,18 +60,4 @@ namespace :surveyor do
60
60
  puts "There are no surveys without response sets"
61
61
  end
62
62
  end
63
- end
64
-
65
- namespace :spec do
66
- namespace :plugins do
67
- begin
68
- require 'spec/rake/spectask'
69
- desc "Runs the examples for surveyor"
70
- Spec::Rake::SpecTask.new(:surveyor) do |t|
71
- t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
72
- t.spec_files = FileList['vendor/plugins/surveyor/spec/**/*_spec.rb']
73
- end
74
- rescue MissingSourceFile
75
- end
76
- end
77
- end
63
+ end
@@ -151,15 +151,15 @@ describe SurveyorController do
151
151
  end
152
152
  def do_put_with_finish
153
153
  responses = {
154
- "6"=>{"question_id"=>"6", "20"=>{"string_value"=>"saf"}}, #string
155
- "7"=>{"question_id"=>"7", "21"=>{"text_value"=>""}}, #text
156
- "1"=>{"question_id"=>"1", "answer_id"=>"1", "4"=>{"string_value"=>""}}, #radio+txt
157
- "2"=>{"answer_id"=>"6"}, #radio
158
- "3"=>{"answer_id"=>"10"}, #radio
154
+ "6"=>{"question_id"=>"6", "answer_id" => "6", "string_value"=>"saf"}, #string
155
+ "7"=>{"question_id"=>"7", "answer_id" => "11", "text_value"=>"foo"}, #text
156
+ "1"=>{"question_id"=>"1", "answer_id"=>"1", "string_value"=>"bar"}, #radio+txt
157
+ "2"=>{"question_id"=>"2", "answer_id"=>"6"}, #radio
158
+ "3"=>{"question_id"=>"3", "answer_id"=>"10"}, #radio
159
159
  "4"=>{"question_id"=>"4", "answer_id"=>"15"}, #check
160
- "5"=>{"question_id"=>"5", "16"=>{"selected"=>"1"}, "19"=>{"string_value"=>""}} #check+txt
160
+ "5"=>{"question_id"=>"5", "answer_id"=>"16", "string_value"=>""} #check+txt
161
161
  }
162
- put :update, :survey_code => "XYZ", :response_set_code => "PDQ", :finish => "finish", :responses => responses
162
+ put :update, :survey_code => "XYZ", :response_set_code => "PDQ", :finish => "finish", :r => responses
163
163
  end
164
164
 
165
165
  it "should find the response set requested" do
@@ -169,7 +169,6 @@ describe SurveyorController do
169
169
  it "should redirect to 'edit' without params" do
170
170
  do_put
171
171
  response.should redirect_to(:action => :edit)
172
- flash[:notice].should == "Unable to update survey"
173
172
  end
174
173
  it "should complete the found response set on finish" do
175
174
  do_put_with_finish
@@ -182,4 +181,37 @@ describe SurveyorController do
182
181
  end
183
182
 
184
183
  end
184
+
185
+ describe "update my survey with ajax" do
186
+ before(:each) do
187
+ @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
188
+ @section = Factory(:survey_section, :survey => @survey)
189
+ @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
190
+ ResponseSet.stub!(:find_by_access_code).and_return(@response_set)
191
+ end
192
+ def do_ajax_put(r)
193
+ xhr :put, :update, :survey_code => "XYZ", :response_set_code => "PDQ", :r => r
194
+ end
195
+ it "should return an id for new responses" do
196
+ do_ajax_put({
197
+ "2"=>{"question_id"=>"4", "answer_id"=>"14"}, #check
198
+ "4"=>{"question_id"=>"4", "answer_id"=>"15"} #check
199
+ })
200
+ JSON.parse(response.body).should == {"ids" => {"2" => 1, "4" => 2}, "remove" => {}, "show" => [], "hide" => []}
201
+ end
202
+ it "should return a delete for when responses are removed" do
203
+ r = @response_set.responses.create(:question_id => 4, :answer_id => 14)
204
+ do_ajax_put({
205
+ "2"=>{"question_id"=>"4", "answer_id"=>"", "id" => r.id} # uncheck
206
+ })
207
+ JSON.parse(response.body).should == {"ids" => {}, "remove" => {"2" => r.id}, "show" => [], "hide" => []}
208
+ end
209
+ it "should return dependencies" do
210
+ @response_set.should_receive(:all_dependencies).and_return({"show" => ['q_1'], "hide" => ['q_2']})
211
+ do_ajax_put({
212
+ "4"=>{"question_id"=>"9", "answer_id"=>"12"} #check
213
+ })
214
+ JSON.parse(response.body).should == {"ids" => {"4" => 1}, "remove" => {}, "show" => ['q_1'], "hide" => ["q_2"]}
215
+ end
216
+ end
185
217
  end