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,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SurveyorHelper do
4
+ before(:each) do
5
+
6
+ end
7
+ it "should return the question text with number" do
8
+ q1 = Factory(:question)
9
+ q2 = Factory(:question, :display_type => "label")
10
+ q3 = Factory(:question, :dependency => Factory(:dependency))
11
+ q4 = Factory(:question, :display_type => "image", :text => "something.jpg")
12
+ q5 = Factory(:question, :question_group => Factory(:question_group))
13
+ helper.q_text(q1).should == "1) #{q1.text}"
14
+ helper.q_text(q2).should == q2.text
15
+ helper.q_text(q3).should == q3.text
16
+ helper.q_text(q4).should == '<img alt="Something" src="/images/something.jpg" />'
17
+ helper.q_text(q5).should == q5.text
18
+ end
19
+ it "should return the group text with number" do
20
+ g1 = Factory(:question_group)
21
+ helper.q_text(g1).should == "1) #{g1.text}"
22
+ end
23
+ it "should find or create responses, with index" do
24
+ q1 = Factory(:question, :answers => [a = Factory(:answer, :text => "different")])
25
+ q2 = Factory(:question, :answers => [b = Factory(:answer, :text => "strokes")])
26
+ q3 = Factory(:question, :answers => [c = Factory(:answer, :text => "folks")])
27
+ rs = Factory(:response_set, :responses => [r1 = Factory(:response, :question => q1, :answer => a), r3 = Factory(:response, :question => q3, :answer => c, :response_group => 1)])
28
+
29
+ helper.response_for(rs, nil).should == nil
30
+ helper.response_for(nil, q1).should == nil
31
+ helper.response_for(rs, q1).should == r1
32
+ helper.response_for(rs, q1, a).should == r1
33
+ helper.response_for(rs, q2).attributes.should == Response.new(:question => q2, :response_set => rs).attributes
34
+ helper.response_for(rs, q2, b).attributes.should == Response.new(:question => q2, :response_set => rs).attributes
35
+ helper.response_for(rs, q3, c, "1").should == r3
36
+
37
+ end
38
+ it "should keep an index of responses" do
39
+ helper.response_idx.should == "1"
40
+ helper.response_idx.should == "2"
41
+ helper.response_idx(false).should == "2"
42
+ helper.response_idx.should == "3"
43
+ end
44
+ it "should translate response class into attribute" do
45
+ helper.rc_to_attr(:string).should == :string_value
46
+ helper.rc_to_attr(:text).should == :text_value
47
+ helper.rc_to_attr(:integer).should == :integer_value
48
+ helper.rc_to_attr(:float).should == :float_value
49
+ helper.rc_to_attr(:datetime).should == :datetime_value
50
+ helper.rc_to_attr(:date).should == :datetime_value
51
+ helper.rc_to_attr(:time).should == :datetime_value
52
+ end
53
+ end
@@ -15,21 +15,26 @@ describe Answer, "when creating a new answer" do
15
15
  # @answer.should_not be_valid
16
16
  # end
17
17
 
18
- it "should have 'default' renderer with nil question.pick and response_class" do
19
- @answer.question = Factory(:question, :pick => nil)
20
- @answer.response_class = nil
21
- @answer.renderer.should == :default
18
+ it "should tell me its css class" do
19
+ @answer.custom_class = "foo bar"
20
+ @answer.css_class.should == "foo bar"
21
+ @answer.is_exclusive = true
22
+ @answer.css_class.should == "exclusive foo bar"
22
23
  end
23
24
 
24
- it "should have a_b renderer for a question.pick and B response_class" do
25
- @answer.question = Factory(:question, :pick => "a")
26
- @answer.response_class = "B"
27
- @answer.renderer.should == :a_b
25
+ it "should hide the label when hide_label is set" do
26
+ @answer.split_or_hidden_text.should == "Red"
27
+ @answer.hide_label = true
28
+ @answer.split_or_hidden_text.should == ""
29
+ end
30
+ it "should split up pre/post labels" do
31
+ @answer.text = "before|after|extra"
32
+ @answer.split_or_hidden_text(:pre).should == "before"
33
+ @answer.split_or_hidden_text(:post).should == "after|extra"
28
34
  end
29
35
  it "should delete validation when it is deleted" do
30
36
  v_id = Factory(:validation, :answer => @answer).id
31
37
  @answer.destroy
32
38
  Validation.find_by_id(v_id).should be_nil
