surveyor 0.20.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.gitignore +26 -0
  2. data/Gemfile +11 -0
  3. data/Gemfile.rails_version +13 -0
  4. data/README.md +40 -22
  5. data/Rakefile +92 -29
  6. data/app/helpers/survey_form_builder.rb +37 -0
  7. data/app/helpers/surveyor_helper.rb +9 -9
  8. data/app/views/partials/_answer.html.haml +1 -1
  9. data/app/views/partials/_question.html.haml +3 -3
  10. data/app/views/partials/_question_group.html.haml +2 -2
  11. data/app/views/partials/_section.html.haml +1 -1
  12. data/app/views/surveyor/edit.html.haml +11 -3
  13. data/app/views/surveyor/new.html.haml +8 -1
  14. data/ci-exec.sh +50 -0
  15. data/config/routes.rb +9 -13
  16. data/cucumber.yml +10 -0
  17. data/doc/question types.png +0 -0
  18. data/features/redcap_parser.feature +1 -1
  19. data/features/step_definitions/common_setps.rb +3 -0
  20. data/features/step_definitions/parser_steps.rb +8 -1
  21. data/features/step_definitions/surveyor_steps.rb +33 -9
  22. data/features/step_definitions/web_steps.rb +85 -165
  23. data/features/support/env.rb +40 -46
  24. data/features/support/paths.rb +4 -4
  25. data/features/support/selectors.rb +39 -0
  26. data/features/surveyor.feature +63 -13
  27. data/features/surveyor_dependencies.feature +289 -0
  28. data/features/surveyor_parser.feature +29 -20
  29. data/lib/formtastic/surveyor_builder.rb +9 -9
  30. data/lib/generators/surveyor/custom_generator.rb +17 -0
  31. data/lib/generators/surveyor/install_generator.rb +50 -0
  32. data/{generators/extend_surveyor/templates/extensions → lib/generators/surveyor/templates/app/controllers}/surveyor_controller.rb +0 -0
  33. data/{generators/extend_surveyor/templates/extensions → lib/generators/surveyor/templates/app/views/layouts}/surveyor_custom.html.erb +0 -0
  34. data/{generators/surveyor/templates → lib/generators/surveyor/templates/config}/locales/surveyor_en.yml +0 -0
  35. data/{generators/surveyor/templates → lib/generators/surveyor/templates/config}/locales/surveyor_es.yml +0 -0
  36. data/{generators/surveyor/templates → lib/generators/surveyor/templates/config}/locales/surveyor_he.yml +0 -0
  37. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_api_ids.rb +0 -0
  38. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_correct_answer_id_to_questions.rb +0 -0
  39. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_default_value_to_answers.rb +0 -0
  40. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_display_order_to_surveys.rb +0 -0
  41. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_display_type_to_answers.rb +0 -0
  42. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_index_to_response_sets.rb +0 -0
  43. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_index_to_surveys.rb +0 -0
  44. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_section_id_to_responses.rb +0 -0
  45. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_unique_indicies.rb +0 -0
  46. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_answers.rb +0 -0
  47. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_dependencies.rb +0 -0
  48. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_dependency_conditions.rb +0 -0
  49. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_question_groups.rb +0 -0
  50. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_questions.rb +0 -0
  51. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_response_sets.rb +0 -0
  52. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_responses.rb +0 -0
  53. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_survey_sections.rb +0 -0
  54. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_surveys.rb +0 -0
  55. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_validation_conditions.rb +0 -0
  56. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_validations.rb +0 -0
  57. data/{generators/surveyor/templates/assets/images → lib/generators/surveyor/templates/public/images/surveyor}/next.gif +0 -0
  58. data/{generators/surveyor/templates/assets/images → lib/generators/surveyor/templates/public/images/surveyor}/prev.gif +0 -0
  59. data/lib/generators/surveyor/templates/public/javascripts/surveyor/jquery.blockUI.js +499 -0
  60. data/{generators/surveyor/templates/assets/javascripts → lib/generators/surveyor/templates/public/javascripts/surveyor}/jquery.surveyor.js +47 -4
  61. data/{generators/surveyor/templates/assets/javascripts → lib/generators/surveyor/templates/public/javascripts/surveyor}/jquery.tools.min.js +0 -0
  62. data/{generators/surveyor/templates/assets → lib/generators/surveyor/templates/public}/stylesheets/sass/custom.sass +0 -0
  63. data/{generators/surveyor/templates/assets → lib/generators/surveyor/templates/public}/stylesheets/sass/surveyor.sass +2 -2
  64. data/{generators/surveyor/templates/assets/stylesheets → lib/generators/surveyor/templates/public/stylesheets/surveyor}/dateinput.css +0 -0
  65. data/{generators/surveyor/templates/assets/stylesheets → lib/generators/surveyor/templates/public/stylesheets/surveyor}/reset.css +0 -0
  66. data/{generators/surveyor/templates/assets/stylesheets → lib/generators/surveyor/templates/public/stylesheets/surveyor}/results.css +0 -0
  67. data/{generators/extend_surveyor/templates/EXTENDING_SURVEYOR → lib/generators/surveyor/templates/surveys/EXTENDING_SURVEYOR.md} +0 -0
  68. data/{generators → lib/generators}/surveyor/templates/surveys/kitchen_sink_survey.rb +0 -0
  69. data/{generators → lib/generators}/surveyor/templates/surveys/quiz.rb +0 -0
  70. data/lib/surveyor.rb +7 -0
  71. data/lib/surveyor/engine.rb +12 -0
  72. data/lib/surveyor/models/answer_methods.rb +0 -1
  73. data/lib/surveyor/models/dependency_condition_methods.rb +1 -3
  74. data/lib/surveyor/models/question_methods.rb +0 -1
  75. data/lib/surveyor/models/response_methods.rb +2 -2
  76. data/lib/surveyor/models/response_set_methods.rb +30 -14
  77. data/lib/surveyor/models/survey_methods.rb +2 -1
  78. data/lib/surveyor/models/survey_section_methods.rb +1 -2
  79. data/lib/surveyor/parser.rb +9 -7
  80. data/lib/surveyor/redcap_parser.rb +10 -7
  81. data/lib/surveyor/surveyor_controller_methods.rb +45 -11
  82. data/lib/surveyor/version.rb +3 -0
  83. data/lib/tasks/surveyor_tasks.rake +2 -2
  84. data/rails/init.rb +0 -1
  85. data/spec/controllers/surveyor_controller_spec.rb +81 -35
  86. data/spec/helpers/surveyor_helper_spec.rb +7 -1
  87. data/spec/models/dependency_condition_spec.rb +76 -63
  88. data/spec/models/response_set_spec.rb +52 -47
  89. data/spec/models/response_spec.rb +19 -16
  90. data/spec/spec_helper.rb +21 -44
  91. data/surveyor.gemspec +25 -176
  92. metadata +302 -77
  93. data/CHANGELOG +0 -335
  94. data/VERSION +0 -1
  95. data/ci-env.sh +0 -29
  96. data/generators/extend_surveyor/extend_surveyor_generator.rb +0 -21
  97. data/generators/surveyor/surveyor_generator.rb +0 -69
  98. data/generators/surveyor/templates/README +0 -10
  99. data/generators/surveyor/templates/tasks/surveyor.rb +0 -4
  100. data/hudson.rakefile +0 -33
  101. data/init_testbed.rakefile +0 -55
  102. data/testbed/Gemfile +0 -13
