Skip to content

Commit

Permalink
Merge pull request #155 from codemancers/add-results-per-attempt
Browse files Browse the repository at this point in the history
Added results per attempt
  • Loading branch information
iffyuva authored Dec 4, 2024
2 parents 1caf693 + 28eaa23 commit 3dfada0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
7 changes: 7 additions & 0 deletions app/controllers/rapidfire/surveys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def destroy
def results
params[:filter] ||= {}
@survey = Rapidfire::Survey.find(params[:id])
@survey_attempts = @survey.attempts
@survey_results =
SurveyResults.new(survey: @survey).extract(filter_params)

Expand All @@ -83,6 +84,12 @@ def results
end
end

def show_result
@survey = Rapidfire::Survey.find(params[:survey_id])
@attempt = Rapidfire::Attempt.find(params[:id])
@answers = @attempt.answers.includes(:question)
end

private

def survey_params
Expand Down
34 changes: 4 additions & 30 deletions app/views/rapidfire/surveys/results.html.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
<h1>Results</h1>
<%= link_to "Download CSV", results_survey_path(id: params[:id], format: :csv) %>
<%- @survey_results.each do |result| %>
<div>
<h4><%= result.question.question_text %></h4>
<p>
<%- if result.results.is_a?(Array) %>
<ol>
<%- result.results.each do |answer| %>
<li><%= answer %></li>
<% end %>
</ol>
<% elsif result.results.is_a?(Hash) %>
<table>
<thead>
<tr>
<th>Option</th>
<th>Count</th>
<th>Filter</th>
</tr>
</thead>
<tbody>
<%- result.results.each do |option, count| %>
<tr>
<td><%= option %></td>
<td><%= count %></td>
<td><%= filter_link(result.question.id, option) %></th>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<%- @survey_attempts.each do |attempt| %>
<div class="flex justify between">
<h4>User : <%= attempt.user_id %></h4>
<%= link_to "View Answers", survey_result_path(@survey, attempt.id) %>
</div>
<% end %>
17 changes: 17 additions & 0 deletions app/views/rapidfire/surveys/show_result.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Survey Result</h1>

<p><strong>Survey Name:</strong> <%= @survey.name %></p>
<p><strong>User ID:</strong> <%= @attempt.user_id %></p>

<div>
<% @answers.each do |answer| %>
<div class="question-answer-pair">
<p><strong>Question:</strong> <%= answer.question.question_text %></p>
<p><strong>Answer:</strong> <%= answer.answer_text %></p>
<hr>
</div>
<% end %>
</div>


<%= link_to "Back to All Results", results_survey_path(@survey) %>
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Rapidfire::Engine.routes.draw do
resources :surveys do
get 'results', on: :member
member do
get 'results'
end

get 'results/:id', to: 'surveys#show_result', as: 'result'

resources :questions
resources :attempts, only: [:new, :create, :edit, :update, :show]
Expand Down
24 changes: 20 additions & 4 deletions spec/features/rapidfire/managing_surveys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
context "when user can administer" do
before do
allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true)

visit rapidfire.root_path
click_button "Delete"
end
Expand Down Expand Up @@ -148,9 +147,26 @@
end
end

it "shows results for particular question group" do
expect(page).to have_content "Results"
expect(page).to have_content "hindi 3"
it "shows results for all attempts" do
survey.attempts.each do |attempt|
expect(page).to have_content "User: #{attempt.user_id}"
expect(page).to have_link "View Answers", href: result_survey_path(survey, attempt.id)
end
end

context "when viewing an individual attempt" do
let(:attempt) { survey.attempts.first || survey.attempts.create(user_id: 1) }

before do
visit rapidfire.survey_result_path(survey, attempt.id)
end

it "displays the questions and answers for the attempt" do
attempt.answers.each do |answer|
expect(page).to have_content answer.question.question_text
expect(page).to have_content answer.answer_text
end
end
end
end
end

0 comments on commit 3dfada0

Please sign in to comment.