33
39
  end
34
-
35
40
  end
@@ -1,82 +1,77 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
 
4
- describe DependencyCondition, "Class methods" do
4
+ describe DependencyCondition do
5
5
  it "should have a list of operators" do
6
6
  %w(== != < > <= >=).each do |operator|
7
7
  DependencyCondition.operators.include?(operator).should be_true
8
8
  end
9
- end
10
- end
11
-
12
- describe DependencyCondition, "instance" do
13
- before(:each) do
14
- @dependency_condition = DependencyCondition.new(:dependency_id => 1, :question_id => 45, :operator => "==", :answer_id => 23, :rule_key => "1")
15
- end
9
+ end
10
+ describe "instance" do
11
+ before(:each) do
12
+ @dependency_condition = DependencyCondition.new(:dependency_id => 1, :question_id => 45, :operator => "==", :answer_id => 23, :rule_key => "1")
13
+ end
16
14
 
17
- it "should be valid" do
18
- @dependency_condition.should be_valid
19
- end
15
+ it "should be valid" do
16
+ @dependency_condition.should be_valid
17
+ end
20
18
 
21
- it "should be invalid without a parent dependency_id, question_id" do
22
- # this causes issues with building and saving
23
- # @dependency_condition.dependency_id = nil
24
- # @dependency_condition.should have(1).errors_on(:dependency_id)
25
- # @dependency_condition.question_id = nil
26
- # @dependency_condition.should have(1).errors_on(:question_id)
27
- end
19
+ it "should be invalid without a parent dependency_id, question_id" do
20
+ # this causes issues with building and saving
21
+ # @dependency_condition.dependency_id = nil
22
+ # @dependency_condition.should have(1).errors_on(:dependency_id)
23
+ # @dependency_condition.question_id = nil
24
+ # @dependency_condition.should have(1).errors_on(:question_id)
25
+ end
28
26
 
29
- it "should be invalid without an operator" do
30
- @dependency_condition.operator = nil
31
- @dependency_condition.should have(2).errors_on(:operator)
32
- end
27
+ it "should be invalid without an operator" do
28
+ @dependency_condition.operator = nil
29
+ @dependency_condition.should have(2).errors_on(:operator)
30
+ end
33
31
 
34
- it "should be invalid without a rule_key" do
35
- @dependency_condition.should be_valid
36
- @dependency_condition.rule_key = nil
37
- @dependency_condition.should_not be_valid
38
- @dependency_condition.should have(1).errors_on(:rule_key)
39
- end
32
+ it "should be invalid without a rule_key" do
33
+ @dependency_condition.should be_valid
34
+ @dependency_condition.rule_key = nil
35
+ @dependency_condition.should_not be_valid
36
+ @dependency_condition.should have(1).errors_on(:rule_key)
37
+ end
40
38
 
41
- it "should have unique rule_key within the context of a dependency" do
42
- @dependency_condition.should be_valid
43
- DependencyCondition.create(:dependency_id => 2, :question_id => 46, :operator => "==", :answer_id => 14, :rule_key => "2")
44
- @dependency_condition.rule_key = "2" #rule key uniquness is scoped by dependency_id
45
- @dependency_condition.dependency_id = 2
46
- @dependency_condition.should_not be_valid
47
- @dependency_condition.should have(1).errors_on(:rule_key)
48
- end
39
+ it "should have unique rule_key within the context of a dependency" do
40
+ @dependency_condition.should be_valid
41
+ DependencyCondition.create(:dependency_id => 2, :question_id => 46, :operator => "==", :answer_id => 14, :rule_key => "2")
42
+ @dependency_condition.rule_key = "2" #rule key uniquness is scoped by dependency_id
43
+ @dependency_condition.dependency_id = 2
44
+ @dependency_condition.should_not be_valid
45
+ @dependency_condition.should have(1).errors_on(:rule_key)
46
+ end
49
47
 
50
- it "should have an operator in DependencyCondition.operators" do
51
- DependencyCondition.operators.each do |o|
52
- @dependency_condition.operator = o
53
- @dependency_condition.should have(0).errors_on(:operator)
48
+ it "should have an operator in DependencyCondition.operators" do
49
+ DependencyCondition.operators.each do |o|
50
+ @dependency_condition.operator = o
51
+ @dependency_condition.should have(0).errors_on(:operator)
52
+ end
53
+ @dependency_condition.operator = "#"
54
+ @dependency_condition.should have(1).error_on(:operator)
54
55
  end