@@ -0,0 +1,3 @@
1
+ module Surveyor
2
+ VERSION = '0.21.0'
3
+ end
@@ -4,7 +4,7 @@ task :surveyor => :"surveyor:parse"
4
4
  namespace :surveyor do
5
5
  task :parse => :environment do
6
6
  raise "USAGE: file name required e.g. 'FILE=surveys/kitchen_sink_survey.rb'" if ENV["FILE"].blank?
7
- file = File.join(RAILS_ROOT, ENV["FILE"])
7
+ file = File.join(Rails.root, ENV["FILE"])
8
8
  raise "File does not exist: #{file}" unless FileTest.exists?(file)
9
9
  puts "--- Parsing #{file} ---"
10
10
  Surveyor::Parser.parse File.read(file)
@@ -13,7 +13,7 @@ namespace :surveyor do
13
13
  desc "generate and load survey from REDCap Data Dictionary (specify FILE=surveys/redcap.csv)"
14
14
  task :redcap => :environment do
15
15
  raise "USAGE: file name required e.g. 'FILE=surveys/redcap_demo_survey.csv'" if ENV["FILE"].blank?
16
- file = File.join(RAILS_ROOT, ENV["FILE"])
16
+ file = File.join(Rails.root, ENV["FILE"])
17
17
  raise "File does not exist: #{file}" unless FileTest.exists?(file)
18
18
  puts "--- Parsing #{file} ---"
19
19
  Surveyor::RedcapParser.parse File.read(file), File.basename(file, ".csv")
