Skip to content

Commit

Permalink
Add happy and sad paths for query survey feature (#48)
Browse files Browse the repository at this point in the history
* adding happy and sad path for query survey feature, implementing all necessary logic

* making rubocop happy
  • Loading branch information
jacobtoddmathes authored Mar 8, 2024
1 parent 849dbd9 commit a4f93c1
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 8 deletions.
13 changes: 11 additions & 2 deletions rails_root/app/controllers/survey_responses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# frozen_string_literal: true

# Controller for survey responses

# rubocop:disable Metrics/ClassLength
class SurveyResponsesController < ApplicationController
before_action :set_survey_data, only: %i[show edit update destroy]
before_action :set_survey_sections, only: %i[show edit update new]

# GET /survey_responses or /survey_responses.json
def index
@survey_responses = SurveyResponse.all
if params[:query].present?
@survey_responses = SurveyResponse.where(share_code: params[:query])
flash[:warning] = "No survey responses found for share code #{params[:query]}" if @survey_responses.empty?
else
@survey_responses = SurveyResponse.all
end
end

# GET /survey_responses/1 or /survey_responses/1.json
Expand Down Expand Up @@ -50,7 +57,8 @@ def create_survey_response
rescue ActiveRecord::RecordNotFound
return respond_with_error 'invalid user_id'
end
@survey_response = SurveyResponse.new(profile:)
# create new survey response with unique share code
@survey_response = SurveyResponse.new(profile:, share_code: SecureRandom.hex(3))

create_survey_answers

Expand Down Expand Up @@ -161,3 +169,4 @@ def survey_response_params
params.require(:survey_response).permit! # FIXME: Figure out how to use strong params with new model
end
end
# rubocop:enable Metrics/ClassLength
6 changes: 6 additions & 0 deletions rails_root/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<!DOCTYPE html>

<% flash.each do |type, message| %>
<div class="alert alert-<%= type %>">
<%= message %>
</div>
<% end %>
<html>
<head>
<title>ELRC | Synergistic Leadership Theory</title>
Expand Down
14 changes: 13 additions & 1 deletion rails_root/app/views/survey_responses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@

<h1>Survey responses</h1>

<%= form_with url: survey_responses_path, method: :get do %>
<div class="form-group">
<label for="query">Query by case number</label>
<%= text_field_tag :query, params[:query], class: 'form-control', id: 'query' %>
</div>
<%= submit_tag 'Search', class: 'btn btn-primary' %>
<% end %>

<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Share code</th>
<th>Actions</th>
<th>Answers</th>
</tr>
</thead>
<tbody>
<% @survey_responses.each do |response| %>
<tr>
<td><%= response.profile.user_id %></td>
<td><%= response.profile.last_name %>, <%= response.profile.first_name %></td>
<td><%= response.share_code%></td>
<td><%= link_to "Show", response %></td>
<% response.answers.map do |answer| %>
<td><%= answer.choice %></td>
<% end %>
<td><%= link_to "Show", response %></td>
</tr>
<% end %>
</tbody>
Expand Down
10 changes: 5 additions & 5 deletions rails_root/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

# seed data for the survey_profiles table

# rubocop:disable Layout/LineLength
SurveyProfile.create!([
{ user_id: 5, first_name: 'John', last_name: 'Doe', campus_name: 'Campus 1',
district_name: 'District 1' },
Expand All @@ -28,14 +27,15 @@
SurveyResponse.create!(profile: survey_profile, share_code: "debug#{survey_profile.user_id}")
end

question = SurveyQuestion.create!(text: "Leads by Example", explanation: "This is a placeholder.", section: 0)
question = SurveyQuestion.create!(text: 'Leads by Example', explanation: 'This is a placeholder.', section: 0)
SurveyQuestion.create!(text: 'Empowers Others', explanation: 'This is a placeholder.', section: 1)
SurveyQuestion.create!(text: 'Builds Relationships', explanation: 'This is a placeholder.', section: 2)
SurveyQuestion.create!(text: 'Communicates Effectively', explanation: 'This is a placeholder.', section: 3)

SurveyResponse.all.each_with_index do |response, idx|
SurveyAnswer.create!(choice: idx, question: question, response: response)
SurveyAnswer.create!(choice: idx, question:, response:)
end

# rubocop:enable Layout/LineLength

# Path: csce606-ELRC-Synergistic-Leadership-Theory/rails_root/db/schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
Expand Down
19 changes: 19 additions & 0 deletions rails_root/features/query_survey.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: Query Survey information
Verify researchers and users can query survey responses with unique case number

Scenario:
Given survey profiles exist
And survey responses exist
And I am on the survey responses page
When I enter a unique case number in the "Query by case number field"
Then I see a list of survey responses with that case number

Scenario:
Given survey profiles exist
And survey responses exist
And I am on the survey responses page
When I enter a unique case number with no linked survey responses in the "Query by case number field"
Then I don't see a list of survey_responses with that case number
And a warning is flashed


5 changes: 5 additions & 0 deletions rails_root/features/step_definitions/common_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@
@model_attributes['SurveyResponse'] = {}
@model_attributes['SurveyResponse']['profile_id'] = 10
end

Given('survey profiles exist') do
SurveyProfile.create!(user_id: 1)
SurveyProfile.create!(user_id: 2)
end
2 changes: 2 additions & 0 deletions rails_root/features/step_definitions/model_rework_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Given('I have an invalid set of attributes for all models') do
@model_attributes = {}

Expand Down
39 changes: 39 additions & 0 deletions rails_root/features/step_definitions/query_survey_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

Given('survey responses exist') do
SurveyResponse.create!(profile_id: 1, share_code: '123')
SurveyResponse.create!(profile_id: 2, share_code: '456')
end

Given('I am on the survey responses page') do
visit survey_responses_path
end

When('I enter a unique case number in the {string}') do |_string|
fill_in 'query', with: '123'
click_button 'Search'
end

Then('I see a list of survey responses with that case number') do
# expect there to be survey responses with the case number in the table of survey responses
within 'table' do
expect(page).to have_content('123')
end
end

When('I enter a unique case number with no linked survey responses in the {string}') do |_string|
fill_in 'query', with: '789'
click_button 'Search'
end

Then("I don't see a list of survey_responses with that case number") do
# expect there to be no survey responses with the case number in the table of survey responses
within 'table' do
expect(page).not_to have_content('789')
end
end

Then('a warning is flashed') do
# expect there to be a warning flashed
expect(page).to have_content('No survey responses found for share code 789')
end

0 comments on commit a4f93c1

Please sign in to comment.