55
- @dependency_condition.operator = "#"
56
- @dependency_condition.should have(1).error_on(:operator)
57
- end
58
56
 
59
- it "should evaluate within the context of a response set object" do
60
- @response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 23)
61
- @response.answer = Answer.new(:question_id => 45, :response_class => "answer")
62
- @dependency_condition.is_met?([@response]).should be_true
63
- # inversion
64
- @alt_response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 55)
65
- @alt_response.answer = Answer.new(:question_id => 45, :response_class => "answer")
57
+ it "should evaluate within the context of a response set object" do
58
+ @response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 23)
59
+ @response.answer = Answer.new(:question_id => 45, :response_class => "answer")
60
+ @dependency_condition.is_met?([@response]).should be_true
61
+ # inversion
62
+ @alt_response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 55)
63
+ @alt_response.answer = Answer.new(:question_id => 45, :response_class => "answer")
66
64
 
67
- @dependency_condition.is_met?([@alt_response]).should be_false
68
- end
65
+ @dependency_condition.is_met?([@alt_response]).should be_false
66
+ end
69
67
 
70
- it "converts to a hash for evaluation by the dependency object" do
71
- @response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 23)
72
- @rs = mock(ResponseSet, :responses => [@response])
73
- @dependency_condition.stub!(:is_met?).and_return(true)
74
- @dependency_condition.to_hash(@rs)
68
+ it "converts to a hash for evaluation by the dependency object" do
69
+ @response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 23)
70
+ @rs = mock(ResponseSet, :responses => [@response])
71
+ @dependency_condition.stub!(:is_met?).and_return(true)
72
+ @dependency_condition.to_hash(@rs)
73
+ end
75
74
  end
76
- end
77
-
78
- describe DependencyCondition, "evaluating the response_set state" do
79
-
80
75
  describe "when if given a response object whether the dependency is satisfied using '=='" do
81
76
  before(:each) do
82
77
  @dep_c = DependencyCondition.new(:answer_id => 2, :operator => "==")
@@ -23,13 +23,13 @@ describe QuestionGroup do
23
23
  @dependency = Factory(:dependency)
24
24
  @question_group.dependency = @dependency
25
25
  @dependency.should_receive(:is_met?).and_return(true)
26
- @question_group.css_class(Factory(:response_set)).should == "dependent"
26
+ @question_group.css_class(Factory(:response_set)).should == "g_dependent"
27
27
 
28
28
  @dependency.should_receive(:is_met?).and_return(false)
29
- @question_group.css_class(Factory(:response_set)).should == "dependent hidden"
29
+ @question_group.css_class(Factory(:response_set)).should == "g_dependent g_hidden"
30
30
 
31
31
  @question_group.custom_class = "foo bar"
32
32
  @dependency.should_receive(:is_met?).and_return(false)
33
- @question_group.css_class(Factory(:response_set)).should == "dependent hidden foo bar"
33
+ @question_group.css_class(Factory(:response_set)).should == "g_dependent g_hidden foo bar"
34
34
  end
35
35
  end
@@ -28,6 +28,16 @@ describe Question, "when creating a new question" do
28
28
  @question.pick = nil
29
29
  @question.pick.should == nil
30
30
  end
31
+
32
+ it "should split the text" do
33
+ @question.split_text.should == "What is your favorite color?"
34
+ @question.split_text(:pre).should == "What is your favorite color?"
35
+ @question.split_text(:post).should == ""
36
+ @question.text = "before|after|extra"
37
+ @question.split_text.should == "before|after|extra"
38
+ @question.split_text(:pre).should == "before"
39
+ @question.split_text(:post).should == "after|extra"
40
+ end
31
41
  end
32
42
 
33
43
  describe Question, "that has answers" do
@@ -3,97 +3,109 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe ResponseSet do
4
4
  before(:each) do
5
5
  @response_set = Factory(:response_set)
6
+ @radio_response_attributes = HashWithIndifferentAccess.new({"1"=>{"question_id"=>"1", "answer_id"=>"1", "string_value"=>"XXL"}, "2"=>{"question_id"=>"2", "answer_id"=>"6"}, "3"=>{"question_id"=>"3"}})
7
+ @checkbox_response_attributes = HashWithIndifferentAccess.new({"1"=>{"question_id"=>"9", "answer_id"=>"11"}, "2"=>{"question_id"=>"9", "answer_id"=>"12"}})
8
+ @other_response_attributes = HashWithIndifferentAccess.new({"6"=>{"question_id"=>"6", "answer_id" => "3", "string_value"=>""}, "7"=>{"question_id"=>"7", "answer_id" => "4", "text_value"=>"Brian is tired"}, "5"=>{"question_id"=>"5", "answer_id" => "5", "string_value"=>""}})
6
9
  end