data/rails/init.rb CHANGED
@@ -1,2 +1 @@
1
- # For Rails 2.3 gem engine to work
2
1
  require 'surveyor'
@@ -1,26 +1,25 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe SurveyorController do
4
-
4
+
5
5
  # map.with_options :controller => 'surveyor' do |s|
6
6
  # s.available_surveys "#{root}", :conditions => {:method => :get}, :action => "new" # GET survey list
7
7
  # s.take_survey "#{root}:survey_code", :conditions => {:method => :post}, :action => "create" # Only POST of survey to create
8
8
  # s.view_my_survey "#{root}:survey_code/:response_set_code", :conditions => {:method => :get}, :action => "show" # GET viewable/printable? survey
9
- # s.edit_my_survey "#{root}:survey_code/:response_set_code/take", :conditions => {:method => :get}, :action => "edit" # GET editable survey
10
- # s.update_my_survey "#{root}:survey_code/:response_set_code", :conditions => {:method => :put}, :action => "update" # PUT edited survey
11
- # end
12
-
13
- describe "available surveys: GET /surveys" do
9
+ # s.edit_my_survey "#{root}:survey_code/:response_set_code/take", :conditions => {:method => :get}, :action => "edit" # GET editable survey
10
+ # s.update_my_survey "#{root}:survey_code/:response_set_code", :conditions => {:method => :put}, :action => "update" # PUT edited survey
11
+ # end
14
12
 
13
+ describe "available surveys: GET /surveys" do
15
14
  before(:each) do
16
15
  @survey = Factory(:survey)
17
16
  Survey.stub!(:find).and_return([@survey])
18
17
  end
19
-
18
+
20
19
  def do_get
21
20
  get :new
22
21
  end
23
-
22
+
24
23
  it "should render index template" do
25
24
  do_get
26
25
  response.should be_success
@@ -31,7 +30,7 @@ describe SurveyorController do
31
30
  Survey.should_receive(:find).with(:all).and_return([@survey])
32
31
  do_get
33
32
  end
34
-
33
+
35
34
  it "should assign the found surveys for the view" do
36
35
  do_get
37
36
  assigns[:surveys].should == [@survey]
@@ -39,14 +38,13 @@ describe SurveyorController do
39
38
  end
40
39
 
41
40
  describe "take survey: POST /surveys/xyz" do
42
-
43
41
  before(:each) do
44
42
  @survey = Factory(:survey, :title => "xyz", :access_code => "xyz")
45
43
  @response_set = Factory(:response_set, :access_code => "pdq")
46
44
  ResponseSet.stub!(:create).and_return(@response_set)
47
45
  Survey.stub!(:find_by_access_code).and_return(@survey)
48
46
  end
49
-
47
+
50
48
  describe "with success" do
51
49
  def do_post
52
50
  post :create, :survey_code => "xyz"
@@ -61,10 +59,11 @@ describe SurveyorController do
61
59
  end
62
60
  it "should redirect to the new response_set" do
63
61
  do_post
64
- response.should redirect_to(edit_my_survey_url(:survey_code => "xyz", :response_set_code => "pdq"))
62
+ response.should redirect_to(
63
+ edit_my_survey_url(:survey_code => "xyz", :response_set_code => "pdq"))
65
64
  end
66
65
  end
67
-
66
+
68
67
  describe "with failures" do
69
68
  it "should re-redirect to 'new' if ResponseSet failed create" do
70
69
  ResponseSet.should_receive(:create).and_return(false)
@@ -77,15 +76,31 @@ describe SurveyorController do
77
76
  response.should redirect_to(available_surveys_url)
78
77
  end
79
78
  end
79
+
80
+ describe "determining if javascript is enabled" do
81
+ it "sets the user session to know that javascript is enabled" do
82
+ post :create, :survey_code => "xyz", :surveyor_javascript_enabled => "true"
83
+ session[:surveyor_javascript].should_not be_nil
84
+ session[:surveyor_javascript].should == "enabled"
85
+ end
86
+
87
+ it "sets the user session to know that javascript is not enabled" do
88
+ post :create, :survey_code => "xyz", :surveyor_javascript_enabled => "not_true"
89
+ session[:surveyor_javascript].should_not be_nil
90
+ session[:surveyor_javascript].should == "not_enabled"
91
+ end
92
+
93
+ end
94
+
80
95
  end
81
96
 
82
97
  describe "view my survey: GET /surveys/xyz/pdq" do
83
- #integrate_views
84
98
  before(:each) do
