Skip to content

Commit d2ed7c3

Browse files
committed
Adds SurveyGenerator service.
This is an abstraction around survey generation and isn't called from anywhere at the moment. It still needs to be determined at what point surveys will be generated (e.g. right after a survey is completed; via cron job; etc). Also the algorithm is just taking the simple approach of picking two questions from each category. We are thinking of refining this in the future so that questions are selected based on previous survey responses.
1 parent 1dcd287 commit d2ed7c3

File tree

8 files changed

+92
-6
lines changed

8 files changed

+92
-6
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ gem "image_processing", "~> 1.2"
4242

4343
gem "frozen_record"
4444

45-
gem 'formtastic'
45+
gem "formtastic"
4646

4747
group :development, :test do
4848
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

app/models/answer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
class Answer < ApplicationRecord
22
belongs_to :survey
3+
4+
def question
5+
@question ||= Question.find(question_id)
6+
end
37
end

app/models/question.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
class Question < FrozenRecord::Base
22
FrozenRecord::Base.base_path = "db/frozen"
3+
4+
CATEGORIES = {
5+
emotional: "emotional",
6+
physical: "physical"
7+
}
38
end

app/services/survey_generator.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class SurveyGenerator
2+
def self.generate_for_user(user)
3+
survey = Survey.create!(user: user)
4+
5+
# TODO: Implement logic that will generate survey with questions based on answers from previous surveys.
6+
# For now we'll just use a random sampling of questions from each category.
7+
Question::CATEGORIES.each do |_, category|
8+
questions = Question.where(category: category).sample(2)
9+
questions.each do |question|
10+
Answer.create!(question_id: question.id, survey: survey)
11+
end
12+
end
13+
14+
survey
15+
end
16+
end

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
get "up" => "rails/health#show", as: :rails_health_check
77

88
get "/questions", to: "survey#questions"
9-
#get "/new", to: "users#new"
9+
# get "/new", to: "users#new"
1010
Rails.application.routes.draw do
11-
resources :users, only: [:new, :create] # Makes only `new` and `create` actions available for users
11+
resources :users, only: [ :new, :create ] # Makes only `new` and `create` actions available for users
1212
end
1313

1414
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)

db/frozen/questions.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@
77
- id: 3
88
category: 'physical'
99
text: 'I sleep well & feel rested upon waking'
10-
10+
- id: 4
11+
category: 'emotional'
12+
text: 'I do not feel emotionally run-down or drained.'
13+
- id: 5
14+
category: 'physical'
15+
text: 'I do not experience stress-related dreams or nightmares.'
16+
- id: 6
17+
category: 'emotional'
18+
text: 'I have fun & engage in restorative activities daily.'

test/fixtures/answers.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
22

3-
answer_one:
3+
answered_one:
44
question_id: 1
55
survey: answered
66
answer: 1
77

8-
answer_two:
8+
answered_two:
99
question_id: 2
1010
survey: answered
1111
answer: 2
12+
13+
answered_three:
14+
question_id: 3
15+
survey: answered
16+
answer: 3
17+
18+
answered_four:
19+
question_id: 4
20+
survey: answered
21+
answer: 4
22+
23+
unanswered_one:
24+
question_id: 1
25+
survey: unanswered
26+
27+
unanswered_two:
28+
question_id: 2
29+
survey: unanswered
30+
31+
unanswered_three:
32+
question_id: 3
33+
survey: unanswered
34+
35+
unanswered_four:
36+
question_id: 4
37+
survey: unanswered
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "test_helper"
2+
3+
class SurveyGeneratorTest < ActiveSupport::TestCase
4+
test "generates a survey for a given user" do
5+
user = users(:bob)
6+
survey = SurveyGenerator.generate_for_user(user)
7+
8+
assert_equal user, survey.user
9+
end
10+
11+
test "generates a survey with a sampling of questions from each category" do
12+
user = users(:bob)
13+
survey = SurveyGenerator.generate_for_user(user)
14+
15+
question_category_count = Question::CATEGORIES.keys.index_with(0)
16+
survey.answers.each do |answer|
17+
assert question_category_count.key?(answer.question.category.to_sym)
18+
question_category_count[answer.question.category.to_sym] += 1
19+
end
20+
21+
expected_count_from_each_category = 2
22+
question_category_count.each do |category, count|
23+
assert_equal expected_count_from_each_category, count,
24+
"Incorrect number of questions from category: #{category}"
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)