10
+
7
11
  it "should have a unique code with length 10 that identifies the survey" do
8
12
  @response_set.access_code.should_not be_nil
9
13
  @response_set.access_code.length.should == 10
10
14
  end
15
+
11
16
  it "is completable" do
12
17
  @response_set.completed_at.should be_nil
13
18
  @response_set.complete!
14
19
  @response_set.completed_at.should_not be_nil
15
20
  @response_set.completed_at.is_a?(Time).should be_true
16
21
  end
22
+
17
23
  it "does not allow completion through mass assignment" do
18
24
  @response_set.completed_at.should be_nil
19
25
  @response_set.update_attributes(:completed_at => Time.now)
20
26
  @response_set.completed_at.should be_nil
21
27
  end
22
- end
23
- describe ResponseSet, "Updating the response set" do
24
- before(:each) do
25
- @response_set = Factory(:response_set)
26
- # {"responses"=>{
27
- # "6"=>{"question_id"=>"6", "20"=>{"string_value"=>""}},
28
- # "7"=>{"question_id"=>"7", "21"=>{"text_value"=>"Brian is tired"}},
29
- # "1"=>{"question_id"=>"1", "answer_id"=>"1", "4"=>{"string_value"=>"XXL"}},
30
- # "2"=>{"question_id"=>"2", "answer_id"=>"6"},
31
- # "3"=>{"question_id"=>"3"},
32
- # "4"=>{"question_id"=>"4"},
33
- # "5"=>{"question_id"=>"5", "19"=>{"string_value"=>""}}},
34
- # "survey_code"=>"test_survey",
35
- # "commit"=>"Next Section (Utensiles and you!) >>",
36
- # "authenticity_token"=>"d03bc1b52fa9669e1ed87c313b939836e7b93e34",
37
- # "_method"=>"put",
38
- # "action"=>"update",
39
- # "controller"=>"app",
40
- # "response_set_code"=>"cIFn0DnxlU",
41
- # "section"=>"2"}
42
-
43
- #TODO test views to produce these params. e.g., blank responses should still have a hash with question_id
44
- @radio_response_attributes = HashWithIndifferentAccess.new({
45
- "1"=>{"question_id"=>"1", "answer_id"=>"1", "4"=>{"string_value"=>"XXL"}},
46
- "2"=>{"question_id"=>"2", "answer_id"=>"6"},
47
- "3"=>{"question_id"=>"3"}
48
- })
49
- @other_response_attributes = HashWithIndifferentAccess.new({
50
- "6"=>{"question_id"=>"6", "20"=>{"string_value"=>""}},
51
- "7"=>{"question_id"=>"7", "21"=>{"text_value"=>"Brian is tired"}},
52
- "5"=>{"question_id"=>"5", "19"=>{"string_value"=>""}}
53
- })
54
- end
55
28
 
56
29
  it "should save new responses from radio buttons, ignoring blanks" do
57
- @response_set.update_attributes(:response_attributes => @radio_response_attributes)
30
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(@radio_response_attributes))
58
31
  @response_set.responses.should have(2).items
59
32
  @response_set.responses.detect{|r| r.question_id == 2}.answer_id.should == 6
60
33
  end
34
+
61
35
  it "should save new responses from other types, ignoring blanks" do
62
- @response_set.update_attributes(:response_attributes => @other_response_attributes)
36
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(@other_response_attributes))
63
37
  @response_set.responses.should have(1).items
64
38
  @response_set.responses.detect{|r| r.question_id == 7}.text_value.should == "Brian is tired"
65
39
  end
40
+
66
41
  it "should ignore data if corresponding radio button is not selected" do
67
- @response_set.update_attributes(:response_attributes => @radio_response_attributes)
42
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(@radio_response_attributes))
68
43
  @response_set.responses.select{|r| r.question_id == 2}.should have(1).item
69
44
  @response_set.responses.detect{|r| r.question_id == 2}.string_value.should == nil
70
45
  end