85
- @survey = Factory(:survey, :title => "xyz", :access_code => "xyz", :sections => [Factory(:survey_section)])
99
+ @survey = Factory(:survey,
100
+ :title => "xyz", :access_code => "xyz", :sections => [Factory(:survey_section)])
86
101
  @response_set = Factory(:response_set, :access_code => "pdq", :survey => @survey)
87
102
  end
88
-
103
+
89
104
  def do_get
90
105
  get :show, :survey_code => "xyz", :response_set_code => "pdq"
91
106
  end
@@ -94,29 +109,28 @@ describe SurveyorController do
94
109
  do_get
95
110
  response.should be_success
96
111
  end
97
-
112
+
98
113
  it "should render show template" do
99
114
  do_get
100
115
  response.should render_template('show')
101
116
  end
102
-
117
+
103
118
  it "should find the response_set requested" do
104
- ResponseSet.should_receive(:find_by_access_code).with("pdq",{:include=>{:responses=>[:question, :answer]}}).and_return(@response_set)
119
+ ResponseSet.should_receive(:find_by_access_code).
120
+ with("pdq",{:include=>{:responses=>[:question, :answer]}}).and_return(@response_set)
105
121
  do_get
106
122
  end
107
123
 
108
124
  it "should redirect if :response_code not found" do
109
125
  get :show, :survey_code => "xyz", :response_set_code => "DIFFERENT"
110
- response.should redirect_to(available_surveys_url)
126
+ response.should redirect_to(available_surveys_url)
111
127
  end
112
-
113
128
  end
114
129
 
115
130
  describe "edit my survey: GET /surveys/XYZ/PDQ/take" do
116
-
117
131
  before(:each) do
118
132
  @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
119
- @section = Factory(:survey_section, :survey => @survey)
133
+ @section = Factory(:survey_section, :survey => @survey)
120
134
  @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
121
135
  end
122
136
 
@@ -128,27 +142,49 @@ describe SurveyorController do
128
142
  assigns[:response_set].should == @response_set
129
143
  assigns[:survey].should == @survey
130
144
  end
145
+
131
146
  it "should redirect if :response_code not found" do
132
147
  get :edit, :survey_code => "XYZ", :response_set_code => "DIFFERENT"
133
- response.should redirect_to(available_surveys_url)
148
+ response.should redirect_to(available_surveys_url)
134
149
  end
135
150
 
151
+ it "should only set dependents if javascript is not enabled" do
152
+ ResponseSet.should_receive(:find_by_access_code).and_return(@response_set)
153
+ controller.stub!(:get_unanswered_dependencies_minus_section_questions).
154
+ and_return([Question.new])
155
+
156
+ get :edit, :survey_code => "XYZ", :response_set_code => "PDQ"
157
+ assigns[:dependents].should_not be_empty
158
+ session[:surveyor_javascript].should be_nil
159
+ end
160
+
161
+ it "should not set dependents if javascript is enabled" do
162
+ ResponseSet.should_receive(:find_by_access_code).and_return(@response_set)
163
+ controller.stub!(:get_unanswered_dependencies_minus_section_questions).
164
+ and_return([Question.new])
165
+
166
+ session[:surveyor_javascript] = "enabled"
167
+
168
+ get :edit, :survey_code => "XYZ", :response_set_code => "PDQ"
169
+ assigns[:dependents].should be_empty
170
+ session[:surveyor_javascript].should == "enabled"
171
+ end
136
172
  end
137
173
 
138
174
  describe "update my survey: PUT /surveys/XYZ/PDQ" do
139
-
140
175
  before(:each) do
141
176
  @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
142
- @section = Factory(:survey_section, :survey => @survey)
177
+ @section = Factory(:survey_section, :survey => @survey)
143
178
  @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
144
179
  # @response_set.stub!(:update_attributes).and_return(true)
145
180
  # @response_set.stub!(:complete!).and_return(Time.now)
146
181
  # @response_set.stub!(:save).and_return(true)
147
182
  end
148
-
183
+
149
184
  def do_put
150
185
  put :update, :survey_code => "XYZ", :response_set_code => "PDQ"
151
186
  end
187
+
152
188
  def do_put_with_finish
153
189
  responses = {
154
190
  "6"=>{"question_id"=>"6", "answer_id" => "6", "string_value"=>"saf"}, #string
@@ -161,27 +197,29 @@ describe SurveyorController do
161
197
  }
162
198
  put :update, :survey_code => "XYZ", :response_set_code => "PDQ", :finish => "finish", :r => responses
163
199
  end
164
-
200
+
165
201
  it "should find the response set requested" do
166
202
  ResponseSet.should_receive(:find_by_access_code).and_return(@response_set)
167
203
  do_put
168
204
  end
205
+
169
206
  it "should redirect to 'edit' without params" do
170
207
  do_put
171
208
  response.should redirect_to(:action => :edit)
172
209
  end
173
- it "should complete the found response set on finish" do
210
+
211
+ it "should complete the found response set on finish" do
174
212
  do_put_with_finish
175
213
  flash[:notice].should == "Completed survey"
176
214
  end
215
+
177
216
  it "should redirect to available surveys if :response_code not found" do
178
217
  put :update, :survey_code => "XYZ", :response_set_code => "DIFFERENT"
179
218
  response.should redirect_to(available_surveys_url)
180
219
  flash[:notice].should == "Unable to find your responses to the survey"
181
220
  end
182
-
183
221
  end
184
-
222
+
185
223
  describe "update my survey with ajax" do
186
224
  before(:each) do
187
225
  @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
@@ -189,29 +227,37 @@ describe SurveyorController do
189
227
  @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
190
228
  ResponseSet.stub!(:find_by_access_code).and_return(@response_set)
191
229
  end
230
+
192
231
  def do_ajax_put(r)
193
232
  xhr :put, :update, :survey_code => "XYZ", :response_set_code => "PDQ", :r => r
194
233
  end
234
+
195
235
  it "should return an id for new responses" do
196
236
  do_ajax_put({
197
237
  "2"=>{"question_id"=>"4", "answer_id"=>"14"}, #check
198
238
  "4"=>{"question_id"=>"4", "answer_id"=>"15"} #check
199
239
  })
200
- JSON.parse(response.body).should == {"ids" => {"2" => 1, "4" => 2}, "remove" => {}, "show" => [], "hide" => []}
240
+ JSON.parse(response.body).
241
+ should == {"ids" => {"2" => 1, "4" => 2}, "remove" => {}, "show" => [], "hide" => []}
201
242
  end
243
+
202
244
  it "should return a delete for when responses are removed" do
203
245
  r = @response_set.responses.create(:question_id => 4, :answer_id => 14)
204
246
  do_ajax_put({
205
247
  "2"=>{"question_id"=>"4", "answer_id"=>"", "id" => r.id} # uncheck
206
248
  })
207
- JSON.parse(response.body).should == {"ids" => {}, "remove" => {"2" => r.id}, "show" => [], "hide" => []}
249
+ # r.id is a String with AR 3.0 and an int with AR 3.1
250
+ JSON.parse(response.body)['remove']['2'].to_s.should == r.id.to_s
208
251
  end
252
+
209
253
  it "should return dependencies" do
210
- @response_set.should_receive(:all_dependencies).and_return({"show" => ['q_1'], "hide" => ['q_2']})
254
+ @response_set.should_receive(:all_dependencies).
255
+ and_return({"show" => ['q_1'], "hide" => ['q_2']})
211
256
  do_ajax_put({
212
257
  "4"=>{"question_id"=>"9", "answer_id"=>"12"} #check
213
258
  })
214
- JSON.parse(response.body).should == {"ids" => {"4" => 1}, "remove" => {}, "show" => ['q_1'], "hide" => ["q_2"]}
259
+ JSON.parse(response.body).
260
+ should == {"ids" => {"4" => 1}, "remove" => {}, "show" => ['q_1'], "hide" => ["q_2"]}
215
261
  end
216
262
  end
217
263
  end
@@ -13,7 +13,13 @@ describe SurveyorHelper do
13
13
  helper.q_text(q1).should == "1) #{q1.text}"
14
14
  helper.q_text(q2).should == q2.text
15
15
  helper.q_text(q3).should == q3.text
16
- helper.q_text(q4).should == '<img alt="Something" src="/images/something.jpg" />'
16
+
17
+ dir = "images"
18
+ if Rails.application.config.respond_to?(:assets) && Rails.application.config.assets
19
+ dir = "assets"
20
+ end
21
+
22
+ helper.q_text(q4).should == %Q(<img alt="Something" src="/#{dir}/something.jpg" />)
17
23
  helper.q_text(q5).should == q5.text
18
24
  end
19
25
  it "should return the group text with number" do
@@ -6,10 +6,13 @@ describe DependencyCondition do
6
6
  %w(== != < > <= >=).each do |operator|
7
7
  DependencyCondition.operators.include?(operator).should be_true