71
- it "should preserve data in checkboxes regardless of selection" do
72
- pending
73
- end
74
46
 
75
- it "should give convenient access to responses through response_for" do
76
- @response_set.save #need to save for the associated models to build/save
77
- @response_set.attributes = {:response_attributes => @radio_response_attributes}
78
- @response_set.save.should be_true
47
+ it "should preserve response ids in checkboxes when adding another checkbox" do
48
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(@checkbox_response_attributes))
79
49
  @response_set.responses.should have(2).items
80
-
81
- pending
50
+ initial_response_ids = @response_set.responses.map(&:id)
51
+ # adding a checkbox
52
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks({"1"=>{"question_id"=>"9", "answer_id"=>"13"}}))
53
+ @response_set.responses.should have(3).items
54
+ (@response_set.responses.map(&:id) - initial_response_ids).size.should == 1
82
55
  end
83
- describe "assoication of responses to a survey_section" do
84
- before(:each) do
85
- @section = Factory(:survey_section)
86
- @response_set.current_section_id = @section.id
87
- end
88
- it "should detect existence of responses to questions that belong to a given survey_section" do
89
- @response_set.update_attributes(:response_attributes => @radio_response_attributes)
90
- @response_set.no_responses_for_section?(@section).should be_false
91
- end
92
- it "should detect absence of responses to questions that belong to a given survey_section" do
93
- @response_set.update_attributes(:response_attributes => @radio_response_attributes) #responses are associated with @section
94
- @another_section = Factory(:survey_section)
95
- @response_set.no_responses_for_section?(@another_section).should be_true
96
- end
56
+
57
+ it "should preserve response ids in checkboxes when removing another checkbox" do
58
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks(@checkbox_response_attributes))
59
+ @response_set.responses.should have(2).items
60
+ initial_response_ids = @response_set.responses.map(&:id)
61
+ # removing a checkbox, reload the response set
62
+ @response_set.update_attributes(:responses_attributes => ResponseSet.reject_or_destroy_blanks({"1"=>{"question_id"=>"9", "answer_id"=>"", "id" => initial_response_ids.first}}))
63
+ @response_set.reload.responses.should have(1).items
64
+ (initial_response_ids - @response_set.responses.map(&:id)).size.should == 1
65
+ end
66
+ it "should clean up a blank or empty hash" do
67
+ ResponseSet.reject_or_destroy_blanks(nil).should == {}
68
+ ResponseSet.reject_or_destroy_blanks({}).should == {}
69
+ end
70
+ it "should clean up responses_attributes before passing to nested_attributes" do
71
+ hash_of_hashes = {
72
+ "11" => {"question_id" => "1", "answer_id" => [""]}, # new checkbox, blank
73
+ "12" => {"question_id" => "2", "answer_id" => ["", "124"]}, # new checkbox, checked
74
+ "13" => {"id" => "101", "question_id" => "3", "answer_id" => [""]}, # existing checkbox, unchecked
75
+ "14" => {"id" => "102", "question_id" => "4", "answer_id" => ["", "147"]}, # existing checkbox, left alone
76
+ "15" => {"question_id" => "5", "answer_id" => ""}, # new radio, blank
77
+ "16" => {"question_id" => "6", "answer_id" => "161"}, # new radio, selected
78
+ "17" => {"id" => "103", "question_id" => "7", "answer_id" => "171"}, # existing radio, changed
79
+ "18" => {"id" => "104", "question_id" => "8", "answer_id" => "181"}, # existing radio, unchanged
80
+ "19" => {"question_id" => "9", "answer_id" => "191", "string_value" => ""}, # new string, blank
81
+ "20" => {"question_id" => "10", "answer_id" => "201", "string_value" => "hi"}, # new string, filled
82
+ "21" => {"id" => "105", "question_id" => "11", "answer_id" => "211", "string_value" => ""}, # existing string, cleared
83
+ "22" => {"id" => "106", "question_id" => "12", "answer_id" => "221", "string_value" => "ho"}, # existing string, changed
84
+ "23" => {"id" => "107", "question_id" => "13", "answer_id" => "231", "string_value" => "hi"} # existing string, unchanged
85
+ }
86
+ ResponseSet.reject_or_destroy_blanks(hash_of_hashes).should == {
87
+ # "11" => {"question_id" => "1", "answer_id" => [""]}, # new checkbox, blank
88
+ "12" => {"question_id" => "2", "answer_id" => ["", "124"]}, # new checkbox, checked
89
+ "13" => {"id" => "101", "question_id" => "3", "answer_id" => [""], "_destroy" => "true"}, # existing checkbox, unchecked
90
+ "14" => {"id" => "102", "question_id" => "4", "answer_id" => ["", "147"]}, # existing checkbox, left alone
91
+ # "15" => {"question_id" => "5", "answer_id" => ""}, # new radio, blank
92
+ "16" => {"question_id" => "6", "answer_id" => "161"}, # new radio, selected
93
+ "17" => {"id" => "103", "question_id" => "7", "answer_id" => "171"}, # existing radio, changed
94
+ "18" => {"id" => "104", "question_id" => "8", "answer_id" => "181"}, # existing radio, unchanged
95
+ # "19" => {"question_id" => "9", "answer_id" => "191", "string_value" => ""}, # new string, blank
96
+ "20" => {"question_id" => "10", "answer_id" => "201", "string_value" => "hi"}, # new string, filled
97
+ "21" => {"id" => "105", "question_id" => "11", "answer_id" => "211", "string_value" => "", "_destroy" => "true"}, # existing string, cleared
98
+ "22" => {"id" => "106", "question_id" => "12", "answer_id" => "221", "string_value" => "ho"}, # existing string, changed
99
+ "23" => {"id" => "107", "question_id" => "13", "answer_id" => "231", "string_value" => "hi"} # existing string, unchanged
100
+ }
101
+ end
102
+ it "should remove responses" do
103
+ r = @response_set.responses.create(:question_id => 1, :answer_id => 2)
104
+ r.id.should_not be nil
105
+ @response_set.should have(1).responses
106
+ ResponseSet.reject_or_destroy_blanks({"2"=>{"question_id"=>"1", "id"=> r.id, "answer_id"=>[""]}}).should == {"2"=>{"question_id"=>"1", "id"=> r.id, "_destroy"=> "true", "answer_id"=>[""]}}
107
+ @response_set.update_attributes(:responses_attributes => {"2"=>{"question_id"=>"1", "id"=> r.id, "_destroy"=> "true", "answer_id"=>[""]}}).should be_true
108
+ @response_set.reload.should have(0).responses
97
109
  end