8
8
  end
9
- end
9
+ end
10
+
10
11
  describe "instance" do
11
12
  before(:each) do
12
- @dependency_condition = DependencyCondition.new(:dependency_id => 1, :question_id => 45, :operator => "==", :answer_id => 23, :rule_key => "1")
13
+ @dependency_condition = DependencyCondition.new(
14
+ :dependency_id => 1, :question_id => 45, :operator => "==",
15
+ :answer_id => 23, :rule_key => "1")
13
16
  end
14
17
 
15
18
  it "should be valid" do
@@ -28,7 +31,7 @@ describe DependencyCondition do
28
31
  @dependency_condition.operator = nil
29
32
  @dependency_condition.should have(2).errors_on(:operator)
30
33
  end
31
-
34
+
32
35
  it "should be invalid without a rule_key" do
33
36
  @dependency_condition.should be_valid
34
37
  @dependency_condition.rule_key = nil
@@ -37,12 +40,14 @@ describe DependencyCondition do
37
40
  end
38
41
 
39
42
  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)
43
+ @dependency_condition.should be_valid
44
+ DependencyCondition.create(
45
+ :dependency_id => 2, :question_id => 46, :operator => "==",
46
+ :answer_id => 14, :rule_key => "2")
47
+ @dependency_condition.rule_key = "2" # rule key uniquness is scoped by dependency_id
48
+ @dependency_condition.dependency_id = 2
49
+ @dependency_condition.should_not be_valid
50
+ @dependency_condition.should have(1).errors_on(:rule_key)
46
51
  end
47
52
 
48
53
  it "should have an operator in DependencyCondition.operators" do
@@ -56,15 +61,17 @@ describe DependencyCondition do
56
61
 
57
62
  it "should evaluate within the context of a response set object" do
58
63
  @response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 23)
59
- @response.answer = Answer.new(:question_id => 45, :response_class => "answer")
64
+ @response.answer = Answer.new(:question_id => 45, :response_class => "answer").
65
+ tap { |a| a.id = 23 }
60
66
  @dependency_condition.is_met?([@response]).should be_true
61
67
  # 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")
68
+ @alt_response = Response.new(:question_id => 45, :response_set_id => 40)
69
+ @alt_response.answer = Answer.new(:question_id => 45, :response_class => "answer").
70
+ tap { |a| a.id = 55 }
64
71
 
65
72
  @dependency_condition.is_met?([@alt_response]).should be_false
66
73
  end
67
-
74
+
68
75
  it "converts to a hash for evaluation by the dependency object" do
69
76
  @response = Response.new(:question_id => 45, :response_set_id => 40, :answer_id => 23)
70
77
  @rs = mock(ResponseSet, :responses => [@response])
@@ -72,40 +79,45 @@ describe DependencyCondition do
72
79
  @dependency_condition.to_hash(@rs)
73
80
  end
74
81
  end
82
+
75
83
  describe "to_hash" do
76
84
  before do
77
- @response = mock(Response)
78
- @question = mock(Question)
79
- @dependency_condition = DependencyCondition.new(:rule_key => "A")
80
- @answer = mock(Answer)
81
- @question.stub!(:answers).and_return([@answer])
82
- @response.stub!(:answer).and_return(@answer)
83
- @rs = mock(ResponseSet, :responses => [@response])
84
- @dependency_condition.stub!(:question).and_return(@question)
85
+ @question = Factory(:question)
86
+ @answer = Factory(:answer, :question => @question)
87
+ @dependency_condition =
88
+ Factory(:dependency_condition, :rule_key => "A", :question => @question)
89
+ @rs = Factory(:response_set)
90
+ @response =
91
+ Factory(:response, :question => @question, :answer => @answer, :response_set => @rs)
92
+ @rs.responses << @response
93
+ @rs.save!
85
94
  end
86
-
95
+
87
96
  it "converts unmet condition to {:A => false}" do
88
97
  @dependency_condition.stub!(:is_met?).and_return(false)
89
98
  @dependency_condition.to_hash(@rs).should == {:A => false}
90
99
  end
91
-
100
+
92
101
  it "converts met condition to {:A => true}" do
93
102
  @dependency_condition.stub!(:is_met?).and_return(true)
94
103
  @dependency_condition.to_hash(@rs).should == {:A => true}
95
104
  end
96
-
105
+
97
106
  it "converts unanswered condition to {:A => false}" do
98
- @question.stub!(:answers).and_return([])
99
- @response.stub!(:answer).and_return(nil)
100
- @rs.stub!(:responses).and_return([])
101
- @dependency_condition.to_hash(@rs).should == {:A => false}
102
- end
107
+ question = Factory(:question)
108
+ dependency_condition =
109
+ Factory(:dependency_condition, :rule_key => "A", :question => @question)
110
+ rs = Factory(:response_set)
111
+ dependency_condition.to_hash(rs).should == {:A => false}
112
+ end
103
113
  end
114
+
104
115
  describe "when if given a response object whether the dependency is satisfied using '=='" do
105
116
  before(:each) do
106
- @dep_c = DependencyCondition.new(:answer_id => 2, :operator => "==")
107
- @select_answer = Answer.new(:question_id => 1, :response_class => "answer")
108
- @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => 2)
117
+ @select_answer = Answer.new(:question_id => 1, :response_class => "answer").
118
+ tap { |a| a.id = 2 }
119
+ @dep_c = DependencyCondition.new(:operator => "==", :answer_id => @select_answer.id)
120
+ @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => @select_answer.id)
109
121
  @response.answer = @select_answer
110
122
  @dep_c.answer = @select_answer
111
123
  @dep_c.as(:answer).should == 2
@@ -134,7 +146,7 @@ describe DependencyCondition do
134
146
  @dep_c.text_value = "hello this is some text for comparison"
135
147
  @dep_c.is_met?([@response]).should be_true
136
148
  @response.text_value = "Not the same text"
137
- @dep_c.is_met?([@response]).should be_false
149
+ @dep_c.is_met?([@response]).should be_false
138
150
  end
139
151
 
140
152
  it "knows an integer value response" do
@@ -154,14 +166,14 @@ describe DependencyCondition do
154
166
  @response.float_value = 130.123
155
167
  @dep_c.is_met?([@response]).should be_false
156
168
  end
157
-
158
169
  end
159
170
 
160
171
  describe "when if given a response object whether the dependency is satisfied using '!='" do
161
172
  before(:each) do
162
- @dep_c = DependencyCondition.new(:answer_id => 2, :operator => "!=")
163
- @select_answer = Answer.new(:question_id => 1, :response_class => "answer")
164
- @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => 2)
173
+ @select_answer = Answer.new(:question_id => 1, :response_class => "answer").
174
+ tap { |a| a.id = 2 }
175
+ @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => @select_answer.id)
176
+ @dep_c = DependencyCondition.new(:operator => "!=", :answer_id => @select_answer.id)
165
177
  @response.answer = @select_answer
166
178
  @dep_c.answer = @select_answer
167
179
  @dep_c.as(:answer).should == 2
@@ -190,7 +202,7 @@ describe DependencyCondition do
190
202
  @dep_c.text_value = "hello this is some text for comparison"
191
203
  @dep_c.is_met?([@response]).should be_false
192
204
  @response.text_value = "Not the same text"
193
- @dep_c.is_met?([@response]).should be_true
205
+ @dep_c.is_met?([@response]).should be_true
194
206
  end
195
207
 
196
208
  it "knows an integer value response" do
@@ -210,7 +222,6 @@ describe DependencyCondition do
210
222
  @response.float_value = 130.123
211
223
  @dep_c.is_met?([@response]).should be_true
212
224
  end
213
-
214
225
  end
215
226
 
216
227
  describe "when if given a response object whether the dependency is satisfied using '<'" do
@@ -220,7 +231,6 @@ describe DependencyCondition do
220
231
  @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => 2)
221
232
  @response.answer = @select_answer
222
233
  @dep_c.answer = @select_answer
223
-
224
234
  end
225
235
 
226
236
  it "knows operator on integer value response" do
@@ -240,7 +250,6 @@ describe DependencyCondition do
240
250
  @response.float_value = 130.123
241
251
  @dep_c.is_met?([@response]).should be_false
242
252
  end
243
-
244
253
  end
245
254
 
246
255
  describe "when if given a response object whether the dependency is satisfied using '<='" do
@@ -250,7 +259,6 @@ describe DependencyCondition do
250
259
  @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => 2)
251
260
  @response.answer = @select_answer
252
261
  @dep_c.answer = @select_answer
253
-
254
262
  end
255
263
 
256
264
  it "knows operator on integer value response" do
@@ -284,7 +292,6 @@ describe DependencyCondition do
284
292
  @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => 2)
285
293
  @response.answer = @select_answer
286
294
  @dep_c.answer = @select_answer
287
-
288
295
  end
289
296
 
290
297
  it "knows operator on integer value response" do