98
110
  end
99
111
 
@@ -116,7 +128,6 @@ describe ResponseSet, "with dependencies" do
116
128
  Factory(:dependency_condition, :rule_key => "B", :question_id => @do_you_like_pie.id, :operator => "==", :answer_id => @do_you_like_pie.answers.first.id, :dependency_id => @what_bakery_dep.id)
117
129
  # Responses
118
130
  @response_set = Factory(:response_set)
119
- @response_set.current_section_id = @section.id
120
131
  @response_set.responses << Factory(:response, :question_id => @do_you_like_pie.id, :answer_id => @do_you_like_pie.answers.first.id, :response_set_id => @response_set.id)
121
132
  @response_set.responses << Factory(:response, :string_value => "pecan pie", :question_id => @what_flavor.id, :answer_id => @what_flavor.answers.first.id, :response_set_id => @response_set.id)
122
133
  end
@@ -125,7 +136,7 @@ describe ResponseSet, "with dependencies" do
125
136
  @response_set.unanswered_dependencies.should == [@what_bakery]
126
137
  end
127
138
  it "should list answered and unanswered dependencies to show inline (javascript turned on)" do
128
- @response_set.all_dependencies[:show].should == ["question_#{@what_flavor.id}", "question_#{@what_bakery.id}"]
139
+ @response_set.all_dependencies[:show].should == ["q_#{@what_flavor.id}", "q_#{@what_bakery.id}"]
129
140
  end
130
141
 
131
142
  end
@@ -245,7 +256,6 @@ describe ResponseSet, "exporting csv" do
245
256
  @what_bakery.answers << Factory(:answer, :response_class => :string, :question_id => @what_bakery.id)
246
257
  # Responses
247
258
  @response_set = Factory(:response_set)
248
- @response_set.current_section_id = @section.id
249
259
  @response_set.responses << Factory(:response, :question_id => @do_you_like_pie.id, :answer_id => @do_you_like_pie.answers.first.id, :response_set_id => @response_set.id)
250
260
  @response_set.responses << Factory(:response, :string_value => "pecan pie", :question_id => @what_flavor.id, :answer_id => @what_flavor.answers.first.id, :response_set_id => @response_set.id)
251
261
  end