@@ -304,7 +311,6 @@ describe DependencyCondition do
304
311
  @response.float_value = 130.123
305
312
  @dep_c.is_met?([@response]).should be_true
306
313
  end
307
-
308
314
  end
309
315
 
310
316
  describe "when if given a response object whether the dependency is satisfied using '>='" do
@@ -314,7 +320,6 @@ describe DependencyCondition do
314
320
  @response = Response.new(:question_id => 314, :response_set_id => 159, :answer_id => 2)
315
321
  @response.answer = @select_answer
316
322
  @dep_c.answer = @select_answer
317
-
318
323
  end
319
324
 
320
325
  it "knows operator on integer value response" do
@@ -343,46 +348,54 @@ describe DependencyCondition do
343
348
  describe "when evaluating a pick one/many with response_class e.g. string" do
344
349
  it "should compare answer ids when the string_value is nil" do
345
350
  a = Factory(:answer, :response_class => "string")
346
- dc = Factory(:dependency_condition, :question_id => a.question.id, :answer_id => a.id, :operator => "==")
351
+ dc = Factory(:dependency_condition,
352
+ :question_id => a.question.id, :answer_id => a.id, :operator => "==")
347
353
  r = Factory(:response, :question_id => a.question.id, :answer_id => a.id, :string_value => "")
348
354
  r.should_receive(:as).with("answer").and_return(a.id)
349
355
  dc.is_met?([r]).should be_true
350
356
  end
357
+
351
358
  it "should compare strings when the string_value is not nil, even if it is blank" do
352
359
  a = Factory(:answer, :response_class => "string")
353
- dc = Factory(:dependency_condition, :question_id => a.question.id, :answer_id => a.id, :operator => "==", :string_value => "foo")
354
- r = Factory(:response, :question_id => a.question.id, :answer_id => a.id, :string_value => "foo")
360
+ dc = Factory(:dependency_condition,
361
+ :question_id => a.question.id, :answer_id => a.id,
362
+ :operator => "==", :string_value => "foo")
363
+ r = Factory(:response,
364
+ :question_id => a.question.id, :answer_id => a.id, :string_value => "foo")
355
365
  r.should_receive(:as).with("string").and_return("foo")
356
- dc.is_met?([r]).should be_true
366
+ dc.is_met?([r]).should be_true
357
367
 
358
- dc2 = Factory(:dependency_condition, :question_id => a.question.id, :answer_id => a.id, :operator => "==", :string_value => "")
359
- r2 = Factory(:response, :question_id => a.question.id, :answer_id => a.id, :string_value => "")
368
+ dc2 = Factory(:dependency_condition,
369
+ :question_id => a.question.id, :answer_id => a.id, :operator => "==", :string_value => "")
370
+ r2 = Factory(:response,
371
+ :question_id => a.question.id, :answer_id => a.id, :string_value => "")
360
372
  r2.should_receive(:as).with("string").and_return("")
361
- dc2.is_met?([r2]).should be_true
373
+ dc2.is_met?([r2]).should be_true
362
374
  end
363
375
  end
376
+
364
377
  describe "when given responses whether the dependency is satisfied using 'count'" do
365
378
  before(:each) do
366
- @dep_c = DependencyCondition.new(:answer_id => nil,
367
- :operator => "count>2")
379
+ @dep_c = DependencyCondition.new(:answer_id => nil,
380
+ :operator => "count>2")
368
381
  @question = Question.new
369
382
  @select_answers = []
370
- 3.times do
371
- @select_answers << Answer.new(:question => @question,
372
- :response_class => "answer")
383
+ 3.times do
384
+ @select_answers << Answer.new(:question => @question,
385
+ :response_class => "answer")
373
386
  end
374
387
  @responses = []
375
388
  @select_answers.slice(0,2).each do |a|
376
- @responses << Response.new(:question => @question, :answer => a,
377
- :response_set_id => 159)
389
+ @responses << Response.new(:question => @question, :answer => a,
390
+ :response_set_id => 159)
378
391
  end
379
- end
392
+ end
380
393
 
381
394
  it "knows operator with >" do
382
395
  @dep_c.is_met?(@responses).should be_false
383
- @responses << Response.new(:question => @question,
384
- :answer => @select_answers.last,
385
- :response_set_id => 159)
396
+ @responses << Response.new(:question => @question,
397
+ :answer => @select_answers.last,
398
+ :response_set_id => 159)
386
399
  @dep_c.is_met?(@responses).should be_true
387
400
  end
388
401